1
votes

I have a MasterPage that I am deploying to a SharePoint 2007 server. I am using a feature and a wsp to do the deployment. After deployment, my new masterpage isn't available to select and use for my site. Then, if I activate my feature, I am able to select my master page. But, when I deactivate my feature (or even retract the solution and delete it from SharePoint), the master page is still available for selection and all of the other files that were part of my feature/solution are still on SharePoint. So, is there any way to remove the master page from being available when my feature is deactivated, and then if it gets activated again, have it be available again?

Hope this makes sense, thanks.

2
I know this is old but see my answer below for code if you like... I move it to another folder first. - trgraglia

2 Answers

3
votes

SharePoint doesn't by default clean up files deployed as part of a feature activation.

In order to remove the master page and other associated files you'll need to write a feature receiver for your feature, implement the FeatureDeactivating method, and remove your files using object model code instead of CAML. MSDN document for feature receivers is here, and there are blog examples of writing feature receiver code all over the web.

Bear in mind that in order to remove your master page you'll first need to make sure you reset the master page for all sites in the site collection to the default/another available master page. You'll also want to be careful not to remove resource files (CSS, images, etc.) that are shared among master pages or page layouts.

1
votes

First make sure you are not using the Master page anymore in the feature deactivating. Then you can remove it.

SPWeb web = (SPWeb)properties.Feature.Parent;

string customMasterUrl = (new Uri(web.Url + "/_catalogs/masterpage/Sample.master")).AbsolutePath;

if (web.MasterUrl != customMasterUrl)
{
  try
  {
    SPFile file = web.GetFile(customMasterUrl);
    SPFolder masterPageGallery = file.ParentFolder;

    SPFolder temp = masterPageGallery.SubFolders.Add("Temp");
    file.MoveTo(temp.Url + "/" + file.Name);
    temp.Delete();
  }
  catch (ArgumentException)
  {
    return;
  }
}