kwizcom banner advertisement

SharePoint Online: Set Site Column Properties using PowerShell

Requirement: Update Properties of a Site Column in SharePoint Online using PowerShell
sharepoint online update site column powershell

SharePoint Online: Set Site Column Properties using PowerShell CSOM
#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 Set-SPOSiteColumn($SiteURL,$ColumnName)
{
    #Setup Credentials to connect
    $Cred = Get-Credential
    $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

    Try {
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Cred

        #Check if the site column exists
        $Fields = $Ctx.web.Fields
        $Ctx.Load($Fields)
        $Ctx.ExecuteQuery()
        $FieldNames = $Fields | Select -ExpandProperty InternalName

        #Get the site column from sharepoint online site
        If($FieldNames.Contains($ColumnName))
        {
            #Get the site column
            $SiteColumn = $Fields.GetByInternalNameOrTitle($ColumnName)

            #Update Field Properties
            $Sitecolumn.Required = $True
            $Sitecolumn.Description="Current Status of the Project"
            $SiteColumn.Update()
            #Use: $siteColumn.UpdateAndPushChanges() to push changes to all lists

            $Ctx.ExecuteQuery()
            Write-host -f Green "Site Column Updated Successfully!"
        }
        else
        {
            Write-host -f Yellow "Site Column doesn't Exist!"
        }
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}

#Call the Function with Site URL and Internal Name of the Field
Set-SPOSiteColumn -SiteURL "https://crescenttech.sharepoint.com" -ColumnName "ProjectName"

Update Site Column Properties using PnP PowerShell
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"
$FieldName = "ProjectStatus" #Internal Name

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PNP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
    #Set Site Column Properties
    Set-PnPField -Identity $FieldName -Values @{Description="Current Status of the Project";Group="Crescent Site Columns V2"}    
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
We also have a -UpdateExistingLists switch to update the site column in all existing Lists.
SharePoint Online: Set Site Column Properties using PowerShell SharePoint Online: Set Site Column Properties using PowerShell Reviewed by Salaudeen Rajack on April 28, 2018 Rating: 5

2 comments:

  1. After running this script getting below error

    Add-Type : Cannot bind parameter 'Path' to the target. Exception setting "Path": "Cannot find path 'C:\Program Files\Common
    Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll' because it does not exist."

    Downloaded both the packages Client.dll and Client.Runtime.dll then also getting same error. Please help.

    ReplyDelete
    Replies
    1. Make sure you downloaded & installed the SharePoint Online SDK from https://www.microsoft.com/en-us/download/details.aspx?id=42038. In case you installed it in different location, set the path accordingly in the script!

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.