Friday, June 17, 2016

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

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

sharepoint online add content type to list 
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
#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



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!


You might also like:

Related Posts Plugin for WordPress, Blogger...