Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Is there a way that i can copy a page from pages library into another Folder(Site Pages) library 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..

share|improve this question

2 Answers 2

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)
        continue;

    try
    {
        ReplaceDefaultAspx(web);
    }
    finally
    {
        web.Dispose();
    }
}

Where ReplaceDefaultAspx method contains the first piece of code.

share|improve this answer
    
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

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.

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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