User Tools

Site Tools


notes:sharepoint2010:features

Features in SharePoint 2010

Apply the custom master page and CSS files to the current website on feature activation. The feature has website scope:

public class MyFeatureEventReceiver : SPFeatureReceiver
{
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        base.FeatureActivated(properties);
 
        SPWeb web = properties.Feature.Parent as SPWeb;
        if (web == null) return;
 
        web.MasterUrl = 
            WebUtils.CombineUrls(web.Site.ServerRelativeUrl, "_catalogs/masterpage/MyMasterPage.master");
        web.CustomMasterUrl = 
            WebUtils.CombineUrls(web.Site.ServerRelativeUrl, "_catalogs/masterpage/MyMasterPage.master");
        web.AlternateCssUrl = 
            WebUtils.CombineUrls(web.Site.ServerRelativeUrl, "_layouts/MyWebsite/custom.css");
        web.Update();
    }
 
    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    {
        base.FeatureDeactivating(properties);
 
        SPWeb web = properties.Feature.Parent as SPWeb;
        if (web == null) return;
 
        web.MasterUrl = 
            WebUtils.CombineUrls(web.Site.ServerRelativeUrl, "_catalogs/masterpage/v4.master");
        web.CustomMasterUrl = 
            WebUtils.CombineUrls(web.Site.ServerRelativeUrl, "_catalogs/masterpage/v4.master");
        web.Update();
    }
}

Another example of activating the custom master page is here.

Activate and deactivate features in multiple site collections:

using System;
using System.Collections.Generic;
using System.Configuration;
 
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Navigation;
 
class WbsFeatureManager
{
    public static void DeactivateCustomFeatures()
    {
        List<string> siteUrls = ... // obtain site URLs
        List<Guid> siteFeatures = ... // obtain a list of site collection features
        List<Guid> webFeatures = ... // obtain a list of site features
 
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            foreach (string siteUrl in siteUrls)
            {
                Console.WriteLine("");
                Console.WriteLine("SiteCollection: " + siteUrl);
 
                using (SPSite site = new SPSite(siteUrl))
                {
                    DeactivateWebFeatures(site, webFeatures);
                    DeactivateRootWebFeatures(site, webFeatures);
                    DeactivateSiteFeatures(site, siteFeatures);
                }
            }
 
            Console.WriteLine("");
        });
    }
 
    public static void ActivateCustomFeatures()
    {
        List<string> siteUrls = ... // obtain site URLs
        List<Guid> siteFeatures = ... // obtain a list of site collection features
        List<Guid> webFeatures = ... // obtain a list of site features
 
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            foreach (string siteUrl in siteUrls)
            {
                Console.WriteLine("");
                Console.WriteLine("SiteCollection: " + siteUrl);
 
                using (SPSite site = new SPSite(siteUrl))
                {
                    ActivateSiteFeatures(site, siteFeatures);
                    ActivateRootWebFeatures(site, webFeatures);
                    ActivateWebFeatures(site, webFeatures);
                }
            }
 
            Console.WriteLine("");
        });
    }
 
    private static void DeactivateWebFeatures(SPSite site, IEnumerable<Guid> webFeatures)
    {
        foreach (SPWeb web in site.RootWeb.Webs)
        {
            Console.WriteLine("Web: " + web.Name);
            foreach (Guid featureId in webFeatures)
            {
                var feature = web.Features.SingleOrDefault(sf => sf.DefinitionId == featureId);
                if (feature != null)
                {
                    string name = 
                        (feature.Definition != null ? 
                        feature.Definition.DisplayName : 
                        feature.DefinitionId.ToString());
 
                    Console.WriteLine(String.Format("    Deactivating feature {0}", name));
                    web.Features.Remove(feature.DefinitionId, true);
                    web.Update();
                    Console.WriteLine(String.Format("    Feature {0} deactivated", name));
                }
            }
        }
    }
 
    private static void DeactivateRootWebFeatures(SPSite site, IEnumerable<Guid> webFeatures)
    {
        Console.WriteLine("RootWeb: " + site.RootWeb.Url);
        foreach (Guid featureId in webFeatures)
        {
            var feature = site.RootWeb.Features.SingleOrDefault(sf => sf.DefinitionId == featureId);
            if (feature != null)
            {
                string name = 
                    (feature.Definition != null ? 
                    feature.Definition.DisplayName : 
                    feature.DefinitionId.ToString());
 
                Console.WriteLine(String.Format("    Deactivating feature {0}", name));
                site.RootWeb.Features.Remove(feature.DefinitionId, true);
                site.RootWeb.Update();
                Console.WriteLine(String.Format("    Feature {0} deactivated", name));
            }
        }
    }
 
    private static void DeactivateSiteFeatures(SPSite site, IEnumerable<Guid> siteFeatures)
    {
        Console.WriteLine("SiteCollection: " + site.Url);
        foreach (Guid featureId in siteFeatures)
        {
            var feature = site.Features.SingleOrDefault(sf => sf.DefinitionId == featureId);
            if (feature != null)
            {
                string name = 
                    (feature.Definition != null ? 
                    feature.Definition.DisplayName : 
                    feature.DefinitionId.ToString());
 
                Console.WriteLine(String.Format("    Deactivating feature {0}", name));
                site.Features.Remove(feature.DefinitionId, true);
                Console.WriteLine(String.Format("    Feature {0} deactivated", name));
            }
        }
    }
 
    private static void ActivateSiteFeatures(SPSite site, IEnumerable<Guid> siteFeatures)
    {
        Console.WriteLine("SiteCollection: " + site.Url);
        foreach (Guid featureId in siteFeatures)
        {
            var feature = site.Features.SingleOrDefault(sf => sf.DefinitionId == featureId);
            if (feature == null)
            {
                Console.WriteLine(String.Format("    Activating feature {0}", featureId.ToString()));
                site.Features.Add(featureId, true);
                var activatedFeature = site.Features[featureId];
                Console.WriteLine(String.Format("    Feature {0} activated",
                    activatedFeature.Definition.DisplayName));
            }
        }
    }
 
    private static void ActivateRootWebFeatures(SPSite site, IEnumerable<Guid> webFeatures)
    {
        Console.WriteLine("RootWeb: " + site.RootWeb.Url);
        foreach (Guid featureId in webFeatures)
        {
            var feature = site.RootWeb.Features.SingleOrDefault(sf => sf.DefinitionId == featureId);
            if (feature == null)
            {
                Console.WriteLine(String.Format("    Activating feature {0}", featureId.ToString()));
                site.RootWeb.Features.Add(featureId, true);
                var activatedFeature = site.RootWeb.Features[featureId];
                Console.WriteLine(String.Format("    Feature {0} activated", 
                    activatedFeature.Definition.DisplayName));
            }
        }
    }
 
    private static void ActivateWebFeatures(SPSite site, IEnumerable<Guid> webFeatures)
    {
        foreach (SPWeb web in site.RootWeb.Webs)
        {
            Console.WriteLine("Web: " + web.Name);
            foreach (Guid featureId in webFeatures)
            {
                var feature = web.Features.SingleOrDefault(sf => sf.DefinitionId == featureId);
                if (feature == null)
                {
                    Console.WriteLine(String.Format("    Activating feature {0}", featureId.ToString()));
                    web.Features.Add(featureId, true);
                    web.Update();
                    var activatedFeature = web.Features[featureId];
                    Console.WriteLine(String.Format("    Feature {0} activated", 
                        activatedFeature.Definition.DisplayName));
                }
            }
        }
    }
}
notes/sharepoint2010/features.txt · Last modified: 2016/10/19 by admin