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

I have been researching whether there is a best way to brand SharePoint 2013 without changing the v4 master page on the farm level that will affect each site collection in the farm, but I haven't been able to find anything conclusive. I feel like I should emphasize that I would like to change EVERY site collection and not just one site collection. Everything I've read seems to emphasize the edit-ability of each site collection individually, but I would like to determine whether I can create a custom master page on the farm level and change it to affect already created sites, or whether I have to change the v4 master page. Any help would be appreciated.

share|improve this question

2 Answers 2

You cannot do this on a farm level. You'd have to add the masterpage to each sitecollection individually.

This can be done with using powershell which adds the modified masterpage to each sitecollection. Or you can develop a Visual Studio feature which is WebApplication scoped which you then install on the server.

Here are some valid pointers:

share|improve this answer

Create a Visual Studio project targeting to the web application you want to change.

Add your custom master page and a feature with an event receiver with this code in feature activated and featureDeactivating

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    if (properties != null)
    {
        SPSite theSite= properties.Feature.Parent as SPSite;
        using (SPWeb web = theSite.RootWeb)
        {
            try
            {
                SPMaster.ActivateMaster(web, "customized.master", false);
                SPMaster.ActivateMaster(web, "customizedBackend.master", true);
            }
            catch (Exception ex)
            {
                FactoriaServicios.LoggerInstance.EscribirErrorLog(ex, System.Reflection.MethodBase.GetCurrentMethod());
            }
        }
    }
}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
    if (properties != null)
    {
        SPSite theSite = properties.Feature.Parent as SPSite;
        using (SPWeb web = theSite.RootWeb)
        {
            try
            {
                SPMaster.DeactivateMaster(web, false);
                SPMaster.DeactivateMaster(web, true);
            }
            catch (Exception ex)
            {
                //exception treatment
            }
        }
    }
}

Create a class SPMaster with this code

public static void ActivateMaster(SPWeb contextoWeb, string rutaMaster, bool masterAdministracion)
{
    try
    {
        contextoWeb.AllowUnsafeUpdates = true;
        if (!rutaMaster.Contains("/_catalogs/masterpage/"))
        {
            rutaMaster = SPUtility.ConcatUrls("/_catalogs/masterpage/", rutaMaster);
        }
        Uri uriMaster = new Uri(SPUtility.ConcatUrls(contextoWeb.Url, rutaMaster));
        if (masterAdministracion)
        {
            if (!contextoWeb.MasterUrl.Contains(rutaMaster))
            {
                contextoWeb.AllProperties["OldMasterUrl"] = contextoWeb.MasterUrl;
            }
            try
            {
                contextoWeb.MasterUrl = uriMaster.AbsolutePath;
                contextoWeb.Update();
            }
            catch (Exception ex)
            {
                //Exception treatment
            }
        }
        else
        {
            if (!contextoWeb.CustomMasterUrl.Contains(rutaMaster))
            {
                contextoWeb.AllProperties["OldMasterUrl"] = contextoWeb.CustomMasterUrl;
            }
            try
            {
                contextoWeb.CustomMasterUrl = uriMaster.AbsolutePath;
                contextoWeb.Update();
            }
            catch (Exception ex)
            {
                //exception treatment
            }
        }
    }
    catch (Exception ex)
    {
        //exception treatment
    }
    finally
    {
        contextoWeb.AllowUnsafeUpdates = false;
    }
}


public static void DeactivateMaster(SPWeb contextoWeb, bool masterAdministracion)
{
    try
    {
        contextoWeb.AllowUnsafeUpdates = true;
        string rutaMaster = string.Empty;
        if (masterAdministracion)
        {
            if (contextoWeb.AllProperties["OldMasterUrl"] != null)
            {
                rutaMaster = contextoWeb.AllProperties["OldMasterUrl"].ToString();
            }
        }
        else
        {
            if (contextoWeb.AllProperties["OldMasterUrl"] != null)
            {
                rutaMaster = contextoWeb.AllProperties["OldMasterUrl"].ToString();
            }
        }
        if (!string.IsNullOrEmpty(rutaMaster))
        {
            Uri uriMaster = new Uri(SPUtility.ConcatUrls(contextoWeb.Url, rutaMaster));
            if (masterAdministracion)
            {
                try
                {
                    contextoWeb.MasterUrl = uriMaster.AbsolutePath;
                    contextoWeb.Update();
                }
                catch (Exception ex)
                {
                    //exception treatment
                }
            }
            else
            {
                try
                {
                    contextoWeb.CustomMasterUrl = uriMaster.AbsolutePath;
                    contextoWeb.Update();
                }
                catch (Exception ex)
                {
                    //exception treatment
                }
            }
        }
    }
    catch (Exception ex)
    {
        //exception treatment
    }
    finally
    {
        contextoWeb.AllowUnsafeUpdates = false;
    }
}

Glad to help you

share|improve this answer

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.