Monday, July 21, 2014

Add ListViewWebPart to wiki page with CSOM

Here is how you can add very easily a ListViewWebPart to a SharePoint wiki page. I've used this code in a provider hosted app for SharePoint online, but of course this will work on-premise as well.
In this example I'm using again the powerfull extensions methods of OfficeAMS.

In SharePoint 2013 list view web parts are still the same xsltlistviewwebparts that we are used to work with. The only difference in this new version is that we have the ability to overwrite the rendering on the client side by making use of JS Link.

A little bit of background information: a remote event receiver is binded to a list, OnItemAdded, I'm creating a new SharePoint site which I'm provisioning with CSOM. I need a document library, a page and a ListViewWebPart of the document library on the wiki page.


You'll have noticed that the WebPartXml property of the WebPartEntity contains a reference to my globals class. In this class, I've created an xml snippet which I can reuse if I want to add a xsltlistviewwebpart to a page. Using this snippet requires 2 params, the List.Id and a title for the web part. Feel free to adapt this snippet for your requirements (toolbar, JS Link reference...)

In a next post, I'll show you how to adapt the view of the webpart.

5 comments:

  1. did you refactor the methods from AMS as Extension methods? Because the methods I find are all non extension methods.

    ReplyDelete
    Replies
    1. I didn't refactor them, they are built as extension methods. Take a look here: https://github.com/OfficeDev/PnP/tree/dev/OfficeDevPnP.Core/OfficeDevPnP.Core/AppModelExtensions

      Delete
  2. never mind, was searching for the code/dll in the samples folder. Found the extension methods you mean! Gonna try now if this works the way we want! Fingers crossed

    ReplyDelete
  3. Have you ever had the problem of the WebPartIndex property not taking effect?

    ReplyDelete