Saturday, April 7, 2012

Hide meeting workspace default templates

After creating a whole arsenal of nice new custom meeting workspace templates for Contoso, business decided thy no longer want to confuse end-users with the default templates and only want them to be able to pick the custom ones when scheduling meetings from outlook and the web front end. The following code will hide all meeting workspace templates containing 'meeting workspace' in their name.
namespace Contoso.HideMeetingWorkspace.Features.HideDefaultMeetingWorkspaceFeature
{
    using System;
    using System.Collections.ObjectModel;
    using System.Runtime.InteropServices;
    using Microsoft.SharePoint;

    [Guid("00000000-0000-0000-0000-000000000000")]
    public class HideDefaultMeetingWorkspaceFeatureEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            using (SPWeb web = properties.Feature.Parent as SPWeb)
            {
                SPWebTemplateCollection existingWebTemps = web.GetAvailableWebTemplates(1033);
                Collection newWebTemps = new Collection();
                for (int i = 0; i < existingWebTemps.Count; i++)
                {
                    if (!existingWebTemps[i].Title.ToLower().Contains("meeting workspace"))
                    {
                        newWebTemps.Add(existingWebTemps[i]);
                    }
                }

                web.SetAvailableWebTemplates(newWebTemps, 1033);
                web.Update();
            }
        }

        // Uncomment the method below to handle the event raised before a feature is deactivated.
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            using (SPWeb web = properties.Feature.Parent as SPWeb)
            {
                web.AllowAllWebTemplates();
                web.Update();
            }
        }

        // Uncomment the method below to handle the event raised after a feature has been installed.
        // public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        // {
        // }

        // Uncomment the method below to handle the event raised before a feature is uninstalled.
        // public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        // {
        // }

        // Uncomment the method below to handle the event raised when a feature is upgrading.
        // public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary parameters)
        // {
        // }
    }
}

Note: This is most definitely not the best written code nor does it cater for 100% of scenarios, but if you’re in a pinch and need a fast fix this should be more than enough to get you started.


No comments:

Post a Comment