共用方式為


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)
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, _
    WebTemplate As SPWebTemplate, _
    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 WebTemplate As SPWebTemplate
Dim useUniquePermissions As Boolean
Dim bConvertIfThere As Boolean
Dim returnValue As SPWeb

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

  • strDescription
    Type: System.String

    A string that contains the description.

  • nLCID
    Type: System.UInt32

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

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

Remarks

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.

Examples

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();
}
Public Overrides Sub FeatureActivated(ByVal properties As SPFeatureReceiverProperties)

    'Get the web site where the feature is activated.
    Dim parentWeb As SPWeb = TryCast(properties.Feature.Parent, SPWeb)
    If parentWeb Is Nothing Then
        Return
    End If

    Dim childWeb As SPWeb = Nothing
    Dim childName As String = "ourwiki"

    ' If a subsite by that name exists, open it.
    Dim webs As String() = parentWeb.Webs.Names
    If webs IsNot Nothing AndAlso Array.IndexOf(webs, childName) >= 0 Then
        childWeb = parentWeb.Webs(childName)
    End If

    ' Otherwise, create the subsite.
    If childWeb Is Nothing Then
        Dim title As String = "Wiki"
        Dim desc As String = "A place to capture knowledge."
        Dim lcid As UInteger = parentWeb.Language

        Dim templateName As String = "WIKI#0"

        childWeb = parentWeb.Webs.Add(childName, title, desc, lcid, _
                                        templateName, False, False)
    End If

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

    ' Get a collection of navigation nodes.
    Dim nodes As SPNavigationNodeCollection = Nothing

    If parentWeb.Navigation.UseShared Then
        ' 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
    End If

    ' Check for an existing link to the subsite.
    Dim node As SPNavigationNode = nodes.Cast(Of SPNavigationNode)().FirstOrDefault( _
        Function(n) n.Url.Equals(childWeb.ServerRelativeUrl))

    ' No link, so add one.
    If node Is Nothing Then

        ' Create the node.
        node = New SPNavigationNode(childWeb.Title, childWeb.ServerRelativeUrl)

        ' Add it to the collection.
        node = nodes.AddAsLast(node)
    End If

    childWeb.Dispose()
    parentWeb.Dispose()
End Sub

See Also

Reference

SPWebCollection Class

SPWebCollection Members

Add Overload

Microsoft.SharePoint Namespace