I am trying to add a webpart on top of the home page of a site programmatically. It's working but adding it at bottom.
var page =context.Web.GetFileByServerRelativeUrl
("/sites/testdevsite/18/SitePages/Home.aspx");
var lwp = page.GetLimitedWebPartManager(PersonalizationScope.Shared);
string webpartxml = System.IO.File.ReadAllText
("C:\\Users\\shakm\\Downloads\\WebPart.webpart");
WebPartDefinition wpd = lwp.ImportWebPart(webpartxml);
lwp.AddWebPart(wpd.WebPart, "Header", 0);
context.ExecuteQuery();
I would check in SharePoint Designer if Home.aspx actually has WebPart zone named Header .
I've created Team site in SPS2016 and default Home page placed in SitePages only has one WebPart zone named not Header but only Bottom :
<WebPartPages:WebPartZone runat="server" ID="Bottom" CssClass="ms-hide" Title="loc:Bottom"><ZoneTemplate></ZoneTemplate></WebPartPages:WebPartZone>
WebParts and AppParts can exists not only in WebParts zones.
You can always add WebPart zone to your aspx page in any place inside:
<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.