0

I am working on a site collection inside sharepoint online office 365. and i want to create a site column with the following properties:-

  1. show the site column in the display form. while hide it inside the create and edit forms.

  2. link the site column with a jslink file.

now when i work on sharepoint on-premises, i do the first point by writing the following power shell script:-

$site = Get-SPSite -Identity "http://********/"
$web = $site.RootWeb

#Assign fieldXML variable with XML string for site column
$fieldXML = '<Field Type="Text"
Name="ItemNumber"
Description="auto generated item number.."
DisplayName="Item Number"
StaticName="ItemNumber"
Group="Custom Columns"
Hidden="FALSE"
Required="FALSE"
Sealed="FALSE"
ShowInDisplayForm="TRUE"
ShowInEditForm="FALSE"
ShowInListSettings="TRUE"
ShowInNewForm="FALSE"></Field>'

#Output XML to console
write-host $fieldXML

#Create site column from XML string
$web.Fields.AddFieldAsXml($fieldXML)

and for the second point by writing the following powershell script:-

$web = Get-SPWeb http://*****/
$field = $web.Fields["Item Number"]
$field.JSLink = "~siteCollection/Style Library/JS/HideItemNumberInQuickEdit.js"
$field.update($true)

but now inside office 365 i can not run powershell scripts. so can anyone adivce how i can achieve the above 2 points inside office 365 sharepoint online? can i run for example remote power-shell scripts? or can i use site apis to create site columns? or any other appraoch ?

2 Answers 2

1

Yes , its possible using PowerShell.

For that you need to download and install CSOM SDK and SharePoint Online PowerShell.

Once done, you can use the entire CSOM code inside powershell with few syntax changes.

So, your code to add field as xml would be as below:

Add-Type -Path (Resolve-Path "$env:Common\Program Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll")
Add-Type -Path (Resolve-Path "$env:Common\Program Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll")

#Site collection URL
$siteurl = "https://tenant-sitecollection-url"

# set the user name and password(ensure that user has sufficient rights to perform op)
# if the tenant has 2 factor auth enabled, use the app password
$userName ="username" #usually something like [email protected] 
$password ="password" 

#client context object and setting the credentials   
[Microsoft.SharePoint.Client.ClientContext]$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($siteurl)   

# convert password into secure string  
$securedpassword = ConvertTo-SecureString $password -AsPlainText -Force   

$clientContext.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $securedpassword) 

#get the rootweb
$site = $clientContext.Site  
$web = $site.RootWeb  

#Get fields collection  
$fields = $web.Fields   

$clientContext.Load($web)  
$clientContext.Load($fields)   

$clientContext.ExecuteQuery()   

#your field xml
$fieldXML = "<your-xml>"
$fields.AddFieldAsXml($fieldXML, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::AddToDefaultContentType)    
$clientContext.Load($fields)  
$clientContext.ExecuteQuery() 

And to add jslink to a field, it would be somewhat as below:

# field internal name
$fieldTitle = "ColumnName"
$customfield = $clientContext.Site.RootWeb.Fields.GetByInternalNameOrTitle($fieldTitle) 
$customfield.JSLink = "<your-jslink-url>"
$customfield.Update()

# $customfield.UpdateAndPushChanges($true)

$clientContext.ExecuteQuery()

Download links -

Ensure that you reference the correct path in the code

CSOM SDK - SharePoint Online Client Components SDK

PowerShell - SPO powershell

7
  • so i download these libraries inside my development PC? is this correct ? also inside your code sample you are referencing these paths "$env:Common\Program Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" so where are these dlls?? inside my dev PC or inside office 365 tenant??
    – John John
    Jul 28, 2017 at 10:23
  • 1
    Yes download and install the SDK and powershell. It will add the CSOM dlls in the program files folder on your local machine. Jul 28, 2017 at 10:31
  • now i have office 365,,, so does installing sharepoint 2013 client components SDK will work for it? or i need 2016 or the SDK for office 365???
    – John John
    Jul 28, 2017 at 11:44
  • 1
    my bad, have updated the link for CSOM. Jul 28, 2017 at 11:54
  • 1
    It should be at the this link - C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI usually , can you check ? Jul 28, 2017 at 12:35
1

Yes. These two operations can be done using PowerShell for SharePoint Online. But, the same snippet cannot be used.

Please check SharePoint PnP PowerShell (https://github.com/SharePoint/PnP-PowerShell), which provides many cmdlets to easily manage SharePoint site collections.

You can see the list of cmdlets here - https://github.com/SharePoint/PnP-PowerShell/blob/master/Documentation/readme.md

There are cmdlets which can create Site Columns and change the field settings for a list.

1
  • from where i can run the Pnp-Powershell script?
    – John John
    Jul 28, 2017 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.