kwizcom banner advertisement

SharePoint Online: Enable Content Types using PowerShell

As the name suggests, Content type defines type of the content such as item or document. In a real world scenario, A content type could refer to a invoice template or project plan document, etc. Content types provide a effective method of grouping relevant columns (metadata), document template, workflows and policies together and helps to standardize a template for reuse. Typically content types are created at the top level site as they are hierarchical in nature. In other words a content type created at root site gets inherited to all its subsites automatically. This saves lot of time as you need to create that content type only once and reuse it wherever required.  

How to Enable Content Types in SharePoint Online?
To enable content type in SharePoint Online list,
  • Go to List >> List Settings >> Advanced Settings
  • Set "Yes" for "Allow management of content types?" and hit OK button to save your changes.
    enable content types in sharepoint online
If you go back to list settings, you'll find a section "Content Types" with the particular list or library's default content type listed (E.g. Item for custom list!). You an add any existing content types to the list now!

PowerShell to Enable Content Types in SharePoint Online List 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 to turn ON Content Type in SharePoint Online list or library
Function Enable-ContentTypes()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName
    )

    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 object
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        $Ctx.Load($List)
        $Ctx.ExecuteQuery()

        #Check if content types are already enabled
        If($List.ContentTypesEnabled -eq $True)
        {
            Write-host "Content Types Already Enabled in the given List!" -ForegroundColor Yellow
        }
        else
        {
            #enable content types in the list
            $List.ContentTypesEnabled=$True
            $List.Update()
            $Ctx.ExecuteQuery()

            Write-host "Content Types Enabled for given List Successfully!" -ForegroundColor Green  
        }
    }
    Catch {
        write-host -f Red "Error enabling Content Type!" $_.Exception.Message
    } 
} 

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"

#Call the function
Enable-ContentTypes -SiteURL $SiteURL -ListName $ListName
SharePoint Online: Enable Content Types using PowerShell SharePoint Online: Enable Content Types using PowerShell Reviewed by Salaudeen Rajack on January 16, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.