Monday, September 26, 2011

Sharepoint 2010 Master page issue with re-deployment with new changes from Visual Studio solution

PROBLEM :- There is a problem with the current process. For the first time this will work but if you make any changes to the master page and re-deploy the same solution will not deploy this new master page with the changes on the server.


In my last post "SharePoint 2010 Deploy Master Page using Visual Studio Solution Project and Set it as Default Master Page.", i explained how to add a master page and set it as a default. I suggest to read the last blog first.


What causes the issue?

Answer: When a file is deployed to the server using a Visual Studio Solution, the file gets deleted first and than added back to server. But when we assign a file as a Default Master Page it "CANNOT" be deleted. So just follow the below given steps to make it a fix.

Solution

Just write a deactivating event in the feature receiver setting V4.master as a master page. Please checkout the code below.


/// <summary>
/// Set the v4.master as default master for site.
/// </summary>
/// <param name="properties"></param>
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
    SPSecurity.RunWithElevatedPrivileges(delegate
    {
        using (SPWeb _web = ((SPSite)properties.Feature.Parent).RootWeb)
        {
            Uri _siteMaster = new Uri(string.Format("{0}/_catalogs/masterpage/v4.master", _web.Url));
            _web.MasterUrl = _siteMaster.AbsolutePath;
            _web.CustomMasterUrl = _siteMaster.AbsolutePath;
            _web.Update();
        }
    });
}
Hope this was helpful to you. Thanks for reading the blog.

4 comments:

  1. Error occurred in deployment step 'Activate Features': Unable to cast object of type 'Microsoft.SharePoint.SPWeb' to type 'Microsoft.SharePoint.SPSite'.

    I m geeting this error what i do .Please tell me ASAP>

    ReplyDelete
    Replies
    1. Its because the feature you are using is a Web Based feature.

      In the above given code use this "SPWeb _web = ((SPWeb)properties.Feature.Parent).Site.RootWeb;"

      Delete
  2. Everyone will also receive an error that the new master does not exist. this is because it has not been set as the default in Designer. Doing it as a feature in Visual Studio does not work.

    ReplyDelete
  3. Thanks, it works fine with SharePoint 2013

    ReplyDelete