SharePoint Pals
 | Sign In
How to Add a JS Link Reference to the Display Form or Any other ASPX Programmatically using CSOM PNP in SharePoint Office 365
by Sathish Nadarajan 21 Oct 2016
Author
SharePoint MVP
Visits   
Today  :   13     Total  :    1729

In the last article, we saw how to Insert the JS link on the default NewForm and EditForm.JS. With some curiosity, I was about to add the Same JS file to any page apart from the default PageType option shared by Patterns By Practices.

By default, PNP Supports the following Page Types.

Invalid = -1,

DefaultView = 0,

NormalView = 1,

DialogView = 2,

View = 3,

DisplayForm = 4,

DisplayFormDialog = 5,

EditForm = 6,

EditFormDialog = 7,

NewForm = 8,

NewFormDialog = 9,

SolutionForm = 10,

PAGE_MAXITEMS = 11

I wanted to extend this to some other Files also. i.e., Coming up with an Extension Method similar to the PNP, which will get any URL within that list and the JS Link.

For that came up with a static class.

 public static class JSLinkCustomizationUtility
     {
         public static void SetJSLinkCustomizationsCustom(this List list, string ServerRelativeUrl, string jslink)
         {
             // Get the list form to apply the JS link
             
             Microsoft.SharePoint.Client.File file = list.ParentWeb.GetFileByServerRelativeUrl(ServerRelativeUrl);
             LimitedWebPartManager wpm = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
             list.Context.Load(wpm.WebParts, wps => wps.Include(wp => wp.WebPart.Title));
             list.Context.ExecuteQueryRetry();
 
             // Set the JS link for all web parts
             foreach (WebPartDefinition wpd in wpm.WebParts)
             {
                 WebPart wp = wpd.WebPart;
                 wp.Properties["JSLink"] = jslink;
                 wpd.SaveWebPartChanges();
 
                 list.Context.ExecuteQueryRetry();
             }
         }
     }
 

And the Usage will be something like, below.

 static void Main(string[] args)
         {
             AddJSLinkReferencceInDefaultView();
         }
 
         public static void AddJSLinkReferencceInDefaultView()
         {
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
             string siteUrl = "https://******.sharepoint.com/sites/CommunitySite/";
             string userName = "Sathish@******.onmicrosoft.com";
             string password = "******";
 
             using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
             {
                 Web web = ctx.Web;
                 ctx.Load(web);
                 ctx.ExecuteQueryRetry();
 
                 List list = web.Lists.GetByTitle("MyList");
                 ctx.Load(list);
                 ctx.Load(list.DefaultView);
                 ctx.ExecuteQueryRetry();
 
                 list.SetJSLinkCustomizationsCustom(list.DefaultView.ServerRelativeUrl, "https:// ******.sharepoint.com/sites/CommunitySite/SiteAssets/JS/TempJS.js");
 
                 list.Update();
                 ctx.ExecuteQueryRetry();
             }
         }
 

Happy Coding,

Sathish Nadarajan.

blog comments powered by Disqus

SharePoint Pals

Pals
SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile

Training

Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community




SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to articles@sharepointpals.com in a word document.

Related Resources

Recent Tweets

Twitter November 21, 21:08
Implementing Multi-Threading with MaxDegreeOfParallelism in SharePoint using CSOM – An Insight - https://t.co/RNM7eVuyaa

Twitter November 21, 12:48
Configure Windows Firewall for SQL Server Analysis Service (SSAS) - https://t.co/re2H0cnGwn

Twitter November 21, 12:46
Implementing Multi-Threading in SharePoint To Upload Files using CSOM C# – An Insight - https://t.co/18QwUQG2eR

Twitter November 20, 21:48
How to Get the Client Context Using App Access token, by passing Client ID using CSOM in SharePoint Office 365 - https://t.co/wpmlJfTeWx

Twitter November 18, 21:30
How to Enable Auto Approval For Permission Requests in Community Site Template – Office 2013 using Client Side Object Model (CSOM) C#

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation