SharePoint 2013 Create Site Column and Content Types Using PowerShell

create content type in sharepoint 2013 using powershell
InfoPath alternatives for form designing SharePoint

In this SharePoint 2013 article we will discuss how we can create site column and content type using PowerShell in SharePoint 2013.  Automating deployment process will always improves the productivity and can eradicate manual errors. In this article I am enhancing our PowerShell knowledge by adding one more feather creating “Site Columns and Content Types”. This will definitely help while setting up a larger site collection with multiple site columns and content types.

The below script will create a new content type group, content type, Site column group with one site column.
try {
$url = “http://sharepoint13:12345/”
$web = get-spweb $url
$MSTechnologyCTName = “MSTechnology Content Type”
$columnName = “Technology”
$contentTypes = $web.AvailableContentTypes | Select-Object Name
$cntTypeflag = $false
# We are taking Item as parent content type to inherit from “0x01” is the ID for Item content type
$parentcntType = $web.contenttypes | where {$_.ID -eq “0x01”} | Select-Object Name
foreach ($contType in $contentTypes) {
if ($contType.Name -eq $MSTechnologyCTName) {
$cntTypeflag = $true
Write-Host “Content Type=” $contType.Name -foregroundcolor White -BackgroundColor Green
}
}



if (!$cntTypeflag) {
$MSTechnologyCTParent = $web.availablecontenttypes[$parentcntType]
$MSTechnologyCT = new-object Microsoft.SharePoint.SPContentType($MSTechnologyCTParent, $web.contenttypes, $MSTechnologyCTName)
$MSTechnologyCT.Group = “MSTechnology Content Types Group”
$web.ContentTypes.Add($MSTechnologyCT)
$web.fields.add($columnName, “Text”, $false)
$MSTechnologyfield = $web.Fields.GetField($columnName)
$MSTechnologyfield.Group = “MSTechnology Site Columns Group”
$MSTechnologyfield.Title = $columnName
$MSTechnologyfield.Update()
$MSTechnology_fieldLink = New-OBject Microsoft.SharePoint.SPFieldLink($MSTechnologyfield)
$MSTechnologyCT.FieldLinks.Add($MSTechnology_fieldLink)
$MSTechnologyCT.Update()
}
else {
Write-Host “Content Type existed in site collection”
}
}
catch {
Write-Host “`nError:: $($_.Exception.Message)” -foregroundcolor red -BackgroundColor Yellow
}

Let us walk through the code Line by Line:



Line 1: As per the MS best practices, I have rapped my code inside a try catch block
Line 2: Created a variable $url for site collection http://sharepoint13:12345
Line 3: We are creating object to site collection http://sharepoint13:12345
Line 4 to 7: I am creating variables for various values like name of the content type to be created, column name to be created, content type existence flag and then I am fetching all the existing content types in the site collection with the below line of code.
$web.AvailableContentTypes | Select-Object Name
Note: In this statement I am selecting the name of content type from the entire collection of properties
Line 9: While creating content type we must define the parent content type from which we are inheriting the new content type. In the below line I am getting the name of “Item” content type.
$parentcntType = $web.contenttypes | where {$_.ID -eq “0x01”} | Select-Object Name
You can see more about the parent content types and the hexadecimal formats in the MSDN article:
Line 10 to 15: I am looping through the content types and ensuring that “MSTechnology Content Type” is not existed in case if it is existed I am showing an error message that it is already existed.
Line 18 to 29:  I am creating below artifacts:
-> Site column “Technology”
-> Site column group “MSTechnology Site Columns Group”
-> Site Content Type Group “MSTechnology Content Types Group”
-> Creating “MSTechnology Content Type”
Once you run the script you can verify the new artifacts in site collection by navigating to Site Settings -> Site Columns.
Newly created site column & Group with PowerShell script:
create site column in sharepoint 2013 using powershell
create site column in sharepoint 2013 using powershell

Newly Created site content type & group with PowerShell script:

create content type in sharepoint 2013 using powershell
create content type in sharepoint 2013 using powershell

Hope this helps, happy administration with PowerShell !!!

Similar SharePoint 2013 Tutorials

About Krishna Vandanapu

I am Krishna.Vandanapu a SharePoint architect working in IT from last 12 years, I worked in SharePoint 2007, 2010, 2013, 2016 and Office 365. I have extensive hands on experience in customizing SharePoint sites from end to end. Expertise in SharePoint migration tools like Sharegate, Doc Ave and Metalogix. Migrated SharePoint sites from SharePoint 2007 to 2010 and 2010 to 2013 several times seamlessly. Implementing CSOM with Microsoft best practices. Spent quality time in configuring SharePoint application services like User Profile, Search, Managed Meta data services etc. Now exploring SharePoint Framework and SharePoint 2019

View all posts by Krishna Vandanapu →

Leave a Reply