Navigate Up
Sign In
Supporters of Developer
Web

SharePoint 2010 Site Templating Using Only C# - Part 1: Feature Activation, Quick Launch Links and Tricky List Templates

Item is currently unrated. Press SHIFT+ENTER to rate this item.1 star selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+ESCAPE to leave rating submit mode.2 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.3 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.4 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.5 stars selected. Press SHIFT+ENTER to submit. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.

You may also be interested in: O'Reilly - SharePoint 2010 at Work

 

Editor's note: Contributor Philip Stathis is a SharePoint developer at Gig Werks. Follow him @philipstathis

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:

2012-06-15-SiteTemplating-Part01-01.png

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

2012-06-15-SiteTemplating-Part01-02.png

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?

2012-06-15-SiteTemplating-Part01-03.png

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:

2012-06-15-SiteTemplating-Part01-04.png

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:

2012-06-15-SiteTemplating-Part01-05.png

Nex time: Web Parts!

Categories:  

Comments

Notify me of comments to this article

E-mail:
   

Add Comment

Title:

 
Comment:
Email:

   


Name:

 
Url: