I'm adding XsltListViewWebPart programmatically like so:

Microsoft.SharePoint.WebPartPages.XsltListViewWebPart listViewWebpart = (Microsoft.SharePoint.WebPartPages.XsltListViewWebPart)webPart;
listViewWebpart.InplaceSearchEnabled = true;
listViewWebpart.ClientRender = true;
listViewWebpart.IsClientRender = false;
listViewWebpart.ServerRender = false;
listViewWebpart.PartImageLarge = "/_layouts/15/images/itdl.png?rev=23";
listViewWebpart.CatalogIconImageUrl = "/_layouts/15/images/itdl.png?rev=23";
SPList list = manager.Web.GetList(manager.ServerRelativeUrl);
listViewWebpart.GhostedXslLink = "main.xsl";
listViewWebpart.ListId = list.ID;
manager.AddWebPart(listViewWebpart, this.ZoneID, this.ZoneIndex);
listViewWebpart.View.JSLink = "clienttemplates.js";
listViewWebpart.View.XslLink = "main.xsl";
listViewWebpart.View.Toolbar = "Standard";

But, whatever I've tried it ends up looking like the following: Example

I was under impression that JSLink or XslLink or ClientRender might help me, but nothing really does. I've also compared the differences between resulting markup for the 'ugly' web part and a 'good-looking' one in SharePoint Designer and found no obvious clues as to what might be wrong.

Help would be really appreciated

  • Adding web part = SP2010 way. In SP2013 those controls are app parts, that is something totally different. See here to add a clientwebpart programaticallY: sharepoint.stackexchange.com/questions/91435/… – Boland Oct 28 '14 at 0:32
  • 3
    The XsltListViewWebPart is NOT an App Part, it is a good old server-side built-in web part. An App Part is something completely different. – Louis Oct 28 '14 at 4:39

Most probably it occurs since list view still uses XSLT rendering mode when web part is added via SharePoint SSOM.

Use ViewId and XmlDefinition properties to specify explicitly which list view is used for rendering.

Below is provided the complete example for adding XsltListViewWebPart web part in SharePoint 2013:

private static void AddListViewWebPart(SPLimitedWebPartManager wpm,string listTitle,string zoneId,int zoneIndex)
   var list = wpm.Web.Lists.TryGetList(listTitle);
   var view = list.GetUncustomizedViewByBaseViewId(1);
   var wp = new XsltListViewWebPart();
   wp.InplaceSearchEnabled = true;
   wp.ClientRender = true;
   wp.ListId = list.ID;
   wp.ViewId = int.Parse(view.BaseViewID);
   wp.XmlDefinition = view.GetViewXml();
   wpm.AddWebPart(wp, zoneId, zoneIndex);

Note: For example, for Pages library, when ViewId and XmlDefinition properties are omitted, list view (BaseViewId = 0) is rendered which in turn used XSLT as a rendering mode.

Key Points:

  • Set ClientRender property to true in order to render list view using CSR rendering mode
  • Specify ViewId and XmlDefinition properties to render the proper list view


using (var site = new SPSite(siteUrl))
    using (var web = site.OpenWeb())
        var pageFile = web.GetFile(pageUrl);
        var wpm = pageFile.GetLimitedWebPartManager(PersonalizationScope.Shared);
        AddListViewWebPart(wpm, "Pages", "Main", 1);

enter image description here

| improve this answer | |
  • 1
    Vadim, thank you so much! It works perfectly. I've virtually spent hours and hours pulling my hair over this problem. I hope it's going to be a very useful answer for others too. – Denis Molodtsov Oct 28 '14 at 19:52
  • Vadim , Currently m Using Your Code ,but Facing Error:"The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again." – Akshay Dattatray Nangare Apr 23 '15 at 6:16
  • This code should be run in feature receiver to setup page webparts during activation – gdbdable Feb 5 '16 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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