Post

Create new site columns in SharePoint Online with PowerShell
This is part 4 of 10 where we will be creating a couple of site columns in SharePoint Online with PowerShell. This is part of the following series: We will be collecting all available web templates in part 1 so we can use this to create a new site in part 2. In part 3 […]

This is part 4 of 10 where we will be creating a couple of site columns in SharePoint Online with PowerShell. This is part of the following series:

We will be collecting all available web templates in part 1 so we can use this to create a new site in part 2. In part 3 we will be creating a web for the newly created site. We then want to create a couple of site columns in part 4 which we will combine to a content type in part 5. This content type will be added (part 8) to our newly created document library in part 7 using a list template from part 6. After everything is set we will be setting the view in part 9 for this list to show the added columns we got from adding the content type. We only want to set permissions for myself so I’ll will be breaking the inheritance and setting permissions in part 10.

Create new site columns in SharePoint Online

This script will create site columns using a .CSV file. This script will be using CSOM to create new site columns in SharePoint Online. We will first create a .CSV file and you can save this to any location. In this post I’ll be using My Documents.

image

We are going to need the Display Name, Name, ID, Group, required and Field Type for each column. You can find more properties for a site column at https://msdn.microsoft.com/en-us/library/office/aa979575.aspx but I’ve only tried the 6 properties we are going to use now. So I’ve created a .CSV file to create 3 columns named Blog Text, Blog Number and Blog User. The .CSV file should look like this:

DisplayName,Name,ID,Group,Required,FieldType
Blog Text,BlogText,{88b4f892-1f2d-4649-90be-b3be781b831d},My Blog Columns,TRUE,Text
Blog Number,BlogNumber,{f0d8c245-df1e-4a2a-af55-2b7fed856ee4},My Blog Columns,TRUE,Number
Blog User,BlogUser,{842ac3d5-d33d-4f3e-ba7e-d85950679db1},My Blog Columns,FALSE,User

We can now start by opening the SharePoint Online Management Shell as administrator which can be downloaded at https://www.microsoft.com/en-us/download/details.aspx?id=35588.

image

You will need to change the first variables to match your Office 365 tenant and copy this bit to PowerShell.

function new-SPOnlineSiteColumns {
    # variables that needs to be set before starting the script
    $siteURL = "https://spfire.sharepoint.com/sites/BlogDemo"
    $CSVLocation = "C:\Users\mpeeters\Documents\temp\siteColumns.csv"
    $userName = "mpadmin@spfire.onmicrosoft.com"
    
    # Let the user fill in their password in the PowerShell window
    $password = Read-Host "Please enter the password for $($userName)" -AsSecureString
    
    # set SharePoint Online credentials
    $SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $password)
        
    # Creating client context object
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
    $context.credentials = $SPOCredentials
    $fields = $context.web.fields
    $context.load($fields)
    
    #send the request containing all operations to the server
    try{
        $context.executeQuery()
        write-host "info: Loaded all Site Columns" -foregroundcolor green
    }
    catch{
        write-host "info: $($_.Exception.Message)" -foregroundcolor red
    }
    
    #load CSV file
    $siteColumns = import-csv $CSVLocation
    
    #loop through each entry and create the columnGroup
    foreach ($column in $sitecolumns){
        #check if column already exists
        foreach($field in $fields){
            if ($field.internalname -eq $column.name){
                $columnExists = 1
            }
            else{
                $columnExists = 0
            }        
        }
        
        if ($columnExists -eq 0){
            #create XML entry for a new field 
            $fieldAsXML = "<Field Type='$($column.FieldType)' 
            DisplayName='$($column.DisplayName)' 
            Name='$($column.name)' 
            ID='$($column.ID)' 
            Group='$($column.group)'
            Required='$($column.required)' />"
            
            #see tips below for info about fieldOptions
            $fieldOption = [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint
            $field = $fields.AddFieldAsXML($fieldAsXML, $true, $fieldOption)
            $context.load($field)
            
            #send the request containing all operations to the server
            try{
                $context.executeQuery()
                write-host "info: column $($column.name) created" -foregroundcolor green
            }
            catch{
                write-host "info: $($_.Exception.Message)" -foregroundcolor red
            }
        }
        else{
        write-host "Info: The column $($column.name) already exists." -foregroundcolor red
        }
    }
}
new-SPOnlineSiteColumns

image

You will be asked to enter the password and press enter

image

You can verify if the columns have been created successfully by going to site settings and then site columns.

image

Tips

You can find more information about fieldOptions at https://msdn.microsoft.com/EN-US/library/office/microsoft.sharepoint.client.addfieldoptions.aspx

The Field Types are case-sensitive so make sure you type then exactly as documented in https://msdn.microsoft.com/en-us/library/office/aa979575.aspx

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Archive