SharePoint Events

  5/13/2013 - Conference: SharePoint Summit 2013
  5/21/2013 - Webcast: SharePoint 2013 and ECM: Content Migration and Storage
  5/22/2013 - Webcast: Managing CAD in SharePoint
  5/23/2013 - Webcast: SharePoint Document Automation and E-Forms for Financial Services
  5/24/2013 - Webcast: What's New in Search for SharePoint 2013

 SharePoint Videos

  Why SharePoint 2013
  SharePoint 2013 Launch
  SharePoint 2013 Migration and Governance
  SharePoint 2013 and Enterprise Content Management
  Top Benefits of SharePoint 2013
  What's New in Business Intelligence in Office and SharePoint 2013
  SharePoint and Office 2013 Integration
  SharePoint 2013 Infrastructure Preview
  SharePoint, Lync, and, Exchange in the Cloud with Office 365
  Advanced Reporting in SharePoint with Microsoft Power View

 Archives

Opening SharePoint Links in a new windowUse SHIFT+ENTER to open the menu (new window).
Mail Enabled Lists vs. The Missing Windows 2008 POP3/IMAP Server Use SHIFT+ENTER to open the menu (new window).
7 Tools for SharePoint DevelopersUse SHIFT+ENTER to open the menu (new window).
Public Facing Masterpage TechniquesUse SHIFT+ENTER to open the menu (new window).
How to Quickly Deploy and Activate a Timer Service to Your Site CollectionUse SHIFT+ENTER to open the menu (new window).
Custom SharePoint Master Page Feature with WSP BuilderUse SHIFT+ENTER to open the menu (new window).
Date Math with InfoPathUse SHIFT+ENTER to open the menu (new window).
Enterprise Search Tricks and Tips Part 1Use SHIFT+ENTER to open the menu (new window).
Populating Word Documents With SharePoint Data. Try The DIP!Use SHIFT+ENTER to open the menu (new window).
Programmatic Deep Dive into Blank SharePoint Lookup ColumnsUse SHIFT+ENTER to open the menu (new window).
1 - 10 Next
SharePoint 2010 Site Templating using only C# (part 1): Feature Activation, quick launch links and tricky List Templates

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:

SharePoint_Blog_SharePoint_2010_Site_Templating_using_only_C

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

SharePoint_Blog_SharePoint_2010_Site_Templating_using_only_C

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?

SharePoint_Blog_SharePoint_2010_Site_Templating_using_only_C

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:

SharePoint_Blog_SharePoint_2010_Site_Templating_using_only_C

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:

SharePoint_Blog_SharePoint_2010_Site_Templating_using_only_C

Next time: Web Parts!

By: Philip Stathis

        

Comments

There are no comments yet for this post.
Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Your Name *


e-mail address *


Website (optional)


Comment *


Attachments

 Subscribe

  GigWerks RSS  Gig Werks Mailing List 

 Contact Us

 Connect

 Resources

  On Demand SharePoint Webcast Recordings
  Upcoming Webinars
  SharePoint Resources
  Business Intelligence Resources
  Gig Werks Website



©2009 Gig Werks. All rights reserved. Privacy Policy