SharePoint Online – Changing the ViewFields, CAML query of a list view webpart using CSOM. ( C# and PowerShell)

Usually, we use some provisioning method, usually using PnP Provisioning tool or a custom product, to provision a large number of sites. More often than not, these sites contain pages or site pages. Now some of these pages contain webparts which are quite easy to modify like SEWP/CEWP, search webparts, content query webparts etc. But many times, they also contain List view webparts(LVWP).

Sometimes, after a large number of sites have been provisioned, we are asked to modify some CAML query or View Fields or Row limit or something or the other in these LVWPs. To modify those values, its a bit tricky and not straightforward.

So, to fix this issue, I have written a PS script as well as CSOM C# managed code to overcome this issue.

Once you run the code, you will be able to change the view settings programatically(see below picture).

qqg1y

To run the code, you need to download and install the CSOM SDK and SharePoint Online PowerShell. If they are already installed, you can skip that and scroll below directly to the code. If not, then the download links are mentioned below:

SharePoint Online Client Components SDK

SharePoint Online Management Shell

Some background info:

By default when you add a list view webpart, a local instance of the view is created. This view is hidden from the UI and is not visible via list settings as well. However, each such view has a GUID.

Now, that GUID which is equal to the webpart definition’s ID. Each webpart that is present on the page also has a GUID.

Using this GUID, you can get the local instance of the listView and modify its ViewQuery,Viewfields and other such view properties.

This is possible using both PowerShell as well as C# console application using CSOM.

So, let’s get right to the code. Please modify it as per your SP list name, page name and webpart title.

If you want to do it programatically using PowerShell, you can use the below code:

To use CSOM C# code, you can use it as below:

Advertisements