Add Publishing Pages to specfic subsites programmatically in FeatureReceiver
-
10-12-2019 - |
Question
Currently, I have created subsites programmatically in FeatureReceiver method and have a way to create publishing pages, but they are being created on the root site. Is there a way to target a specific subsite to add publishing pages to it??
Since this is in the feature receiver I am accessing the current web like this from another method using the SPFeatureReceiver properties
SPWeb spWeb = properties.GetWeb();
I tried something like this but was not successful:
SPSite site = properties.Feature.Parent as SPSite;
{
using (SPWeb oSPWeb = site.OpenWeb("http://www.mysite.com/newsite"))
{
// page creation code
}
}
Any help would be good
Solution
All
Ended up getting things to work
After getting SPSite site = properties.Feature.Parent as SPSite;
I found out that all I needed to do was add the rest of the url which was could be something like "/NewPage" to the rest of the url since I already had the full site url from the properties.
Created a method that passed in the site and the weburl to get my desired result to provison the webpages
UPDATE:
Here is the code passing it these parameters:
This method is used to create the Page
private void CreatePage(SPSite site1, string cWebUrl, string cPageLayout, string cPageName, string cPageTitle)
{
try
{
using (SPWeb spoWeb = site1.OpenWeb(cWebUrl))
{
PublishingWeb pWebRoot = PublishingWeb.GetPublishingWeb(site1.RootWeb);
PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(spoWeb);
PageLayout[] layouts = pWebRoot.GetAvailablePageLayouts();
PageLayout pl = GetPageLayout(layouts, cPageLayout);
if (pl != null)
{
//Create My Pages
PublishingPage newPage = pWeb.GetPublishingPages().Add(cPageName, pl);
newPage.Layout = pl;
newPage.Title = cPageTitle;
newPage.Update();
newPage.CheckIn("");
pWeb.Update();
}
}
}
catch (Exception ex)
{
//Error Handling code
}
}
Then I created another method where I passed in the values to all the pages I wanted to create:
private void CreatePages(SPSite site)
{
CreatePage(site, "/", "MyPage1.Minimal..aspx", "Page1.aspx", "Page1");
CreatePage(site, "/", "MyPage2.Minimal.aspx", "Page2.aspx", "Page2");
CreatePage(site, "/", "MyPage3.Minimal.aspx", "Sitemap.aspx", "Sitemap");
}