Add method (String)

SPWebCollection.Add method (String)

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

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

public SPWeb Add(
	string 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.

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")

SPSite oSiteCollection = SPContext.Current.Site;
SPWebCollection collWebsites = oSiteCollection.AllWebs;
SPWeb oWebsite = collWebsites.Add("Website/Subsite/MyNewSubsite");

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.

© 2015 Microsoft