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
    parentList.EnableFolderCreation
= true; 

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

   // Update
   listItem.Update();

   // Return
   return
listItem.Folder;
}

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

    // Return
    return
newFolder;
}

 

 

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
below.

public
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)
    try
    {
        listTemplateSP =
parentWeb.ListTemplates[listTemplate];
    }
    catch (System.ArgumentException)
    { // Try custom site templates
        SPListTemplateCollection customListTemplateCollection
= parentWeb.Site.GetCustomListTemplates(parentWeb);
        listTemplateSP =
customListTemplateCollection[listTemplate];
    } 

    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,
listTemplateSP); 

    // 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
Web

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
the
title, description, relative url name, locale id, template to be
used, etc. See http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spwebcollection.add.aspx
.

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

(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
addToSitesQuickLaunch)
{
    // 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
        parentWeb.Navigation.TopNavigationBar.AddAsLast(navigationNodeToAdd);
    }
    if (addToSitesQuickLaunch == true)
    { // Add to Quick launch sites
section
        foreach (SPNavigationNode
navigationNode in parentWeb.Navigation.QuickLaunch)
        { // Search sites
            if (navigationNode.Title ==
"Sites")
            { // Add here
                navigationNode.Children.AddAsLast(navigationNodeToAdd);
                // Stop
searching
                break;
            }
        }
    }
    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
.