Is there a way that I can copy a page from Pages library into another library (Site Pages) automatically once my feature is activated? I have written a custom event receiver which will set the master page for newly created personal sites and after the creation of the site I want to replace the 'default.aspx' page with my custom 'default.aspx

Thanks in advance.


If you use SharePoint Publishing feature you can change your welcom page through UI (Site Settings -> Look and feel -> welcom page) or you can use PublishingWeb.DefaultPage method in your code.

  • Hi Alexander But i want to achieve that in mysite personal sites and each of them are individual site collections so how can i achieve them in all personal sites – Anil Jun 7 '12 at 14:31

For replacing default.aspx with your custom file, in feature activation receiver, place the following code:

SPFile newDefaultPage = // get your custom default page from somewhere
SPFile defaultPage = web.RootFolder.Files["default.aspx"];

// saving backup
defaultPage.CopyTo("default-old.aspx", true);

// copying our custom page over the default
newDefaultPage.CopyTo("default.aspx", true);

Similarly, in the page deactivation handler, you can restore the page from the default-old.aspx file.

Update: if you want to activate this feature on site collection scope, and then replace the default.aspx files on all the sites in site collection except root site, you can achieve this using following code:

var site = (SPSite)properties.Feature.Parent;
foreach (SPWeb web in site.AllWebs)
    if (web.IsRootWeb)


Where ReplaceDefaultAspx method contains the first piece of code.

  • Hi Andrey Thanks for the answer i have one more problem actually i want to achieve that only on the sub site level not at the site level .Is there a way that this can be done through code ..Thanks – Anil Jun 7 '12 at 14:38
  • Sure. I've updated the answer. – Andrey Markeev Jun 7 '12 at 14:51
  • Hi Andrey Thanks for the answer . Can you please answer one more question is there a way to just the change the Welcome Page URL in all sub sites when feature is activated , I am pretty new to share point and i would appreciate your help. I posted the question sharepoint.stackexchange.com/questions/37809/… here and the error i am facing is that once i activated the publishing in blog the welcome page is taking me to /pages/default.aspx instead of just default.aspx . so and i observed that if i change the welcome page URl manually its working .Please help me. – Anil Jun 7 '12 at 15:11

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.