SPWebCollection.Add Method (String) (Microsoft.SharePoint)
Creates a Web site object with the specified Web site-relative URL.

Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Syntax

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

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

Parameters

strWebUrl

A string that contains the new Web site URL relative to the root Web site in the site collection. For example, to create a Web site at http://MyServer/sites/MySiteCollection/MyNewWebsite, specify MyNewWebsite, or to create a Web site one level lower at http://MyServer/sites/MySiteCollection/Website/MyNewWebsite, specify Website/MyNewWebsite.

Return Value

An SPWeb object that represents the Web site.
Example

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

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

Dim mySite As SPWeb = subSites.Add("MyNewWebsite")
mySite.Dispose()

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

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 Best Practices: Using Disposable Windows SharePoint Services Objects.

See Also

Tags :


Page view tracker