Partager via


How to: Create or Delete a Site or a Site Collection

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

To create a site, use one of the Add methods of the SPWebCollection class. To create a subsite beneath a site, use the Webs property of the SPWeb class to return the collection of subsites and then call one of the Add methods for the collection.

The following example creates a new subsite that is based on the template of the current site and on information gathered from three text boxes. The text boxes specify the name to use in the new URL, the title to use for the site, and a description for the site.

Note

The code examples in this topic use members of the Microsoft.SharePoint.SPContext class to obtain the current site collection, Web site, or list. Outside of an HTTP context, such as in a console application or a Windows application, you obtain references to key objects with a different method. For more information, see Getting References to Sites, Web Applications, and other Key Objects.

Dim mySite As SPWeb = SPContext.Current.Web
Dim subSites As SPWebCollection = mySite.Webs
Dim currentTemplate As String = mySite.WebTemplate

Dim siteUrl As String = TextBox1.Text.ToString()
Dim siteTitle As String = TextBox2.Text.ToString()
Dim siteDescription As String = TextBox3.Text.ToString()

subSites.Add(siteUrl, siteTitle, siteDescription, 
   Convert.ToUInt32(1033), currentTemplate, True, False)
SPWeb mySite = SPContext.Current.Web;
SPWebCollection subSites = mySite.Webs;
string currentTemplate = mySite.WebTemplate;

string siteUrl = TextBox1.Text.ToString();
string siteTitle = TextBox2.Text.ToString();
string siteDescription = TextBox3.Text.ToString();

subSites.Add(siteUrl, siteTitle, siteDescription, 1033, 
   currentTemplate, true, false); 

The previous example requires a using directive (Imports in Visual Basic) for the Microsoft.SharePoint namespace.

In the example, the WebTemplate property of the SPWeb class returns the name of the current site definition, which is passed as a parameter of the Add method. In addition, three parameters for this method pass the information gathered from the three text boxes. The three other parameters specify 1033 as the locale folder, true to create a site with unique permissions, and false to convert any existing Web site at the same location to a SharePoint site.

To delete a site, use the Delete method of the SPWeb class or the Delete method of the SPWebCollection class.

The following example assumes the use of a text box to specify the URL of a site to delete, and uses the Delete method of the SPWebCollection class to delete the site.

Dim deleteSite As String = TextBox1.Text.ToString()

Dim mySite As SPSite = SPContext.Current.Site
Dim sites As SPWebCollection = mySite.AllWebs

sites.Delete(deleteSite)
string deleteSite = TextBox1.Text.ToString();

SPSite mySite = SPContext.Current.Site;
SPWebCollection sites = mySite.AllWebs;

sites.Delete(deleteSite); 

The previous example requires a using directive (Imports in Visual Basic) for the Microsoft.SharePoint namespace.

In the example, the AllWebs property of the SPSite class returns the collection of all sites within the current site collection.

To create a site collection, use the Sitesproperty of the SPVirtualServer class to return the collection of site collections on the virtual server and use one of the Add methods of the SPSiteCollection class.

The following example creates a site collection within the collection of site collections of the current SharePoint Web application.

Dim webApplication As SPWebApplication = SPContext.Current.Site.WebApplication
Dim siteCollections As SPSiteCollection = webApplication.Sites

siteCollections.Add("http://Server_Name/sites/Site_Collection_Name", 
   "User_Name", "User_Email")
SPWebApplication webApplication = SPContext.Current.Site.WebApplication;
SPSiteCollection siteCollections = webApplication.Sites;

siteCollections.Add("http://Server_Name/sites/Site_Collection_Name",
"User_Name","User_Email ");

The previous example requires a using directive (Imports in Visual Basic) for the Microsoft.SharePoint.Administration namespace.

The example instantiates the SPGlobalAdmin class in order to call the OpenVirtualServer method and return the virtual server with the specified URI.

To delete a site collection from a virtual server requires security validation that uses the AdminFormDigest property of the SPGlobalAdmin class to insert a message digest on the page in the browser. You can do this by registering the digest as a hidden field through the RegisterHiddenField method of the System.Web.UI.Page class. In addition, use the RequestFromAdminPort field of the SPGlobalAdmin class to specify that the context of the request is through the administrative port.

The following example uses the Page_Load event to include an administrative form digest on the page and to establish the context of the request.

Private globalAdmin As New SPGlobalAdmin()

Private Sub Page_Load(sender As Object, e As System.EventArgs)

    Context.Items(SPGlobalAdmin.RequestFromAdminPort) = True
    Page.RegisterHiddenField("__REQUESTDIGEST", 
        globalAdmin.AdminFormDigest)

End Sub 'Page_Load

Private Sub Button1_Click(sender As Object, e As System.EventArgs)

    Dim globalAdmin As New SPGlobalAdmin()
    Dim uri As New System.Uri("http://Server_Name")
    Dim vServer As SPVirtualServer = globalAdmin.OpenVirtualServer(uri)
    Dim siteCollections As SPSiteCollection = vServer.Sites

    siteCollections.Delete("sites/Site_Collection")

End Sub 'Button1_Click
private SPGlobalAdmin globalAdmin = new SPGlobalAdmin();

private void Page_Load(object sender, System.EventArgs e)
{
    Context.Items[SPGlobalAdmin.RequestFromAdminPort] = true;
    Page.RegisterHiddenField("__REQUESTDIGEST", 
        globalAdmin.AdminFormDigest);
}

private void Button1_Click(object sender, System.EventArgs e)
{
    SPGlobalAdmin globalAdmin = new SPGlobalAdmin();
    System.Uri uri = new System.Uri("http://Server_Name");
    SPVirtualServer vServer = globalAdmin.OpenVirtualServer(uri);
    SPSiteCollection siteCollections = vServer.Sites;

    siteCollections.Delete("sites/Site_Collection");
}

The previous example requires a using directive (Imports in Visual Basic) for the Microsoft.SharePoint.Administration namespace.

See Also

Reference

Microsoft.SharePoint

Concepts

Working with List Objects and Collections

Getting Started with Programmatically Customizing a SharePoint Web Site in Visual Studio

Security Validation and Making Posts to Update Data

Elevation of Privilege