1

I created a custom site definition in SharePoint 2010 declarative using ONET.xml. As Part of the Site Definition I created a page from page layout using “module” element as a feature and embedded this feature in Onet.xml “WebFeatures” section. Now what I am trying to achieve is, as soon as the site collection is created from this custom Site Definition the above page created using “module” element must be set as homepage and to create subsites programmatically, But I am stuck here.

Basically what I did was I created a custom “WebProvisioned” event receiver and wrote code to set the newly created page as homepage for the root web of the site collection. I made this event receiver as site scoped. If I navigate to the “Pages” library I can see my new page, but it’s not set as homepage\welcome page. I deactivated and reactivated the event receiver multiple times but still it is not set as the welcome page. Below is my code.

public override void WebProvisioned(SPWebEventProperties properties)
    {
        base.WebProvisioned(properties);
        SPSite site = properties.Web.Site;
        SPWeb web = site.RootWeb;
        SPFolder rootFolder = web.RootFolder;
        rootFolder.WelcomePage = "pages/myHome.aspx";
        rootFolder.Update();            
        web.Update();
    }

Any idea what’s happening? And what are my other options to achieve the same thing?

Thanks

1
  • "WebProvisioned" only fires for subwebs, never for root webs of new site collections. What you want is a feature stapler. I'd write a more detailed answer on that but won't have the time to do that today. Do a web search for feature staplers and that should get you started. msdn.microsoft.com/en-us/library/… Aug 26, 2014 at 13:09

1 Answer 1

1

Forget the event receiver, you can set the home page declaratively alongside your module by setting a property bag value. In the same elements file as your module, right beneath it, put the following code:

<PropertyBag Url="" ParentType="Folder" RootWebOnly="FALSE" AlwaysCreateFolder="TRUE" xmlns="http://schemas.microsoft.com/sharepoint/">
   <Property Name="vti_welcomepage" Value="Pages/MyHome.aspx" Type="string" />  
  </PropertyBag> 
3
  • Just as a fun aside, the question is a classic example of an "XY Problem". meta.stackexchange.com/questions/66377/what-is-the-xy-problem Aug 25, 2014 at 21:29
  • Thanks for the reply "Derek". That sets the home page. But I am also trying to know how can I run custom code immediately after a site is created from site definition.
    – user117048
    Aug 25, 2014 at 22:29
  • You are welcome, "user117048" :) See me comment above for feedback on your code issue. Aug 26, 2014 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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