SharePoint Online: Set Site Column Properties using PowerShell

Requirement: Update Properties of a Site Column in SharePoint Online using PowerShell

How to Edit a Site Column in SharePoint Online?
You can edit site column properties such as name, group, column type, etc in SharePoint Online by following below steps:
  1. Navigate to the SharePoint Online site where the site column was created.
  2. Click on Settings Gear >> Site Settings
  3. On the Site Settings page, Click on "Site Columns" link under "Web Designer Galleries" group
  4. On the Site Columns page, click the name of the site column to be edited.
  5. On the Edit Site Column page, You can make necessary changes and set whether all list columns based on this site column are to be updated to reflect your changes.
  6. Click the OK button to save the changes. sharepoint online update site column powershell
If the site column name is not clickable, then the site column may be created in the parent site. Navigate to the parent site as indicated by "Source Column" where the site column was created and then you can edit the site column.

SharePoint Online: Set Site Column Properties using PowerShell CSOM
Here is the PowerShell to update field in SharePoint Online:
#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.