Rhonin
Rhonin

Reputation: 494

Sharepoint Online Change WebPart View Query using C# / CSOM

I have try to change default view query using C# but it's not working. I can change JSLink property, but not XmlDefinition. Any idea to workaround or what I'm doing wrong?

var webPart = listWebPart.WebPart.
clientContext.Load(webPart.Properties);
clientContext.ExecuteQuery();    
webPart.Properties["XmlDefinition"] = newQuery;
listWebPart.SaveWebPartChanges();
clientContext.Load(listWebPart);
clientContext.ExecuteQuery();

It's list webpart on some page.

Upvotes: 0

Views: 1374

Answers (1)

groveale
groveale

Reputation: 477

You can get the LimitedWebPartManager for the page with the GetLimitedWebPartManager function on SharePoint Online as well, MSDN. You can then load the WebParts using the LimitedWebPartManager as below:

var page = ctx.Web.GetFileByServerRelativeUrl(pageUrl);
LimitedWebPartManager wpMgr = page.GetLimitedWebPartManager(Microsoft.SharePoint.Client.WebParts.PersonalizationScope.Shared);
ctx.Load(wpMgr.WebParts);
ctx.ExecuteQuery();

This will load all the webparts on the page. You can then get the WebPartDefinition of the webpart you require using it's index:

WebPartDefinition webPartDef = wpMgr.WebParts[webpartIndex];
ctx.Load(webPartDef);
ctx.ExecuteQuery();

You can now finally update the desired property and save the definition:

webPartDef.WebPart.Properties["XmlDefinition"] = newQuery;
webPartDef.SaveWebPartChanges();
webPartDef.CloseWebPart();
ctx.ExecuteQuery();

Hopefully this will help you update your webpart properties

Upvotes: 1

Related Questions