I'd like to add custom Web Part to page without Web Part Zone programmatically, however, for some reason, I simply don't know how to do it.
I created blank new Page (Body Only)
PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(web);
string pageName = "NewPage.aspx";
PageLayout[] pageLayouts = pWeb.GetAvailablePageLayouts();
PageLayout newPage = pageLayouts[0]; //Body only type of page layout
PublishingPageCollection pages = pWeb.GetPublishingPages();
PublishingPage nPage = pages.Add(pageName, newPage);
Page is Created, Checked in and Published. This part works as charm.
However, when I try to add custom Web Part to it, code does nothing
SPFile file = web.GetFile(web.Url + "/Pages/NewPage.aspx");
file.CheckOut();
string err = string.Empty;
string wPartURL = web.Url + "/_catalogs/wp/MyWebPart.webpart";
SPLimitedWebPartManager manager = web.GetLimitedWebPartManager(web.Url + "/Pages/NewPage.aspx", PersonalizationScope.Shared);
XmlTextReader reader = new XmlTextReader(new StringReader(web.GetFileAsString(wPartURL)));
System.Web.UI.WebControls.WebParts.WebPart wp = (System.Web.UI.WebControls.WebParts.WebPart)manager.ImportWebPart(reader, out err);
wp.Title = "Web Part Title";
manager.AddWebPart(wp, "Page Content", 0);
manager.SaveChanges(wp);
file.Update();
web.Update();
manager.Dispose();
file.CheckIn("Checked in by Code");
In page's source, I can't find any "Zone" and I was trying to pass to AddWebPart method several different value (like "Page Content", "PageContent", "Central", and so on). Nothing worked, and not even once was thrown exception. I'm taking wild guess here, but I feel like I need to update something which I don't in upper code (and for this reason no error is ever risen). The above code is written as Console Application with no try / catch blocks.
EDIT: With @beavel help, I managed to get zoneID (Page Content), however, Web Part still isn't added to the page.
EDIT2: I found out that problem is - Body Only page layout doesn't have Web Part Zone and because of it SPLimitedWebPartManager fails to add Web Part. Since First of January 2014. the question was edited from "Add programmatically custom Web Part to page" to "Add programmatically custom Web Part to page without Web Part Zone"