Export (0) Print
Expand All
2 out of 2 rated this helpful - Rate this topic

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

Creates a website with the specified site-relative URL, title, description, locale ID, and site definition or site template name.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
public SPWeb Add(
	string strWebUrl,
	string strTitle,
	string strDescription,
	uint nLCID,
	string strWebTemplate,
	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

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

strWebTemplate
Type: System.String

A string that contains the name of the site definition configuration or site template.

By default, a global site template (GLOBAL#0) is added to all site definitions. You cannot explicitly create a site based on a global 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 new website.

The following table shows the values for the default site definition configurations that are included in an installation of Microsoft SharePoint Foundation.

Value

Site Definition

STS#0

Team Site

STS#1

Blank Site

STS#2

Document Workspace

MPS#0

Basic Meeting Workspace

MPS#1

Blank Meeting Workspace

MPS#2

Decision Meeting Workspace

MPS#3

Social Meeting Workspace

MPS#4

Multipage Meeting Workspace

BLOG#0

Blog

SGS#0

Basic Group Work Site

SGS#1

Blank Group Work Site

WIKI#0

Wiki

Note that WIKI provides a site definition configuration for legacy wiki sites that were originally created in an earlier version of SharePoint Foundation. Because standard site pages on SGS sites are wiki-enabled pages, you do not need to create sites that are specifically for wikis.

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

The following example is a console application that creates a new website as a child of another site and uses the Document Workspace template for the new site. After creating the new website, the application adds a link to it to the parent site's top link bar or, if the parent inherits links, to the top link bar for the root website in the collection.

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Navigation;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://localhost"))
            {
                string parentWebName = "Team";

                using (SPWeb parentWeb = site.OpenWeb(parentWebName))
                {
                    string webTitle = "Our Documents";
                    string webDesc = "A site created by a console application.";
                    string webName = "ourdocs";
                    string webUrl = String.Format("{0}/{1}", parentWebName, webName);
                    uint webLcid = parentWeb.Language;

                    // Name value for the Document Workspace template.
                    string webTemplateName = "STS#2";

                    SPWeb newWeb = null;

                    // Create the new web.
                    try
                    {
                        newWeb = site.AllWebs.Add(webUrl, webTitle, webDesc, webLcid, webTemplateName, false, false);
                    }
                    catch (ArgumentException ex)
                    {
                        Console.WriteLine(ex.Message);
                    }

                    // Add a link to the new web on the RootWeb's topnav bar.
                    if (newWeb != null)
                    {
                        // Set the new web's top link bar to inherit links from its parent web.
                        newWeb.Navigation.UseShared = true;

                        // Create a link pointing to the new web.
                        SPNavigationNode node = new SPNavigationNode(newWeb.Title, newWeb.ServerRelativeUrl);

                        // Find out if the parent inherits links.
                        bool parentInheritsTopNav = newWeb.ParentWeb.Navigation.UseShared;

                        if (parentInheritsTopNav)
                        {
                            // Add the link to the top link bar on the root web.
                            site.RootWeb.Navigation.TopNavigationBar.AddAsLast(node);
                        }
                        else
                        {
                            // Add the link to the top link bar on the parent web.
                            newWeb.ParentWeb.Navigation.TopNavigationBar.AddAsLast(node);
                        }

                        newWeb.Dispose();
                    }
                }
            }
            Console.Write("\nPress ENTER to continue....");
            Console.ReadLine();
        }
    }
}

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.