Add column to SharePoint Online 2013 list via Powershell v3

2.7k views Asked by At

Can anybody tell me how to do this? None of the examples I have found seem to work.

My site is https://blah.sharepoint.com/technology and my list is called 'pfa'. I want to add some text columns.

I DO have connectivity with SharePoint Online via my Powershell, as I have managed to get some results back from various commands.

Thanks.

2

There are 2 answers

0
Vadim Gremyachev On

How to provision field in SharePoint Online via CSOM in PowerShell

CSOM API comes with:

to add a list or site column.

The example below demonstrates how to add GeoLocation field into Contacts List:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")

function Provision-Field([Microsoft.SharePoint.Client.ClientContext]$Context,[string]$ListTitle,[string]$FieldSchema)
{
   $list = $Context.Web.Lists.GetByTitle($ListTitle)
   $List.Fields.AddFieldAsXml($FieldSchema,$true,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)
   $Context.Load($List)
   $Context.ExecuteQuery()
}



$UserName = "[email protected]"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
$URL = "https://contoso.sharepoint.com/"

$Context = New-Object Microsoft.SharePoint.Client.ClientContext($URL)
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$Password)
$Context.Credentials = $Credentials

Provision-Field $Context "Contacts" "<Field Type='Geolocation' DisplayName='Location'/>"

How to provision field in SharePoint Online via REST in PowerShell

Endpoints:

http://<site url>/_api/web/fields('<field id>')

http://<site url>/_api/web/lists(guid'<list id>')/fields('<field id>')

The article Consuming the SharePoint 2013 REST API from PowerShell describes how send HTTPS requests to SharePoint REST web services.

Using the Invoke-RestSPO function from the article, the following example demonstrates how to add Note field to List using REST API in PowerShell:

Function Add-SPOField(){

Param(
[Parameter(Mandatory=$True)]
[String]$WebUrl,

[Parameter(Mandatory=$True)]
[String]$UserName,

[Parameter(Mandatory=$False)]
[String]$Password,

[Parameter(Mandatory=$True)]
[String]$ListTitle,

[Parameter(Mandatory=$True)]
[String]$FieldTitle,

[Parameter(Mandatory=$True)]
[System.Int32]$FieldType
)


    $fieldMetadata = @{ 
      __metadata =  @{'type' = 'SP.Field' }; 
      Title = $FieldTitle;
      FieldTypeKind = $FieldType;
    } | ConvertTo-Json


   $Url = $WebUrl + "_api/web/Lists/GetByTitle('" + $ListTitle +  "')/fields"

   $contextInfo = Get-SPOContextInfo $WebUrl $UserName $Password
   Invoke-RestSPO $Url Post $UserName $Password $fieldMetadata $contextInfo.GetContextWebInformation.FormDigestValue
}




. ".\Invoke-RestSPO.ps1"   #InInvoke-RestSPO function

$UserName = "[email protected]"
$Password = Read-Host -Prompt "Please enter your password" 
$WebUrl = "https://contoso.sharepoint.com/"


Add-SPOField -WebUrl $WebUrl -UserName $UserName -Password $Password -ListTitle "Documents" -FieldTitle "Comments" -FieldType 3

References

0
AshFlaw On

Here is someone who created a list with the CSOM through PowerShell with SharePoint Oneline http://www.hartsteve.com/2013/06/sharepoint-online-powershell/

The PowerShell stuff for SharePoint online is limited to some basic admin tasks only. When you used the CSOM in PowerShell, you can do a lot more.