Add JSLink to ListViewWebPart

Vadym Gaievyi 21 Reputation points
2020-12-16T11:05:39.45+00:00

Hello!
I am developing farm solutions in Visual Studio 2019.
I have the list and the js-code with OnPreRender RegisterTemplateOverride. I am trying to add The JSLink to list's ListViewWebPart object, but unfortunatelly, this class doesn't have the JSLink property (unlike ListFormWebPart for example).
Please, help me to add the JSLink to my list's view in feature receiver.

Thanks!

SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,619 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Jerryzy 10,571 Reputation points
    2020-12-17T07:10:43.697+00:00

    Hi anonymous user,

    Please use SPView.JSLink property to set JSLink url to a list view, here is a sample code for your reference:

     using (SPSite site=new SPSite("http://sp/sites/dev"))  
                {  
                    SPWeb web = site.OpenWeb();  
                    SPList list = web.Lists["PBIList"];  
                    SPView view = list.Views["All Items"];  
                    view.JSLink = "~sitecollection/SiteAssets/jslink_Header1.js";  
                    view.Update();  
      
                }  
    

    Reference:

    SPView.JSLink Property


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Vadym Gaievyi 21 Reputation points
    2020-12-17T09:34:15.15+00:00

    Dear Jerryzy-MSFT,
    This approach does not work in SharePoint Server 2019.
    The similar issue exists with SPForm.JSLink

    This code does not work:

    SPWeb _curWeb = SPContext.Current.Web;  
    SPList _currList = _curWeb.Lists["MyList"];  
    SPForm editForm = _currList .Forms[PAGETYPE.PAGE_EDITFORM];  
    editForm.JSLink = "~site/SiteAssets/Scripts/Form.js";  
    
      
    

    https://video2.skills-academy.com/en-us/archive/msdn-magazine/2014/june/sharepoint-using-jslink-with-sharepoint-2013

    But there is another solution for forms:

    SPForm editForm = _currList.Forms[PAGETYPE.PAGE_EDITFORM];  
    var file = _currWeb.GetFile(editForm.Url);  
    file.CheckOut();  
    using (var manager = file.GetLimitedWebPartManager(PersonalizationScope.Shared))  
    {  
       var webPart = (Microsoft.SharePoint.WebPartPages.ListFormWebPart)manager.WebParts[0];  
       if (webPart != null)  
       {  
          webPart.JSLink = "~site/SiteAssets/Scripts/Form.js";  
          manager.SaveChanges(webPart);  
       }  
    }  
    file.CheckIn("Added JSLink to the Form");  
    

    I am looking for similar working solution for Views.

    Thanks

    0 comments No comments

  3. Jerryzy 10,571 Reputation points
    2020-12-18T04:02:37.417+00:00

    Hi anonymous user,

    If you are concern about ListViewWebPart class, yes, there is no JsLink for ListViewWebPart class.

    Per your requirement, you want to inject the JsLink url into a list view, I retested in SharePoint 2019 enviornment, it's working as expected for list view, please check the capture blow:

    49320-snipaste-2020-12-18-12-01-30.png

    49297-snipaste-2020-12-18-11-58-51.png

    I suggest you can try the code snippet in your side to see if JsLink injected into List View.


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.