When creating pages in Microsoft SharePoint, it is quite common to use these pages to display web parts. Web parts can be used to display or edit information in many ways. Web parts such as announcements, image viewers and content editors are normally used to display information on pages but we can also display information from lists to bring through to web parts on our pages. Consider a situation though where you have possibly over a hundred pages in which it will display the same information from the same list on each page. It would be quite time consuming to edit each page, add the web part and set the web part to a specific view on the list to pull through the correct information. In this case, writing a little bit of code to solve this would save you a lot of time. I am going to show you how to add a list view web part to a page through code.First of all you will need to get the page which you are going to add your web parts to. This is done by using the SPLimitedWebPartManager for the current web, passing in the URL to the page and the personalization scope as parameters shown below:

Next thing is to get the list in which you will be using for the list view web parts. In this example I am getting a ‘Drawings’ list using the code below:

We then need to create a new instance of the XsltListViewWebPart which will be used to store the details for our list view web part which we will be adding to the page:

Next we can set our view for the web part using a view from our list. For example I have created a view for my Drawings list called ‘Last 7 Days’ which shows me items which have been created in the last seven days. This can be done with the code below:

You can now set up the rest of the properties for you web part. If you are updating views like in my case where I am setting the Tabular View to false then you will need to call view.update(). Other properties which you should set similar to the code below are the ListName, ViewGuid and XmlDefinition as these are the general syntax which SharePoint understands when it looks for these properties. Other properties such as Title and Description you can specify what you want. Below is the completed code for setting up your list view web part:

The next step is to add the web part to your page. This is done by calling the method ‘AddWebPart’ for your web part manager. The parameters are the following:1. list view web part (which we set up above)

2. The Zone Id – This should be defined in your .aspx page. For example I have a WebPartZone with an ID called ‘DrawingsLastSevenDays’. This is where your web part is to be added.

3. The Zone Index – If you have a zone with space for multiple web parts then you can specify the order in which it sits. For example ‘0’ will be the first index (first in order). See below:

The final step is to save your changes to the web part manager passing in the list view web part. This will save the changes to your page with your included list view web part:

That is the basic steps for setting up a list view web part for a page. As you saw I only set up some basic properties for the list view web part but many more can be applied if necessary to suit your needs. Hopefully this should aid in the time consuming process of setting up list view web parts for pages with some code to automate the process.

If you need more help you can reach out to our teams in Aberdeen and Edinburgh.


Get Up To Speed With SharePoint 2019!

Download Our FREE eBook...


Download SharePoint 2019 eBook