Adding a site column to multiple document libraries

Diane Walters 1 Reputation point
2021-04-21T12:33:14.077+00:00

I want to add a site column/site columns to a number of document libraries at the same time. Essentially I have 50+ document libraries set up and I now need to add a new column to all of them. TIA

SharePoint Server
SharePoint Server
A family of Microsoft on-premises document management and storage systems.
2,221 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jerry Xu-MSFT 7,921 Reputation points
    2021-04-23T07:06:01.487+00:00

    Hi, @Diane Walters ,

    It may be better to use PowerShell to finish the process in bulk. Here is a CSOM script below which you can manually add all the list names inside. Replace the placeholders and run the script in PowerShell. It works for the situation where you only want to add the site column to certain libraries.

    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"  
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
       
    Function Add-SiteColumnToLibrary()  
    {  
    	  
        param  
        (  
            [Parameter(Mandatory=$true)] [string] $LibraryName,  
            [Parameter(Mandatory=$true)] [string] $SiteColumnName  
        )  
    	Try{  
    		  
    		#Get the Site column  
    		$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  
    		{  
    			#Get the list  
    			$List=$Ctx.Web.Lists.GetByTitle($LibraryName)  
           
    			#Get the Site column  
    			$Field = $Web.Fields.GetByTitle($SiteColumnName)  
       
    			#Add the site column to the list  
    			$List.Fields.Add($Field)  
    			$ctx.ExecuteQuery()   
    		}  
    		Write-host "Site Column '$SiteColumnName' Added to the library '$LibraryName' Successfully!" -f Green  
    	}  
    	Catch {  
            write-host -f Red "Error Adding Site Column to Content Type!" $_.Exception.Message  
        }   
    }  
      
    #Variables  
    $SiteURL="<site URL>"  
    $LibraryNames=@("<LibraryName1>", "<LibraryName1>", "<LibraryName1>")  
    $SiteColumnName="<Site Column Name>"  
      
    #Setup the context  
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)  
    $Web = $Ctx.web  
      
    #Call the function to add site column to document libraries  
    foreach($LibraryName in $LibraryNames)  
    {  
    	Add-SiteColumnToLibrary -LibraryName $LibraryName -SiteColumnName $SiteColumnName  
    }  
    

    I am not sure whether you have a csv file holding all the library names or you are doing the process for all libraries in your site. Have a try with the script first. If that does not work for you, please let know more information about how you pick up the libraries.


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. sadomovalex 3,626 Reputation points
    2021-04-23T14:42:17.69+00:00

    you can do that with Add-PnPField cmdlet:
    Add-PnPField -List "MyList" -Field {fieldNameOrId}

    where {fieldNameOrId} is internal name or id of your site column.

    0 comments No comments