This is my third blog post in the Automating SharePoint Online using PowerShell + CSOM Series. In the last article we saw how to authenticate against SharePoint Online site and how to access Web and List using PowerShell + CSOM. Here is the blog post.
Automating SharePoint Online Tasks (O365) using PowerShell + CSOM – Part 1
Automating SharePoint Online Tasks (O365) using PowerShell + CSOM – Part 2
Just reiterate, we are going to create site columns and content types and then adding site columns to content type and then finally adding content type to list.
So in this blog post I am going to show you how to write a code to create a Content Types using PowerShell and CSOM.
# Rather than writing authentication code you can create PS1 file and refer to your code like this.
# ......START Authenticate....................
.\1_Authenticate.ps1
#........END Authenticate.....................
#……..START – Get Content Types……………………
$web = $ctx.Web
$contentTypes = $web.ContentTypes;
$ctx.Load($web)
$ctx.Load($contentTypes);
$ctx.ExecuteQuery()
#……..END – Get Content Types……………
# Get the Parent Content Type – Here I am getting Document Content Type.
$docCT = $contentTypes.GetById(“0x0101”)
$ctx.Load($docCT);
$ctx.ExecuteQuery();
#Create a Content Type Information object
$cType = New-Object Microsoft.SharePoint.Client.ContentTypeCreationInformation
#Set the name for the content type
$cType.Name = “SPS Presentations”;
#Inherit from oob document – 0x0101 and assign
$cType.ParentContentType = $docCT ;
#...POST SP1 use the following If you are using SP 2013 RTM or anything prior to SP1 then you cannot assign Content Type ID.
# SharePoint will assign the ID automatically. You only have to assign Parent Content Type ID like I have done above.
#But if you are using SP1 then You can assign your own ID.
#$cType.Id = "0x0101.......";
#Set content type to be avaialble from specific group
$cType.Group = "SPSBoston Content Types";
#Create the content type
$spsBostonCT = $contentTypes.Add($cType);
$ctx.ExecuteQuery();