Thursday, March 29, 2012

Master Page replaced

Master Page disappeared after feature activation

After installing a usercontrol/webpart as feature, my custom Master page got changed with another one (with the new user control in it).There is only .wsp and .dll(no source code) so don't know what is happening with the activation. How could I bring back my old master page ? As well as have the master page contain the new installed control.(I used sharepoint 2010 but should apply to other versions as well).

1) Find how the user control is embedded in the new Master Page
        search for the user control and you could find an entry like
<SharePoint:DelegateControl ID="DelegateControl1" runat="server" ControlId="UserControlName" Version="4"/> 

2) Change the master page back to the previous one (the one you had before)
          from sharepoint site settings / Sharepoint Designer(set as default option)
     We get back our master page but with out the newly installed control

3) Add the user control entry you found in step 1 to your master page.

Rememeber that sharepoint designer fixes will be gone if you deploy the sharepoint solution through code next time(next update)
Tip:
The reason why I used only the controld(ControlId="UserControlName") is because the controlId is bound to user control when deployed, in Elements.xml (\..\Web Server Extensions\14\TEMPLATE\FEATURES\UserControlNameFeature\UserControl\Elements.xml)

<Control Id="UserControlName" Sequence="10" ControlSrc="~/_controltemplates/User_Control_WebPart/UserControl.ascx">
</Control>


proceed yourself based on what you find in step 1

And the reason why the master page got changed when I activated the user control was because of the following code in the user control/web part.(found from reverse engineering)

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite parent = (SPSite) properties.Feature.Parent;
SPWeb rootWeb = parent.RootWeb;
Uri uri = new Uri(rootWeb.Url + "/_catalogs/masterpage/CoolMaster.master");
rootWeb.MasterUrl = uri.AbsolutePath;
rootWeb.CustomMasterUrl = uri.AbsolutePath;
rootWeb.Update();
}

---End--comment your thoughts please


No comments:

Post a Comment