0

I'm having trouble with a feature I have made which deploys branding to SharePoint 2013, the feature deployed without an problems the first time around however any additional deployments state that "Could not delete file

http://SharePointServer/sites/MyCompany/_catalogs/masterpage/mycustom.master' from server.

So for some reason mycustom.master isn't being removed correctly. Here is my code within my event receiver for the feature.

using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;

namespace Custom_Branding_Full.Features.ActivateTheme
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation,     installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("e4e1f7e5-3318-4d7a-8856-740fe52955cf")]
    public class ActivateThemeEventReceiver : SPFeatureReceiver
    {
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        SPSite site = properties.Feature.Parent as SPSite;

        if (site != null)
        {
            SPWeb topLevelSite = site.RootWeb;

            // Calculate relative path to site from Web Application root.
            string webAppRelativePath = topLevelSite.ServerRelativeUrl;
            if (!webAppRelativePath.EndsWith("/"))
            {
                webAppRelativePath += "/";
            }

            // Activate publishing infrastructure
            site.Features.Add(new Guid("f6924d36-2fa8-4f0b-b16d-06b7250180fa"), true);

            // Enumerate through each site and apply branding.
            foreach (SPWeb web in site.AllWebs)
            {
                // Activate the publishing feature for all webs.
                web.Features.Add(new Guid("94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb"), true);
                              web.MasterUrl = webAppRelativePath + "_catalogs/masterpage/mycustom.master";
                        web.CustomMasterUrl = webAppRelativePath + "_catalogs/masterpage/mycustom.master";
                        web.AlternateCssUrl = webAppRelativePath + "_catalogs/masterpage/css/mycustom.css";
                web.CustomJavaScriptFileUrl = webAppRelativePath + "_catalogs/masterpage/js/stuff.js";

                web.Update();
            }
        }
    }

    /// Deactivating the feature

    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    {
        SPSite siteCollection = properties.Feature.Parent as SPSite;
        if (siteCollection != null)
        {
            SPWeb topLevelSite = siteCollection.RootWeb;

            // Calculate relative path to site from Web Application root.
            string webAppRelativePath = topLevelSite.ServerRelativeUrl;
            if (!webAppRelativePath.EndsWith("/"))
            {
                webAppRelativePath += "/";
            }

            // Enumerate through each site and apply branding.
            foreach (SPWeb site in siteCollection.AllWebs)
            {

                        site.AlternateCssUrl = webAppRelativePath + "";
                site.CustomJavaScriptFileUrl = webAppRelativePath + "";
                site.SiteLogoUrl = string.Empty;
                site.CustomMasterUrl = webAppRelativePath + "_catalogs/masterpage/seattle.master";
                site.MasterUrl = webAppRelativePath + "_catalogs/masterpage/seattle.master";
                site.Update();
            }
        }
    }
}
}

Does anyone know what may be wrong here? Additionally my feature when deployed is not actually showing up in the features list as something that can be activated and deactivated which is odd.

Any help is appreciated, many thanks.

1
  • Where are you deleting the file in your code?
    – Aanchal
    May 23, 2014 at 8:06

1 Answer 1

0

Write following code after finishing your foreach block:

 foreach (SPWeb site in sitecollection.AllWebs)
                    {
                        WebAppRelativePath = site.Url;
                        if (!WebAppRelativePath.EndsWith("/"))
                        {
                            WebAppRelativePath += "/";
                        }


                        file = site.GetFile(WebAppRelativePath + "_catalogs/masterpage/custom.master");
                        if (file.Exists)

                        {
                            file.Delete();
                        }
                        file.Update();
                    }

Also check : http://social.msdn.microsoft.com/Forums/sharepoint/en-US/22183e15-5985-4aee-a9f0-247a58f472b8/delete-custom-master-page-error-on-feature-deactivation?forum=sharepointdevelopmentlegacy

Unable to Delete Custom Master Page Programmatically During Feature Deactivation 2013

3
  • Thanks for adding this answer, that makes sense I guess telling it to delete it. I tried adding the code you pasted and also looked at the links you provided by I'm getting a couple of reference errors. 'file' and 'WebAppRelativePath' do not exist in the current context.....any ideas?
    – Yanayaya
    May 23, 2014 at 8:20
  • Do let me know if does not solve your issue.
    – Aanchal
    May 23, 2014 at 8:21
  • Please mark this as answer if it helps you.
    – Aanchal
    May 27, 2014 at 8:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.