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

  • Could you add your page creation code as well? – Robert Kaucher Mar 13 '14 at 16:21
  • What's the scope of your feature? – Robert Lindgren Mar 13 '14 at 17:36
  • Its scope as Site feature – nholloway4 Mar 13 '14 at 18:05


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


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)
            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;
        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");
| improve this answer | |
  • Post your code, and have another upvote! – Robert Kaucher Mar 15 '14 at 15:45
  • Will do Robert.. – nholloway4 Mar 16 '14 at 2:46
  • Hmmm ... I'd like to see it too, since I have to solve this exact problem. TIA, Josh – Josh Korn Mar 18 '14 at 13:06
  • Josh, I am going to chastise him via email for not adding it yet. – Robert Kaucher Mar 19 '14 at 20:41
  • Posted code in my answer above – nholloway4 Mar 19 '14 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.