How to hide the default Site Templates from Custom User Groups in SharePoint 2010 using C#

1.8k views Asked by At

I need to hide all the default site templates from a custom user group except Blog and custom site templates using programming in C#.

I have Level - 2 SharePoint Coordinator group which should allow the users to create sites from my custom site templates and Blog all other site templates should be hidden.

I have Level - 3 SharePoint Coordinator group it has Full Control permission level so this group users should have all the site templates available.

My code:

 public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        SPWeb web = properties.Feature.Parent as SPWeb;
        SPWebTemplateCollection templates = web.GetAvailableWebTemplates(1033, true);
        foreach (SPWebTemplate template in templates)
        {
            if (template.Name != "Blog" && template.DisplayCategory =="CUSTOM")
            {
                //how to attach the user group to a site template or any other suggestions
            }
        }
    }

My Question is: So how to attach a user group to a site template or how to display the custom site template to Level - 2 and all site templates for Level - 3 Group?

Part Answer.

Thanks SigarDave. I was able to hide default site templates except Blog and my Custom Site templates. Below is the code.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        try
        {
            SPWeb web = properties.Feature.Parent as SPWeb;
            SPWebTemplateCollection templates = web.GetAvailableWebTemplates((uint)1033);
            Collection<SPWebTemplate> collection = new Collection<SPWebTemplate>();
            foreach (SPWebTemplate template in templates)
            {
                if(template.Name == "BLOG#0" || template.DisplayCategory == "Custom")
                collection.Add(template);
            }
            web.SetAvailableWebTemplates(collection, (uint)1033);
            web.Update();
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.ToString());
        }
    }

    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    {
        try
        {
            SPWeb web = properties.Feature.Parent as SPWeb;
            // Show all available web templates
            web.AllowAllWebTemplates();
            web.Update();
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.ToString());
        }
    }

This code hides the template for all the users including Site Collection Admin, How to enable or show all the site templates to SC Admin? Thanks.

0

There are 0 answers