Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

SPWebCollection.Add method (String, String, String, UInt32, SPWebTemplate, Boolean, Boolean)

Creates an SPWeb object with the specified site-relative URL, title, description, locale ID, and site definition or site template object.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
public SPWeb Add(
	string strWebUrl,
	string strTitle,
	string strDescription,
	uint nLCID,
	SPWebTemplate WebTemplate,
	bool useUniquePermissions,
	bool bConvertIfThere
)

Parameters

strWebUrl
Type: System.String

A string that contains the new website URL relative to the root website in the site collection. For example, to create a website at http://MyServer/sites/MySiteCollection/MyNewWebsite, specify MyNewWebsite, or to create a website one level lower at http://MyServer/sites/MySiteCollection/Website/MyNewWebsite, specify Website/MyNewWebsite.

strTitle
Type: System.String

A string that contains the title.

strDescription
Type: System.String

A string that contains the description.

nLCID
Type: System.UInt32

An unsigned 32-bit integer that specifies the locale ID.

WebTemplate
Type: Microsoft.SharePoint.SPWebTemplate

An SPWebTemplate object that represents the site definition or site template.

useUniquePermissions
Type: System.Boolean

true to create a subsite that does not inherit permissions from another site; otherwise, false.

bConvertIfThere
Type: System.Boolean

true to convert an existing folder of the same name to a SharePoint site. false to throw an exception that indicates that a URL path with the specified site name already exists.

Return value

Type: Microsoft.SharePoint.SPWeb
An SPWeb object that represents the website.

By default, a global site template (GLOBAL#0) is added to all site definitions. You cannot explicitly create a site based on the global site template.

The following example is part of a larger project that uses a Web-scoped Feature to create a subsite below the website where the Feature is activated. The Feature includes an event handler that implements the SPFeatureReceiver class. Code to create the subsite is in the feature receiver's FeatureActivated method.

After creating the new website, the example code adds a link to it to the parent site's top link bar or, if the parent inherits links, to the parent site's Quick Launch.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    // Get the web site where the feature is activated.
    SPWeb parentWeb = properties.Feature.Parent as SPWeb;
    if (parentWeb == null)
        return;

    SPWeb childWeb = null;
    string childName = "ourwiki";

    // If a subsite by that name exists, open it.
    string[] webs = parentWeb.Webs.Names;
    if (webs != null && Array.IndexOf(webs, childName) >= 0)
    {
        childWeb = parentWeb.Webs[childName];
    }

    // Otherwise, create the subsite.
    if (childWeb == null)
    {
        string title = "Wiki";
        string desc = "A place to capture knowledge.";
        uint lcid = parentWeb.Language;

        string templateName = "WIKI#0";

        childWeb = parentWeb.Webs.Add(childName, title, desc, lcid, templateName, false, false);
    }

    // Let the subsite use the parent site's top link bar.
    childWeb.Navigation.UseShared = true;

    // Get a collection of navigation nodes.
    SPNavigationNodeCollection nodes = null;
    if (parentWeb.Navigation.UseShared)
    {
        // Parent site does not have its own top link bar
        // so use the parent's Quick Launch.
        nodes = parentWeb.Navigation.QuickLaunch;
    }
    else
    {
        // Parent site has its own top link bar,
        // so use it.
        nodes = parentWeb.Navigation.TopNavigationBar;
    }

    // Check for an existing link to the subsite.
    SPNavigationNode node = nodes
        .Cast<SPNavigationNode>()
        .FirstOrDefault(n => n.Url.Equals(childWeb.ServerRelativeUrl));

    // No link, so add one.
    if (node == null)
    { 
        // Create the node.
        node = new SPNavigationNode(childWeb.Title, childWeb.ServerRelativeUrl);

        // Add it to the collection.
        node = nodes.AddAsLast(node);
    }

    childWeb.Dispose();
    parentWeb.Dispose();
}
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.