I am trying to get the ZoneId
of the webparts on a page, but when debugging the code that property contains this error:
‘webpartDefinition.ZoneId’ threw an exception of type ‘Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException’
Here is my code
public void TestGetWebPart() { using (_clientContext = new ClientContext(Url)) { _clientContext.Credentials = _credentials; var web = _clientContext.Web; var file = web.GetFileByServerRelativeUrl("/subsite/SitePages/Home.aspx"); var limitedWebPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared); var webPartDefinitions = limitedWebPartManager.WebParts; _clientContext.Load(webPartDefinitions); _clientContext.Load(limitedWebPartManager); _clientContext.ExecuteQuery(); foreach (var webpartDefinition in limitedWebPartManager.WebParts) { _clientContext.Load(webpartDefinition); _clientContext.Load(webpartDefinition.WebPart); _clientContext.ExecuteQuery(); Console.WriteLine($ "{webpartDefinition.WebPart.ZoneIndex} - "); } var webPartDefinition = limitedWebPartManager.ImportWebPart(_scriptEditorXml); limitedWebPartManager.AddWebPart(webPartDefinition.WebPart, "", 0); } }
I tried “loading” the property with
_clientContext.Load(webpartDefinition, w => w.ZoneId);
but it just says
‘WebPartDefinition’ does not contain a definition for ‘ZoneId’ and no extension method ‘ZoneId’ accepting a first argument of type ‘WebPartDefinition’ could be found (are you missing a using directive or an assembly reference?)
Even though I can see it in the debugging window:
How is this even possible?