Hi All,
I am attempting to add a web part to a wiki page (SitePages/Home.aspx in this case), using the code below.
private void addWPToPage(ClientContext cc, Web web)
{
try
{
Microsoft.SharePoint.Client.File file = web.GetFileByServerRelativeUrl(web.ServerRelativeUrl + "/SitePages/Home.aspx");
cc.Load(file);
cc.ExecuteQuery();
LimitedWebPartManager wpm = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
cc.Load(wpm.WebParts, wps => wps.Include(wpp => wpp.WebPart.Title));
cc.ExecuteQuery();
List targetList = web.Lists.GetByTitle("General Documents");
cc.Load(targetList);
cc.ExecuteQuery();
string webPartXMLTemplate = "<webParts>" +"<webPart xmlns='http://schemas.microsoft.com/WebPart/v3'>" +"<metaData>" +"<type name='Microsoft.SharePoint.WebPartPages.XsltListViewWebPart, Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' />" +"<importErrorMessage>Cannot import this Web Part.</importErrorMessage>" +"</metaData>" +"<data>" +"<properties>" +"<property name='ShowWithSampleData' type='bool'>False</property>" +"<property name='Default' type='string' />" +"<property name='NoDefaultStyle' type='string' null='true' />" +"<property name='CacheXslStorage' type='bool'>True</property>" +"<property name='ViewContentTypeId' type='string' />" +"<property name='XmlDefinitionLink' type='string' />" +"<property name='ManualRefresh' type='bool'>False</property>" +"<property name='ListUrl' type='string' />" +"<property name='ListId' type='System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'>" + targetList.Id.ToString() + "</property>" +"<property name='TitleUrl' type='string'></property>" +"<property name='EnableOriginalValue' type='bool'>False</property>" +"<property name='Direction' type='direction'>NotSet</property>" +"<property name='ServerRender' type='bool'>False</property>" +"<property name='ViewFlags' type='Microsoft.SharePoint.SPViewFlags, Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'>Html, TabularView, Hidden, Mobile</property>" +"<property name='AllowConnect' type='bool'>True</property>" +"<property name='ListName' type='string'>{" + targetList.Id.ToString() + "}</property>" +"<property name='ListDisplayName' type='string' />" +"<property name='Title' type='string'>General Documents</property>" +"</properties>" +"</data>" +"</webPart>" +"</webParts>";
WebPartDefinition newWebPart = wpm.ImportWebPart(webPartXMLTemplate);
var wp = wpm.AddWebPart(newWebPart.WebPart, "wpz", 0);
cc.ExecuteQuery();
}
catch (Exception ex)
{
}
}The web part is added to the page successfully, however it is not visible on the page. If I change the zoneId from 'wpz' to 'Bottom', the web part is added and is visible on the page, but not in the area I want it to be and it has limitations.
The same question has previously been asked at the link below without a resolution
http://stackoverflow.com/questions/27268492/how-to-add-a-web-part-into-a-sitepages-home-aspx-using-csom
Is this possible and if so, how can it be achieved?
Thanks
David