SharePoint Online: Add Content Type to List or Library using PowerShell

Requirement: SharePoint Online PowerShell to add content type to library.

How to add content type to list or library in SharePoint Online?
Assuming you have the content type already created, here are the steps to add content type to list in SharePoint online:

Step 1: Enable content types management for the list, if its not enabled already
  • Navigate to the List settings >> Click on "Advanced Settings" link under General Settings
  • Set "Yes" for "Allow management of content types?" option and hit OK button in the bottom of the page to save your changes.
Step 2: Add Content Type to List or Library
Once content types are enabled, here is how you can add content type to list or library in SharePoint Online:
  • Go to the list settings page again, You'll find "Content Types" group created with the default content type listed (E.g. You'll find "Item" content type listed for custom lists).
  • Click on "Add from existing site content types" Link 
    sharepoint online powershell add content type to library
  • From the Add content types page, select your content type to add and click on "Add" button and then "OK" once.
    sharepoint online add content type to list
  • That's all. Now you'll find the content type you added under "Content Types" group of list settings.

SharePoint Online PowerShell to Add Content Type to Library
Lets add content type to list in SharePoint Online using PowerShell
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Function Add-ContentTypeToList()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $CTypeName
    )

    Try {
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
        
        #Get the List
        $List = $Ctx.web.Lists.GetByTitle($ListName)
        $Ctx.Load($List)
        $Ctx.ExecuteQuery()
        
        #Enable managemnt of content type in list - if its not enabled already
        If($List.ContentTypesEnabled -ne $True)
        {
            $List.ContentTypesEnabled = $True
            $List.Update()
            $Ctx.ExecuteQuery()
            Write-host "Content Types Enabled in the List!" -f Yellow
        }

        #Get all existing content types of the list
        $ListContentTypes = $List.ContentTypes
        $Ctx.Load($ListContentTypes)

        #Get the content type to Add to list
        $ContentTypeColl = $Ctx.Web.ContentTypes
        $Ctx.Load($ContentTypeColl)
        $Ctx.ExecuteQuery()
        
        #Check if the content type exists in the site        
        $CTypeToAdd = $ContentTypeColl | Where {$_.Name -eq $CTypeName}
        If($CTypeToAdd -eq $Null)
        { 
            Write-host "Content Type '$CTypeName' doesn't exists in  '$SiteURL'" -f Yellow
            Return 
        }

        #Check if content type added to the list already
        $ListContentType = $ListContentTypes | Where {$_.Name -eq $CTypeName}
        If($ListContentType -ne $Null)
        {
            Write-host "Content type '$CTypeName' already exists in the List!" -ForegroundColor Yellow
        }
        else
        {
            #Add content Type to the list or library
            $AddedCtype = $List.ContentTypes.AddExistingContentType($CTypeToAdd)
            $Ctx.ExecuteQuery()
           
            Write-host "Content Type '$CTypeName' Added to '$ListName' Successfully!" -ForegroundColor Green
        }
   }
    Catch {
        write-host -f Red "Error Adding Content Type to the List!" $_.Exception.Message
    } 
}

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Project Frosty"
$CTypeName="Project Template"

#Call the function
Add-ContentTypeToList -SiteURL $SiteURL -ListName $ListName -CTypeName $CTypeName

SharePoint Online PnP PowerShell to Add Content Type to Library 
Here is how to add content type to document library in SharePoint Online using Add-PnPContentTypeToList PnP PowerShell
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com/sites/marketing"
$ContentTypeName="Crescent Invoice Template V2"
$ListName="Invoices"

#Get Credentials to connect
$Cred = Get-Credential

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
#Get the content type
$ContentType = Get-PnPContentType -Identity $ContentTypeName

If($ContentType)
{
    #Add Content Type to Library
    Add-PnPContentTypeToList -List $ListName -ContentType $ContentType
}
else
{
    Write-host -f Yellow "Could Not Find Content Type:"$ContentTypeName
}
Use -DefaultContentType switch to make the content type as default content type!
SharePoint Online: Add Content Type to List or Library using PowerShell SharePoint Online: Add Content Type to List or Library using PowerShell Reviewed by Salaudeen Rajack on June 17, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.