I have some logic that does some custom file provisioning in CSOM. It basically tries to upload various files located on a provided-hosted (Azure) site to mapped locations in the Sharepoint host site collection. One of our requirement is niot to overwrite any content accidentally. This means that if there is a naming conflict during the provisioning process, the original file will simply be moved and renamed, and the new one will take its place. This works like a charm in most cases, since the files are referenced by their URL' which stays the same from the external perspective.

Here's part of my code to better illustrate.

The problem is that when I try to apply this logic to a master page file, all the sites that reference this master page throughout the site collection still point to the renamed file and not the one I've just provisioned. This means that the master page referencing is either not based on straight URL mapping, but some other underlying indexing method that actually provides the right URL value at runtime, or that it can detect a change to a master page's location and subsequently updates all the sites that currently reference that master page.

Either way, I'm simply trying to find a way to keep the old master page while also setting a new one. Any ideas ?

if (fileAlreadyExists)
        {
            if (!fileCreationInfo.Overwrite)
            {
                // The existing file will be archived before being replaced by the new one
                var destinationFileName = string.Format(
                    "{0} ({1}){2}",
                    Path.GetFileNameWithoutExtension(ensuredFile.Name),
                    DateTime.UtcNow.ToString("yyyy-MM-dd hh-mm-ss"),
                    Path.GetExtension(ensuredFile.Name));

                var destinationurl = string.Format("{0}/{1}", parentFolder.ServerRelativeUrl, destinationFileName);
                ensuredFile.MoveTo(destinationurl, MoveOperations.None);
                clientContext.ExecuteQuery();
            }
        }
  • 1
    Have you tried to set it to default master page, then move the custom master page, and then upload new master page, and then set to new master page? – Amal Hashim Oct 31 '14 at 21:32
  • Do you know how I could set the default master page through CSOM ? That would be great, since it would take care of the branding for the whole site collection for me. – Phil Gref Nov 3 '14 at 14:12
up vote 1 down vote accepted

Alright, here's what I ended up doing.

Since I couldn't actually move the file without the references also being redirected, I had to first make sure that the original master page content was saved. I simply made a copy of the original file under a different name.

Now, I still had the problem of updating my master page throughout my site collection (since all my sites are pointing to it). Creating a new master page and iterating over all the webs in the site collection was either painfully slow or simply didn't work at all. I have yet to find a solution to quickly set the master page for all sites through CSOM such as in SharePoint manager or the theme UI in site settings, but this is more or less another topic.

I actually went around the problem of creating and setting a master page by updating the content of the one which is already set. This saves a lot of time and works perfectly. Here's the updated code.

if (fileAlreadyExists)
        {
            fileIsMasterPage = Path.GetExtension(ensuredFile.Name) == ".master";

            if (!fileCreationInfo.Overwrite)
            {                    
                // The existing file will be archived before being replaced by the new one
                var destinationFileName = string.Format(
                    "{0} ({1}){2}",
                    Path.GetFileNameWithoutExtension(ensuredFile.Name),
                    DateTime.UtcNow.ToString("yyyy-MM-dd hh-mm-ss"),
                    Path.GetExtension(ensuredFile.Name));

                var destinationurl = string.Format("{0}/{1}", parentFolder.ServerRelativeUrl, destinationFileName);

                if (fileIsMasterPage)
                {
                    // Note: we cannot simply move an active master page file and replace the original
                    // The content itself must be updated 
                    ensuredFile.CopyTo(destinationurl, false);
                    clientContext.ExecuteQuery();

                    using (var fileStream = fileCreationInfo.ContentStream)
                    {
                        ensuredFile.SaveBinary(new FileSaveBinaryInformation()
                                                   {
                                                       ContentStream = fileStream,
                                                   });
                        clientContext.ExecuteQuery();
                    }
                }
                else
                {
                    ensuredFile.MoveTo(destinationurl, MoveOperations.None);
                    clientContext.ExecuteQuery();
                }
            }

Using CSOM you can change the master page before moving as below

using (var ctx = new ClientContext(siteURL))
{
    var web = ctx.Web;
    var masterPageUrl = String.Format( "{0}/_catalogs/masterpage/custom.master", ctx.Web.ServerRelativeUrl );
    web.MasterUrl = masterPageUrl;
    web.CustomMasterUrl = masterPageUrl;
    web.Update();
    ctx.Load( web );
    ctx.ExecuteQuery();
}

Change masterpage from Seattle to Oslo using CSOM, Sharepoint online

  • Yes, I already have this kind of logic implemented. The problem is that I have to iterate over all the webs in my site collection in order to set the master page on each web. Although a perfectly valid solution, I'd only want to find a way to actually set the actual default master page, which would automatically set the master page on all webs throughout the site collection. My question was if there was a way to do this using CSOM. – Phil Gref Nov 3 '14 at 15:17
  • This is possible using Sharepoint designer, and the MoveTo method probably uses something simmilar to apply the change when relocating a master page file. Question is, do I have access to it ? – Phil Gref Nov 3 '14 at 15:19

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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