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 ?

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 user@tenantname.onmicrosoft.com 
$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

  • 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 Gu Jul 28 '17 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. – Gautam Sheth Jul 28 '17 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 Gu Jul 28 '17 at 11:44
  • 1
    my bad, have updated the link for CSOM. – Gautam Sheth Jul 28 '17 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 ? – Gautam Sheth Jul 28 '17 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.

  • from where i can run the Pnp-Powershell script? – john Gu Jul 28 '17 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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