共用方式為


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)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
Public Function Add ( _
    strWebUrl As String, _
    strTitle As String, _
    strDescription As String, _
    nLCID As UInteger, _
    strWebTemplate As String, _
    useUniquePermissions As Boolean, _
    bConvertIfThere As Boolean _
) As SPWeb
'Usage
Dim instance As SPWebCollection
Dim strWebUrl As String
Dim strTitle As String
Dim strDescription As String
Dim nLCID As UInteger
Dim strWebTemplate As String
Dim useUniquePermissions As Boolean
Dim bConvertIfThere As Boolean
Dim returnValue As SPWeb

returnValue = instance.Add(strWebUrl, _
    strTitle, strDescription, nLCID, _
    strWebTemplate, useUniquePermissions, _
    bConvertIfThere)
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.

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

Remarks

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.

Examples

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("https://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();
        }
    }
}
Imports System
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Navigation

Module ConsoleApp

    Sub Main()

        Using site As New SPSite("https://localhost")

            Dim parentWebName As String = "Team"

            Using parentWeb As SPWeb = site.OpenWeb(parentWebName)
                Dim webTitle As String = "Our Documents"
                Dim webDesc As String = "A site created by a console application."
                Dim webName As String = "ourdocs"
                Dim webUrl As String = [String].Format("{0}/{1}", parentWebName, webName)
                Dim webLcid As UInteger = parentWeb.Language

                ' Name value for the Document Workspace template.
                Dim webTemplateName As String = "STS#2"

                Dim newWeb As SPWeb = Nothing

                ' Create the new web.
                Try
                    newWeb = site.AllWebs.Add(webUrl, webTitle, _
                                              webDesc, webLcid, _
                                              webTemplateName, _
                                              False, False)
                Catch ex As ArgumentException
                    Console.WriteLine(ex.Message)
                End Try

                ' Add a link to the new web on the RootWeb's topnav bar.
                If newWeb IsNot Nothing Then
                    ' 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.
                    Dim node As New SPNavigationNode(newWeb.Title, newWeb.ServerRelativeUrl)

                    ' Find out if the parent inherits links.
                    Dim parentInheritsTopNav As Boolean = newWeb.ParentWeb.Navigation.UseShared

                    If parentInheritsTopNav Then
                        ' 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)
                    End If

                    newWeb.Dispose()
                End If

            End Using
        End Using

        Console.Write(vbCrLf & "Press ENTER to continue....")
        Console.Read()
    End Sub

End Module

See Also

Reference

SPWebCollection Class

SPWebCollection Members

Add Overload

Microsoft.SharePoint Namespace