Post

Create new content type in SharePoint Online with PowerShell
This is part 5 of 10 where we will be creating a new content type in SharePoint Online with PowerShell. This is part of the following series: We will be collecting all available web templates in part 1 so we can use this to create a new site in part 2. In part 3 we […]

This is part 5 of 10 where we will be creating a new content type in SharePoint Online with PowerShell. This is part of the following series:

We will be collecting all available web templates in part 1 so we can use this to create a new site in part 2. In part 3 we will be creating a web for the newly created site. We then want to create a couple of site columns in part 4 which we will combine to a content type in part 5. This content type will be added (part 8) to our newly created document library in part 7 using a list template from part 6. After everything is set we will be setting the view in part 9 for this list to show the added columns we got from adding the content type. We only want to set permissions for myself so I’ll will be breaking the inheritance and setting permissions in part 10.

 Create new content type in SharePoint Online

This script will create a new content type in SharePoint Online using CSOM. We have created three site columns in part 4 and we will be adding them to a new content type.

We will first start by opening the SharePoint Online Management Shell as administrator which can be downloaded at https://www.microsoft.com/en-us/download/details.aspx?id=35588.

image

You will need to change the first variables to match your Office 365 tenant and copy this bit to PowerShell.

function new-SPOnlineContentType {
    #variables that needs to be set before starting the script
    $siteURL = "https://spfire.sharepoint.com/sites/blogdemo"
    $adminUrl = "https://spfire-admin.sharepoint.com"
    $userName = "mpadmin@spfire.onmicrosoft.com"
    $contentTypeGroup = "My Content Types"
    $contentTypeName = "Blog Content Type"
    $columns = "BlogNumber", "BlogText", "BlogUser"
    $parentContentTypeID = "0x0101"
    
    # Let the user fill in their password in the PowerShell window
    $password = Read-Host "Please enter the password for $($userName)" -AsSecureString
    
    # set SharePoint Online credentials
    $SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $password)
        
    # Creating client context object
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
    $context.credentials = $SPOCredentials
    $fields = $context.web.fields
    $contentTypes = $context.web.contenttypes
    $context.load($fields)
    $context.load($contentTypes)
    
    # send the request containing all operations to the server
    try{
        $context.executeQuery()
        write-host "info: Loaded Fields and Content Types" -foregroundcolor green
    }
    catch{
        write-host "info: $($_.Exception.Message)" -foregroundcolor red
    }
        
    # Loop through all content types to verify it doesn't exist
    foreach ($contentType in $contentTypes){
        if ($contentType.name -eq $contentTypeName){
            write-host "Info: The content type $($contentTypeName) already exists." -foregroundcolor red
            $contentTypeExists = $true
        }
        else{
            $contentTypeExists = $false
        }
    }
        
    # create content type if it doesnt exist based on specified Content Type ID
    if($contentTypeExists -eq $false){
        # load parent content type
        $parentContentType = $contentTypes.GetByID($parentContentTypeID)
        $context.load($parentContentType)
        
        # send the request containing all operations to the server
        try{
            $context.executeQuery()
            write-host "info: loaded parent Content Type" -foregroundcolor green
        }
        catch{
            write-host "info: $($_.Exception.Message)" -foregroundcolor red
        }
        
        # create Content Type using ContentTypeCreationInformation object (ctci)
        $ctci = new-object Microsoft.SharePoint.Client.ContentTypeCreationInformation
        $ctci.name = $contentTypeName
        $ctci.ParentContentType = $parentContentType
        $ctci.group = $contentTypeGroup
        $ctci = $contentTypes.add($ctci)
        $context.load($ctci)
        
        # send the request containing all operations to the server
        try{
            $context.executeQuery()
            write-host "info: Created content type" -foregroundcolor green
        }
        catch{
            write-host "info: $($_.Exception.Message)" -foregroundcolor red
        }
        
        # get the new content type object
        $newContentType = $context.web.contenttypes.getbyid($ctci.id)
        
        # loop through all the columns that needs to be added
        foreach ($column in $columns){
            $field = $fields.GetByInternalNameOrTitle($column)
            #create FieldLinkCreationInformation object (flci)
            $flci = new-object Microsoft.SharePoint.Client.FieldLinkCreationInformation
            $flci.Field = $field
            $addContentType = $newContentType.FieldLinks.Add($flci)
   write-host "info: added $($column) to array" -foregroundcolor green
        }        
        $newContentType.Update($true)
        
        # send the request containing all operations to the server
        try{
            $context.executeQuery()
            write-host "info: Added columns to content type" -foregroundcolor green
        }
        catch{
            write-host "info: $($_.Exception.Message)" -foregroundcolor red
        }
    }
}
new-SPOnlineContentType

image

You will be asked to enter the password and press enter

image

You can verify if the content type has been created successfully by going to site settings and then content types.

image

Open the content type to view if the columns also have been added

image

Tips

You can find more properties for the ContentTypeCreationInformation class at https://msdn.microsoft.com/EN-US/library/office/microsoft.sharepoint.client.contenttypecreationinformation_members.aspx.

You can find more properties for the FieldLinkCreationInformation class at https://msdn.microsoft.com/EN-US/library/office/microsoft.sharepoint.client.fieldlinkcreationinformation_members.aspx.

You can find the Content Type ID with PowerShell or navigate to site settings and then click on content types and then click on the content type you wish to be the parent

image

You can find the ID in the URL after ctype=<ContentTypeID>

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Archive