SharePoint online - how to include jslink by default for all lists?

840 Views Asked by At

Has anyone found a way to automatically add a jslink URL to lists by default instead of having to add the URL to every list manually when they are created? Ultimately we'd like to have default jslink for each type of app the editors have access to.

2

There are 2 best solutions below

0
On

Another solution is to write your own code to update all of your pages. On this page Tobias Zimmergren shows a code updating JSLink with PowerShell: PowerShell: Configure the JSLink property of a Web Part

I personally use C# CSOM to update JSLink on all requested pages. Here is the sample code which updates JSLink on one page (for simplicity I have stripped out exception handling and all non-happy-path logic):

using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.WebParts;

void UpdateWebPart(ClientContext webpartContext, string RelativeUrl, string JSLink)
{
    File page = webpartContext.Web.GetFileByServerRelativeUrl(RelativeUrl);
    LimitedWebPartManager wpm = page.GetLimitedWebPartManager(PersonalizationScope.Shared);
    webpartContext.Load(page);
    webpartContext.Load(wpm.WebParts, wps => wps.Include(w => w.WebPart.Title, w => w.WebPart.Properties));
    webpartContext.ExecuteQuery();
    if (wpm.WebParts.Count > 0)
    {
        // You can find your WebPart inside wpm.WebParts e.g. by Title. On each page I have only 1 WebPart, so I just take the first.
        WebPartDefinition wpd = wpm.WebParts[0];
        WebPart myWP = wpd.WebPart;
        if ((string)myWP.Properties["JSLink"] != JSLink)
        {
            myWP.Properties["JSLink"] = JSLink;
            wpd.SaveWebPartChanges();
            webpartContext.ExecuteQuery();
        }
    }
}
  • webpartContext is SharePoint ClientContext created earlier
  • RelativeUrl is something like: "/Lists/Sample%20Tasks/AllItems.aspx"
  • JSLink is new value for JSLink of WebPart, which you want to update
0
On

yes, this was described perfectly by Chris O'Brien here: http://www.sharepointnutsandbolts.com/2013/01/using-jslink-to-change-ui-of-sharepoint_20.html

short story - you should create list template and define jslink there, so all lists based on this template would have the js link. If you don't want to assosiate lists with templates by any reason, you should look into event receivers. (for List created event.)