Allow Export of List Views (XsltListViewWebPart) in SharePoint Online / SP2016

Disclaimer – This does require a single manual step where you need to add a GUID to the exported file.

Wow .. its been a while since I blogged last but finally have something SharePoint(y) to post about 🙂

This time I was asked by a client if it was possible add a “List View” (XsltListViewWebPart) to a different site than the one where the list exists.

Now, I know that this is possible because I’ve done it on older projects, simply by exporting the List View, adding the “Web ID” and re-importing it to another page. This works as long as the page you are importing it into are in the same site collection.

The problem of course is that the List View web parts in SharePoint Online (and SP2016) currently have the “Allow Export” property set to false .. AND you can’t change this through the UI.

Luckily, this property is access via a DOM attribute which is extremely easy to modify. The following JavaScript identifies all “Web Parts” (in edit mode) which have an “allow export” attribute set, and then sets it to “true”.

var webParts = document.querySelectorAll(“div[webpartid][allowexport]”);

 

for(i=0; i<webParts.length; i++) {

 

webParts[i].setAttribute(“allowexport”, “true”);

 

}

You can of course add this to the page any way you see fit (either a Script Editor Web Part, Master Page, Custom Action, SPFx Application Customiser .. depending on the type of set and purpose for this script).

So .. this enables the “Export” menu on the Web Part Properties. Once you have exported the Web Part definition if you open it you will find that the WebId property will be blank.

<property name=”WebId” type=”System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089″>00000000-0000-0000-0000-000000000000</property>

This will need updating with the “Web Id” property of the web where the list resides. You can easily retrieve this using the REST API:

https://tenant.sharepoint.com/sites/SiteN/subsite/_api/web?$select=id

Just copy-paste that into the Web Part definition (NotePad will do the trick) and you can now upload that Web Part definition into ANY page anywhere in the Site Collection.