By: Philip Stathis
Here is a list of some common customizations to a site template that one would come across:
How do I activate a site collection feature programatically?
There are a few ways to do this; the way I find most intuitive is by using the display name of the actual feature you are trying to activate. The following code snippet shows this process:
public static void SiteFeatureActivate(SPSite site)
{
List<Guid> list = new List<Guid>();
foreach (SPFeatureDefinition featuredef in site.WebApplication.Farm.FeatureDefinitions)
{
if (featuredef.DisplayName == "Branding_Branding")
list.Add(featuredef.Id);
if (featuredef.DisplayName == "PublishingSite")
list.Add(featuredef.Id);
}
foreach (Guid id in list)
{
site.Features.Add(id, true);
}
}
The extra step here is to get the GUID, but this can be omitted if the GUID is known, it’s just meant as a convenience. The last line of code, adding the Feature to the site is essentially pressing the Activate Feature Button:

How do I add an SPList using a specific list template?

There is a convenient enum that can be used:
public static void WorkerMethod(SPWeb web)
{
web.Lists.Add("Project Documents", string.Empty, SPListTemplateType.DocumentLibrary);
web.Lists.Add("Announcements", string.Empty, SPListTemplateType.Announcements);
web.Lists.Add("External Links", string.Empty, SPListTemplateType.Links);
web.Lists.Add("Group Calendar", string.Empty, SPListTemplateType.Events);
web.Lists.Add("Project Calendar", string.Empty, SPListTemplateType.Events);
web.Lists.Add("Project Contacts", string.Empty, SPListTemplateType.Contacts);
web.Lists.Add("Discussion Board", string.Empty, SPListTemplateType.DiscussionBoard);
}
I would like to add an Asset Library, and I can’t find it in the enum, is there a way?

Yes there is, but the asset library template will only appear after the Publishing site collection feature has been activated. (Conveniently, that’s the "PublishingSite" feature that is shown in the first question.
public static void AddAssetLibs(SPWeb web)
{
SPListTemplate template = web.ListTemplates["Asset Library"];
web.Lists.Add("Project Photos", string.Empty, template);
web.Lists.Add("Project Videos", string.Empty, template);
web.Update();
}
The code essentially shows that we have to find the template manually since it’s not available in the SPListTemplateType enum.
How do I add navigation links for all these?
Given the business requirements/rules, there is often a very strict grouping of links that need to appear on the Quick Launch in the SharePoint site. After the creation of libraries and lists, 3 nodes in the quick launch have been generated. In the following case we specify which children are going to go under the specific heading.
The desired result is:

This is how we can make this happen:
public static void ProvisionQuickLaunch(SPWeb web)
{
SPNavigationNode Libraries = null;
SPNavigationNode Lists = null;
SPNavigationNode Discussions = null;
SPNavigationNodeCollection nodes = web.Navigation.QuickLaunch;
foreach (SPNavigationNode node in nodes)
{
if (node.Title == "Libraries")
Libraries = node;
if (node.Title == "Lists")
Lists = node;
if (node.Title == "Discussions")
Discussions = node;
}
Libraries.Children.AddAsFirst(GenerateNode(web, "Project Documents"));
Libraries.Children.AddAsLast(GenerateNode(web, "Project Photos"));
Libraries.Children.AddAsLast(GenerateNode(web, "Project Videos"));
Lists.Children.AddAsFirst(GenerateNode(web, "Project Calendar"));
Lists.Children.AddAsLast(GenerateNode(web, "External Links"));
Lists.Children.AddAsLast(GenerateNode(web, "Project Contacts"));
Lists.Children.AddAsLast(GenerateNode(web, "Announcements"));
Lists.Children.AddAsLast(GenerateNode(web, "Group Calendar"));
Discussions.Children.AddAsLast(GenerateNode(web, "Discussion Board"));
}
private static SPNavigationNode GenerateNode(SPWeb web, string p)
{
string url = web.Lists.TryGetList(p).DefaultViewUrl;
return new SPNavigationNode(p, url, true);
}
The end result will look like this:

Next time: Web Parts!
By: Philip Stathis