features

How to hide the workspace creation option in the Events, in sharepoint 2010.

Posted on

This scenario is pretty easy, I needed to be able to HIDE by WEBSITE only, the option to show the workspace creation checkbox for the events.
The thing is that because we have many sites, in some it should be still possible to create the workspaces for events.
What I did, its a feature scoped to Web, and then I used the following code, which allows me to hide/unhide when the feature is activated/deactivated.

  public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            // False, sets the Hidden property to true.
            ChangeWorkspaceCreationVisibility(properties, true);
        }

        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            // True, sets the Hidden property to false.
            ChangeWorkspaceCreationVisibility(properties, false);
        }

        // 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)
        //{
        //}

        private void ChangeWorkspaceCreationVisibility(SPFeatureReceiverProperties properties, bool enable)
        {
            using (SPWeb web = properties.GetWeb())
            {

                for (int i = 0; i < web.Lists.Count; i++)
                {
                    if (web.Lists[i].BaseTemplate == SPListTemplateType.Events)
                    {
                        var field = web.Lists[i].Fields[SPBuiltInFieldId.WorkspaceLink];
                        field.Hidden = enable;
                        field.Update();
                    }
                }               
            }
        }

    }

    public static class Extensions
    {
        public static SPWeb GetWeb(this SPFeatureReceiverProperties properties)
        {
            SPWeb site;
            if (properties.Feature.Parent is SPWeb)
            {
                site = (SPWeb)properties.Feature.Parent;
            }
            else if (properties.Feature.Parent is SPSite)
            {
                site = ((SPSite)properties.Feature.Parent).RootWeb;
            }
            else
            {
                throw new Exception("Error 192424234223442: Unable to retrieve SPWeb - this feature is not Site or Web-scoped.");
            }
            return site;
        }
    }