Monday, January 18, 2016

SharePoint Online: How to Add a Site Column to Content Type using Powershell

Requirement:  Add site column to content type in SharePoint Online.

How to add field to existing content type in SharePoint?
By default Title column gets added to the content type. However, you may want to add additional columns.

  •  Go to the SharePoint Online site where you want Add a column to site content type. Click on Settings >> Click Site Settings. 
  • In Site Settings, Click on Site content types under Web Designer Galleries section
  • Select your target content type to modify. In site content types page, under Columns section, click on "Add from existing site columns" link.
    sharepoint online add column to content type powershell
  • This takes you to a page where you can select any existing site column. Select the appropriate  group and pick the site column you want to add, and then click Add to move the column to the Columns to add list.
    sharepoint online add column to content type powershell
Lets see how to add column to content type using PowerShell.

PowerShell to Add Site Column to Content Type in SharePoint Online:
Lets add column to existing content type programmatically with 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-SiteColumnToContentType()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ContentTypeName,
        [Parameter(Mandatory=$true)] [string] $SiteColumnName
    )

    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 content type
        $ContentTypeColl = $Ctx.Web.ContentTypes
        $Ctx.Load($ContentTypeColl)
        $Ctx.ExecuteQuery()
        
        #Check if the content type exists in the site        
        $ContentType = $ContentTypeColl | Where {$_.Name -eq $ContentTypeName}
        If($ContentType -eq $Null)
        { 
            Write-host "Content Type '$ContentTypeName' doesn't exists in  '$SiteURL'" -f Yellow
            Return 
        }
        #Get the site column to add
        $SiteColumnColl = $Ctx.Web.Fields
        $Ctx.Load($SiteColumnColl)
        $Ctx.ExecuteQuery()
        $SiteColumn = $SiteColumnColl | Where {$_.Title -eq $SiteColumnName}
    
        #Check if given site column exists
        if($SiteColumn -eq $Null)
        {
            Write-host "Site Column '$SiteColumnName' doesn't exists!" -f Yellow
            Return
        }
        else
        {
            #Check if site column already added to the content type
            $FieldCollection = $ContentType.Fields
            $Ctx.Load($FieldCollection)
            $Ctx.ExecuteQuery()
            $Field = $FieldCollection | Where {$_.Title -eq $SiteColumnName}
            if($Field -ne $Null)
            {
                Write-host "Site Column '$SiteColumnName' Already Exists in the content type!" -f Yellow
                Return
            }
    
            #Add a field to content type
            $FieldLink = New-Object Microsoft.SharePoint.Client.FieldLinkCreationInformation
            $FieldLink.Field = $SiteColumn
            [Void]$ContentType.FieldLinks.Add($FieldLink)
            $ContentType.Update($true)
            $Ctx.ExecuteQuery() 
       
            Write-host "Site Column '$SiteColumnName' Added to '$ContentTypeName' Successfully!" -ForegroundColor Green
        }
   }
    Catch {
        write-host -f Red "Error Adding Site Column to Content Type!" $_.Exception.Message
    } 
}

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ContentTypeName="ProjectTemplate"
$SiteColumnName="Project Title"

#Call the function
Add-SiteColumnToContentType -SiteURL $SiteURL -ContentTypeName $ContentTypeName -SiteColumnName $SiteColumnName



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...