9

Using CSOM and c#, I would like to change the master-page from Seattle to Oslo. Can anybody help me?

8

Use the MasterUrl and CustomMasterUrl properties of Web:

using (var ctx = new ClientContext( url ))
{
    var web = ctx.Web;
    var masterPageUrl = String.Format( "{0}/_catalogs/masterpage/oslo.master", ctx.Web.ServerRelativeUrl );

    web.MasterUrl = masterPageUrl;
    web.CustomMasterUrl = masterPageUrl;
    web.Update();

    ctx.Load( web );
    ctx.ExecuteQuery();
}
2

Changing Master page is very easy in SharePoint Using CSOM, we can give the Master Page URL in the web masterpage property, for reference u can check this link : http://www.sharepointsol.com/2015/01/apply-master-page-using-csom.html

  • is there a way to enforce "reset all subsites to use same master page" using CSOM? – Vignesh Subramanian Oct 7 '15 at 9:30
1

For SharePoint Online - use Visual Studio. NuGet the following package into your winform or console app: Micrososft.SharePointOnline.CSOM

Then use code similar to the below:

using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ChangeMaster
{
public partial class ChangeMaster : System.Windows.Forms.Form
{
    public ChangeMaster()
    {
        InitializeComponent();
    }


    private SecureString ConvertToSecureString(string password)
    {
        if (password == null)
            throw new ArgumentNullException("password");

        var securePassword = new SecureString();

        foreach (char c in password)
            securePassword.AppendChar(c);

        securePassword.MakeReadOnly();
        return securePassword;
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Microsoft.SharePoint.Client.ClientContext cc = new Microsoft.SharePoint.Client.ClientContext("https://yourco.sharepoint.com/");
        SecureString secstring = ConvertToSecureString("yourpassword");
        SharePointOnlineCredentials creds = new SharePointOnlineCredentials("user@yourco.onmicrosoft.com", secstring);


        // The SharePoint web at the URL.
        Web web = cc.Web;

        // We want to retrieve the web's properties.
        cc.Load(web);

        // Execute the query to the server.
        cc.ExecuteQuery();

        var a = web.Title; // For debug checks.
        var master = web.CustomMasterUrl; // For debug checks.
        web.CustomMasterUrl = "/_catalogs/masterpage/seattle.master"; // Or the original page desired.
        web.MasterUrl = "/_catalogs/masterpage/seattle.master";
        web.Update();
        // Execute the query to server.
        cc.ExecuteQuery();
    }
}

}

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.