Automating the process of creating Content Types can help speed up a deployment, and reduce the risk of human-error. Here in this blog specify how to create content type using PowerShell.
There are common steps to start with a PowerShell script as following.
Step 1: As always, start by adding the SharePoint module.
Add-PSSnapin Microsoft.SharePoint.PowerShell
Step 2: We use Start-SPAssignment to ensure that all objects are disposed.
Start-SPAssignment –Global
Step 3: n this step is to open the web where you want to add the Content Type to.
$url = "http://sharepoint/"
$web = get-spweb $url
Step 4: Provide a Name to the new Content Type.
$kirtictypeName = "Kirti Content Type"
Step 5: We need a parent Content Type for our new Content Type. We can get a list of available Content Types by running
$web.AvailableContentTypes | Select-Object Name
This gives an output of all of the Content Types available on the current web as following figure.
Step 6: we need a reference to the parent Content Type. We can a name from available Content Type as per our requirement and use the reference as parent, and create a new content type,
$kirtictypeParent = $web.availablecontenttypes["Item"]
$Kirtictype = new-object Microsoft.SharePoint.SPContentType($kirtictypeParent, $web.contenttypes, $ kirtictypeName)
$Kirtictype.Group = "Kirti Content Type Group"
$web.ContentTypes.Add($Kirtictype)
Step 7: Add a custom site column to the newly created content type.
$web.fields.add(“My Column", "Text", $false)
Step 8: Add a custom site column to the newly created content type.
$Kirtifield = $web.Fields.GetField("My Column")
$ Kirtifield.Group = "Kirti Site Column Group"
Step 9: Now create a reference to newly created site column and add the field to the newly create content type.
$Kirti_fieldLink = New-OBject Microsoft.SharePoint.SPFieldLink($Kirtifield)
$ Kirtictype.FieldLinks.Add($Kirti_fieldLink)
Step 10: Now save the changes to SharePoint and stop the Assignment. Found the result of the script as following image.
$ Kirtictype.Update()
Stop-SPAssignment –Global