Search code examples
c#visual-studio-2008sharepoint-2007

File Copy the masterpage from sharepoint MainSite to the sharepoint subsite


I have this sharepoint masterpage file

http://abcd.com/sites/forum/_catalogs/masterpage/MyCustomMasterPage.master

and I wanted to copy the MyCustomMasterPage.master to

http://abcd.com/site/forum/MySiteA/_catalogs/masterpage/MyCustomMasterPage.master

How will I do this in C#? Please help me. Thanks!


Solution

  • string strMPageURL ="http://abcd.com/sites/forum/_catalogs/masterpage/MyCustomMasterPage.master";
    
    SPFolder mPageFolder = spWeb.Folders["_catalogs"].SubFolders["masterpage"];
    using (WebClient oWebClient = new WebClient())
    {
        SPFileCollection mPageFileCollection = mPageFolder.Files;
        SPFile mPageFile = mPageFileCollection.Add(
            "MyCustomMasterPage.master",
            oWebClient.OpenRead(strMPageURL)
        );
    }
    

    It actually behaves like you are uploading the masterpage to the _catalogs/masterpage folder but the difference is that it came from the web not from the local machine.

    If you plan to upload the masterpage just like how your out of the box behave using file upload from local machine, you can do this.

    string strMPageLocation =@"C://MyCustomMasterPage.master";
    
    SPFolder mPageFolder = spWeb.Folders["_catalogs"].SubFolders["masterpage"];
    using (FileStream mPageStream = new FileStream(strMPageLocation,FileMode.Open))
    {
        SPFileCollection mPageFileCollection = mPageFolder.Files;
        SPFile mPageFile = mPageFileCollection.Add(
            "MyCustomMasterPage.master",
            mPageStream
        );
    }