4

I have a site column (Status) that I've added JSLink to, that runs my custom JavaScript. This works fine on lists. The JavaScript changes the text to a colored image.

I've then created a Visual Web Part that has some custom functionality, one of which is that I'm adding a XsltListViewWebPart from within the code behind.

 XsltListViewWebPart lvwp = new XsltListViewWebPart();
 lvwp.WebId = elevatedWeb.ID;
 lvwp.ListId = List.ID;
 lvwp.JSLink = "/_layouts/15/JS/jquery-2.1.1.min.js|/_layouts/15/JS/JSLinkFields.js";                        
 lvwp.ServerRender = false;
 lvwp.ClientRender = true;
 var view = list.GetUncustomizedViewByBaseViewId(1);
 lvwp.ViewId = int.Parse(view.BaseViewID);

 string organisationalFilter = "<Where><Eq><FieldRef Name=\"internalName\"/><Value Type=\"Text\">" + department + "</Value></Eq></Where>";
 lvwp.XmlDefinition = "<View BaseViewID='1'>" +
           "<Query>" + organisationalFilter + "</Query>" +
              "<ViewFields>" +
                  "<FieldRef Name=\"Status\" />" +
              "</ViewFields>" +
           "</View>";

  lvwp.BorderStyle = BorderStyle.None;
  lvwp.ChromeType = PartChromeType.None;

  ListViewWebPartPlaceHolder.Controls.Add(lvwp);

I've set a debugger; in javascript and when I load the list, the script works. However when I load the Web Part, nothing happens.

From searching the web, I've come across some tips but nothing has really worked. Such as adding lvwp.ServerRender = False;and adding the JSLink to the Webpart (lvwp.JSLink = "";)

Would be great to get to the bottom of this.

2 Answers 2

2

I suspect this behavior could be related with JavaScript template itself. It seems that when XLV web part is instantiated via Visual web part, custom template is not applied since clienttemplates.js (part of CSR library) library is not yet loaded.

In order to make sure the custom template is registered property, try to enclose it using SP.SOD.executeFunc function as shown below:

SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() {

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides({

      OnPreRender: function(ctx) {
          console.log('PreRender event is triggered');
      },


      OnPostRender: function(ctx) {
         console.log('PostRender event is triggered');
      }

    });

});
1

Not sure but this could be the problem:

lvwp.JSLink = "~site/_layouts/15/JS/jquery2.1.1.min.js|~site/_layouts/15/JS/JSLinkFields.js"; 

I think you should remove the ~site part from the URL:s, like this:

lvwp.JSLink = "/_layouts/15/JS/jquery2.1.1.min.js|/_layouts/15/JS/JSLinkFields.js";                       

Also make sure to set the ClientRender property to true on the webpart:

lvwp.ClientRender = true;
3
  • Thanks for the reply, I've tried that with no luck. Aswell as: •~sitecollection •~siteLayouts •~siteCollectionLayouts Commented May 7, 2015 at 13:49
  • 1
    I found another thing missing, check my revised answer Commented May 7, 2015 at 14:00
  • I'm currently looking a this post: but the cliendRender = true; did not do anything.sharepoint.stackexchange.com/questions/119266/… Commented May 7, 2015 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.