SharePoint Pals
 | Sign In
How to Add a JS Link Reference to the NewForm.aspx and EditForm.aspx Programmatically using CSOM PNP in SharePoint Office 365
by Sathish Nadarajan 20 Oct 2016
Author
SharePoint MVP
Visits   
Today  :   15     Total  :    1959

Recently I met with a requirement that on the default OOTB list, while creating or editing the list item, we need to do some custom action in SharePoint Office 365. To achieve that, we thought of writing a separate JS file and on that JS file, we can do our functionality.

Now, the challenge comes like, we cannot edit the NewForm.aspx and EditForm.aspx and insert a Script Editor Webpart in the Production Environment. They wanted a small utility (EXE) which will be inserting the JS file to the NewForm and EditForm.

For this, PNP helped us a lot and thought of sharing to the community.

To show a demo,

1. Create a List

2. In the Site Assets, upload a JS file.

3. In my case, I have a JS file with only an Alert Message.

4. Go to the New Item Page of the List.

clip_image002

5. If we search in the developer tool, no JS file will be found.

Now, execute the below code.

 namespace Console.Office365
 {
     using Microsoft.SharePoint.Client;
     using Newtonsoft.Json.Linq;
     using System;
     using System.Collections.Generic;
     using System.IO;
     using System.Linq;
 
     class Program
     {
         static void Main(string[] args)
         {
             AddJSLinkReferencce();
         }
 
         public static void AddJSLinkReferencce()
         {
             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.ExecuteQueryRetry();
 
                 list.SetJSLinkCustomizations(PageType.NewForm, "https://*********.sharepoint.com/sites/CommunitySite/SiteAssets/JS/TempJS.js");
 
                 list.Update();
                 ctx.ExecuteQueryRetry();
             }
         }
 
          
 
 
     }
 
 }
 

6. Go back and Refresh the NewForm, we will get the popup as below.

clip_image004

7. PageType is an Enum, which has the below options.

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

With the above forms, the method is straight forward.

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