Overwrite the site page with custom page (Client and Server)- SharePoint 2013

In SharePoint 2013 MySite, there is a static search box in default.aspx. Since we need to apply our custom master page and make sure the search box will only show on the master page, we have to remove the static search box from default page.

Using Client Object Model

 List mpGallery = clientContext.Site.RootWeb.Lists.GetByTitle(folderName);
 FileCreationInformation fci = new FileCreationInformation();
 fci.Content = System.IO.File.ReadAllBytes(filePath);
 fci.Url = newUrl;
 fci.Overwrite = true;

 Microsoft.SharePoint.Client.File newfile = mpGallery.RootFolder.Files.Add(fci);
 clientContext.ExecuteQuery();

Using Server Object Model

// Overwrite Default page
SPFileCollection pages = web.RootFolder.Files;
byte[] files = GetPageFromLayout(@"\cibcmysite\default.aspx");
// Creates a file in the collection using the specified URL, a byte array that contains the contents 
of a file, and a Boolean value that specifies whether to overwrite any file that has the same name.
pages.Add("default.aspx", files, true);

// Get Custom default.aspx
public static byte[] GetPageFromLayout(string pageUrl)
{
       byte[] fileBinary = null;
       var path = SPUtility.GetVersionedGenericSetupPath(@"template\layouts" + pageUrl, 15);
       using (FileStream fs = System.IO.File.OpenRead(path))
       {
            fileBinary = new byte[fs.Length];
            int numToRead = (int)fs.Length;
            int numRead = 0;
            while (numToRead > 0)
            {
                 int n = fs.Read(fileBinary, numRead, numToRead);

                 if (n == 0)
                     break;

                 numRead += n;
                 numToRead -= n;
             }

        }

        return fileBinary;
}

Referencehttp://msdn.microsoft.com/en-us/library/system.io.filestream.read.aspx

http://msdn.microsoft.com/en-us/library/Microsoft.SharePoint.SPFileCollection.aspx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s