woensdag 13 juni 2012

SharePoint 2010: Restrict web templates

Define which web templates may be available when you create a subsite


In the analysis of a new project was described that the users only a few webtemplates could see when they want to create a new subsite. After some research on the internet I found that it is possible to define that via the UI of SharePoint 2010:

Site Actions -> Site Settings -> Page layouts and site templates

(default)

(after the selection)



In this settings page you can define which web templates you set available but that's not a solution on the question of the analysis. You can't ask to each contributor to set this setting by themself so I wrote a featureReceiver and via featureStapling I hang to the defined site templates.
Important! If you want to do this via code be sure that the Publishing Infrastructure and Publshing Feature is activated!



The code of the FeatureReceiver:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
//The collection of web templates that you want to set available. Defined via their templateName
string[] limitedWebTempColl_EN = {"STS#0","SGS#0","BLOG#0","WIKI#0","ENTERWIKI#0","MPS#2,ACCSRV#1","ACCSRV#4","ACCSRV#6","ACCSRV#5"};

string[] limitedWebTempColl_NL = {"STS#0","SGS#0","BLOG#0","WIKI#0","ENTERWIKI#0","MPS#2,ACCSRV#1","ACCSRV#4","ACCSRV#6","ACCSRV#5"};

//Get subsite web object
SPWeb web = properties.Feature.Parent as SPWeb;
if (web != null)
{
try
{
//en-US
//Retrieve the current web template collection for en-US and store it in a feature property. This make it able to put it back in the featureDeActivated method
SPWebTemplateCollection currentWebTempColl = web.GetAvailableWebTemplates(1033);
//The CreateCommaSeparatedString method loops trhough the currentWebTempCollection and join each webtemplate.Name with a , in a string
web.Properties[PREVWebTemps_en_KEY] = CreateCommaSeparatedString(currentWebTempColl);                
//Get only the webtemplates that are defined in the "limitedWebTempColl_EN" string array
limitedCollWebTemps_EN = GetWebTemplateCollection(currentWebTempColl, limitedWebTempColl_EN);
//nl-NL
//Retrieve the current web template collection for nl-NL and store it in a feature property
SPWebTemplateCollection currentWebTempColl = web.GetAvailableWebTemplates(1043);
web.Properties[PREVWebTemps_nl_KEY] = CreateCommaSeparatedString(currentWebTempColl);
//Get only the webtemplates that are defined in the "LimitedWebTempColl_NL" string array
limitedCollWebTemps_NL = GetWebTemplateCollection(currentWebTempColl, limitedWebTempColl_NL);
//Set only the defined webtemplates and update the subsite
web.SetAvailableWebTemplates(limitedCollWebTemps_EN, 1033);
web.SetAvailableWebTemplates(limitedCollWebTemps_NL, 1043);

//Update the subsite to set the web template collection
//It's very important to update first the web and than the web properties if you do it in the other way web.SetAvailableWebTemplates(Collection<SPWebTemplate>, int language) is not set!
web.Update();
//Store the existing web template collection comma separated string in the property bag
web.Properties.Update();
}

catch (Exception ex)
{
IntranetLogger.LogError(IntranetDiagnosticsCategory.FeatureReceivers, "SetWebTemplates Activated - ERROR: {0}", ex.Message);
}
}
}

The method that gets the collection of web templates defined in the string array:

/// <summary>
/// Search the defined web templates in the incomming web template collection
/// </summary>
/// <param name="completeWebTemps">SPWebTemplateCollection the web template collection to search in</param>
/// <returns>A collection of webtemplates depended on the feature property value</returns>
private static Collection<SPWebTemplate> GetWebTemplateCollection(SPWebTemplateCollection webTempCollToSearchIn, string[] limitedColl)
{
Collection<SPWebTemplate> finalWebTempColl = new Collection<SPWebTemplate>();
for (int i = 0; i < webTempCollToSearchIn.Count; i++)
{
//Find web template in the web template collection where the name is the same as the name of the limitedColl defined web template
string webTempFound = Array.Find(limitedColl, str => str.Equals(webTempCollToSearchIn[i].Name));
//If the web template is found
if (!String.IsNullOrEmpty(webTempFound))
{
//Add them to the final web template collection
finalWebTempColl.Add(webTempCollToSearchIn[i]);
}
}

return finalWebTempColl;
}

Geen opmerkingen:

Een reactie posten