SPWebCollection.Add method (String)

Creates an SPWeb object with the specified website-relative URL.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'Declaration
Public Function Add ( _
    strWebUrl As String _
) As SPWeb
'Usage
Dim instance As SPWebCollection
Dim strWebUrl As String
Dim returnValue As SPWeb

returnValue = instance.Add(strWebUrl)
public SPWeb Add(
    string strWebUrl
)

Parameters

  • strWebUrl
    Type: System.String

    A string that specifies the URL for the new website. If the SPWebCollection object represents the websites within a site collection, the URL is server-relative and begins with a forward slash (for example, "/sites/MySitecollection/MyNewWebSite"). If the SPWebCollection object represents the child websites directly beneath a website, the URL is relative to the website and does not begin with a forward slash (for example, "MyNewWebSite").

Return value

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

Examples

The following code example creates a website within the current site collection.

Dim siteCollection As SPSite = SPControl.GetContextSite(Context)
Dim subSites As SPWebCollection = siteCollection.AllWebs

Dim mySite As SPWeb = subSites.Add("MyNewWebsite")
mySite.Dispose()
SPSite oSiteCollection = SPContext.Current.Site;
SPWebCollection collWebsites = oSiteCollection.AllWebs;
SPWeb oWebsite = collWebsites.Add("Website/Subsite/MyNewSubsite");
oWebsite.Dispose();

Note

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects.

See also

Reference

SPWebCollection class

SPWebCollection members

Add overload

Microsoft.SharePoint namespace