1

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.

1
  • I know this is old but see my answer below for code if you like... I move it to another folder first. Commented May 16, 2011 at 11:48

2 Answers 2

3

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.

3
  • Thanks, I was thinking I might have to do that, but my concern was what would happen when I reactivate the feature. Will the files get put back if I reactivate the feature?
    – TehOne
    Commented Jul 3, 2010 at 8:15
  • 1
    Yes everytime you activate the feature, the files will be provisioned again (unless they already exists). But remember that if you delete the files using a featureReceiver then any modifications (done using SharePoint Designer) are lost Commented Jul 3, 2010 at 8:56
  • Thanks for the help guys. Maybe there is a date stamp for last modified I can use to help me decide if I should delete the files using the FeatureReceiver.
    – TehOne
    Commented Jul 4, 2010 at 21:17
1

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;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.