Creating the Site Columns/Fields Using CSOM in PowerShell in Office 365 / SharePoint Online

Step 1: Preparing the .CSV file having all the site columns and values for their respective properties which we want to set.

For example the below .CSV file which we are using in this script,

CSV
                                                      Figure 1: Site Columns CSV file format

In the above .CSV file, column headings are basically the properties of the site columns. We can add any property as the column which we want to set. Each row represents the site column and their properties values.

Step 2:

Set the required variables like SiteCollection URL where we need to create the site columns,the UserName and Password of your site to connect a below:

  1. #Site collection URL where we need to create the site columns  
  2. $siteurl = "" # Your site collection URL  
  3.  
  4. #User name and Passwords  
  5. $userName ="" # Your user name  
  6. $password ="" # Password   
Step 3: Get the Microsoft.SharePoint.Client.ClientContext instance and set the credentials as: 
  1. #client context object and setting the credentials   
  2. [Microsoft.SharePoint.Client.ClientContext]$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($siteurl)   
  3.  
  4. # convert password into secured string  
  5. $securedpw = ConvertTo-SecureString $password -AsPlainText -Force   
  6.   
  7. $clientContext.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $securedpw) 
Step 4: Get / Load the required web object, we will require it to get the fields collection to add our new fields,
  1. #get the respective web  
  2. $site = $clientContext.Site  
  3. $web = $site.RootWeb  
  4.  
  5. #Get all fields collection  
  6. $fields = $web.Fields   
  7.   
  8. $clientContext.Load($web)  
  9. $clientContext.Load($fields)   
  10.   
  11. $clientContext.ExecuteQuery()   
Step 5: Import the site columns .CSV file,
  1. # Import the site columns CSV file  
  2. $fieldslist = Import-Csv -path 'C:\CreateSiteColumns.csv' #give here your .csv file path   
Step 6: Loop through above fields list and create the site column,

 

  1. #Create the respective fields  
  2. ForEach($field in $fieldslist)  
  3. {  
  4.  
  5.     #Prepare the fieldxml  
  6.     for Site Column - Please be careful with single and double quotes,  
  7.         if those got disturbed then it take some time to fix: )  
  8.   
  9. $fieldXML = "<Field Name='" + $field.Name + "' Type='" + $field.Type + "' Description='" + $field.Description + "' DisplayName='" + $field.DisplayName + "' StaticName='" + $field.StaticName + "' Group='" + $field.Group + "' Hidden='" + $field.Hidden + "' Required='" + $row.Required + "' Sealed='" + $field.Sealed + "' ShowInDisplayForm='" + $field.ShowInDisplayForm + "' ShowInEditForm='" + $field.ShowInEditForm + "' ShowInNewForm='" + $field.ShowInNewForm + "'></Field>"  
  10.  
  11. #reate Site Column from fieldXML  
  12. $fields.AddFieldAsXml($fieldXML, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)  
  13.   
  14. $clientContext.Load($fields)  
  15.   
  16.   
  17. }  
  18. #foreach  
  19.   
  20. $clientContext.ExecuteQuery() 

 

Ebook Download
View all
Learn
View all