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
.

LEAVE A REPLY

Please enter your comment!
Please enter your name here