10

Has anybody successfully set the JSLink for SP Online? I usually set it through Powershell on the view itself but that Command isn't available Online

http://technet.microsoft.com/en-us/library/fp161388(v=office.15).aspx

I don't want to set the JSLink on a List View Web Part properties, it doesn't work. I've had this same problem with on-prem SP.

1 Answer 1

8

Since SharePoint 2013 supports several sets of APIs, you could utilize client APIs (CSOM/REST) in PowerShell.

How to set View.JSlink property using PowerShell in SharePoint Online

CSOM version

The following example demonstrates how to specify View.JSlink property via CSOM in PowerShell:

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


Function Get-SPOCredentials([string]$UserName,[string]$Password)
{
   if([string]::IsNullOrEmpty($Password)) {
      $SecurePassword = Read-Host -Prompt "Enter the password" -AsSecureString 
   }
   else {
      $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
   }
   return New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
}

Function Configure-ListView([Microsoft.SharePoint.Client.ClientContext]$Context, [string]$ListTitle,[string]$ViewTitle)
{
    $list = $Context.Web.Lists.GetByTitle($ListTitle)
    $view = $list.Views.GetByTitle($ViewTitle)
    $view.JSLink = "~sitecollection/SiteAssets/Tasks.js"
    $view.Update()
    $Context.ExecuteQuery()
}

Usage

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


$Context = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl)
$Context.Credentials = Get-SPOCredentials -UserName $UserName -Password $Password
Configure-ListView -Context $Context -ListTitle "Tasks" -ViewTitle "All Tasks"
$Context.Dispose()

It is also possible to accomplish using REST API, since this API could be consumed in PowerShell, follow this article for a more details.

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.