SharePoint 2007 basics – Add a Folder

Folders can be added under a list or under another
folder and the way of adding in each case is very similar. Below another
example of sample functions.

public SPFolder AddFolder(SPList parentList, String title)
    // Enable
folder creation on the list
= true; 

    // Add folder
listItem = parentList.Folders.Add("", SPFileSystemObjectType.Folder);
= title;

   // Update

   // Return

public SPFolder
AddFolder(SPFolder folder, String title)
    // Add
newFolder = folder.SubFolders.Add(title); 

    // Return



SharePoint 2007 basics – Add a List

Adding a list is done very much in the same way as
a web. You just have to pay attention to were you should get your list template
from, because depending if it is a native or a custom list template the place where
to get it is different (
SPWeb.ListTemplates vs SPSite.GetCustomListTemplates). Of course that a list can only be added to a web.

So a function to add a list could be like the one

SPList AddList(SPWeb parentWeb, String title, String description, String
listTemplate, Boolean onQuickLaunch)
    // Get list template object
    SPListTemplate listTemplateSP = null; 

    // Try out-of-box list templates first (Not be best way of doing this)
        listTemplateSP =
    catch (System.ArgumentException)
    { // Try custom site templates
        SPListTemplateCollection customListTemplateCollection
= parentWeb.Site.GetCustomListTemplates(parentWeb);
        listTemplateSP =

    if (listTemplateSP == null)
        // Could not found
        throw new Exception("AddList: Could not found
list template"); 

    // Ok. Add list
    Guid newListGuid = parentWeb.Lists.Add(title, description,

    // Get new list
    SPList newList = parentWeb.Lists[newListGuid];
    newList.OnQuickLaunch = onQuickLaunch; 

    // Return
    return newList;


SharePoint 2007 basics – Add a Web

For my first SharePoint post I wanted to start by
some of the basics related to working with the SharePoint Object Model. For
sure there are other posts about this on the net, but I think that my blog
should also reflect my SharePoint learning experience, and maybe by doing this
I can show different ways accomplish similar things.

So let’s begin. How do you programmatically add Web’s,
List’s and Folder’s to SharePoint?

Add a

To add a web I used the SPWebs.Webs
property that returns a
SPWebCollection where
you can
Add new
Web’s. Of course that this means that you should add a web under another web
(the parent web).

So to add a web you have to give parameters like
title, description, relative url name, locale id, template to be
used, etc. See

With this other things that you have to think about
are related with the
web permissions and navigation

(Quick Launch and Top Link Bar’s).

I finish with a sample function that can be used as
a starting point

public SPWeb AddWeb(SPWeb parentWeb, String title,
String description, String relativeUrlName, UInt32 localeId, String template,
Boolean useUniquePermissions, Boolean addToTopNavigationBar, Boolean
    // Create
    SPWeb newWeb =
parentWeb.Webs.Add(relativeUrlName, title, description, localeId, template,
useUniquePermissions, false); 

    // Default uses parent navigation
    newWeb.Navigation.UseShared = true; 

    // Test links to add
    SPNavigationNode navigationNodeToAdd
= new SPNavigationNode(title, relativeUrlName, false);
    if (addToTopNavigationBar == true)
    { // Add
    if (addToSitesQuickLaunch == true)
    { // Add to Quick launch sites
        foreach (SPNavigationNode
navigationNode in parentWeb.Navigation.QuickLaunch)
        { // Search sites
            if (navigationNode.Title ==
            { // Add here
                // Stop
    return newWeb;

By the way, the last part to add the navigation
node to the Quick Launch Sites section is just an example that may not work if
you are using sites in languages other than English