I've exported a XsltListViewWebPart with all of my settings to a .webpart file. I have added this to the webpart gallery in several site collections, I can edit a page add the webpart and it somehow finds the relative list and renders fine with the embedded view XML defined in the XmlDefinition property and renders with my custom view which has special filter and sorting.

My goal though is to automate this process, because I have to update 50+ site collections, so I made an attempt to do this in code.

SharePointHelper.FileWebPartToPublishingPage(rootWeb, newPage, relUrl + "/_catalogs/wp/NewsHistory.webpart", "xc16729f684e3144f0b668b0489721c566", 1, "News");
public static void AddFileWebPartToPublishingPage(SPWeb rootWeb, PublishingPage page, string webPartUrl, string zoneId, int zoneIndex, string title)
    SPLimitedWebPartManager partManager = page.ListItem.File.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
    XmlTextReader reader = new XmlTextReader(new StringReader(rootWeb.GetFileAsString(webPartUrl)));
    string errMsg = null;
    System.Web.UI.WebControls.WebParts.WebPart webpartInstance = partManager.ImportWebPart(reader, out errMsg);
    webpartInstance.Title = title;
    partManager.AddWebPart(webpartInstance, zoneId, zoneIndex);

This code adds the web-part fine but the odd thing is that it doesn't render with the embedded view but reverts to using the default AllItems view on the list. I've also tried updating the listId and ListName to the list Guid before adding the webpart with no success.

My Question is how can I script out XsltListViewWebPart webpart provisioning so that it will work the same as doing it manually.

Unfortunately, of all of the dataview controls that MS provides OOTB this is the only one that provides proper paging, yet it seems to be the most obnoxious control to deploy.


Your Answer

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