Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
SDK Documentation
SPSite Class
SPSite Methods
OpenWeb Method

  Switch on low bandwidth view
Community Content
In this section
Statistics Annotations (2)
SPSite.OpenWeb Method (Microsoft.SharePoint)
Returns the specified Web site from the site collection.
Name Description
SPSite.OpenWeb ()
Returns the site that is associated with the URL that is used in an SPSite constructor.
SPSite.OpenWeb (Guid)
Returns the site with the specified GUID.
SPSite.OpenWeb (String)
Returns the site that is located at the specified server-relative or site-relative URL.
SPSite.OpenWeb (String, Boolean)
Returns the site that is located at the specified server-relative or site-relative URL based on a Boolean value that specifies whether the exact URL must be supplied.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Using SPsite.OpenWeb() to create a new site under and existing site.      borading_king ... Sparxz   |   Edit   |   Show History

I don't know about most developers who are new to a topic but when I am learning something new, I would prefer to see lots of examples on how to use a method (and copy it into my own project) rather than to read all about it from top to bottom. Therefore this code sample shows a simple use of the OpenWeb() method and how it could be used to create a new subsite called "news" under and existing site called "sections":

(This will create a subsite based on the CMSPUBLISHING#0 template which is installed as part of Sharepoint 2007).

using (SPSite topLevelSite = new SPSite(http://www.yourwebsite.co.uk/sections))

{

using (SPWeb topLevelSiteWeb = topLevelSite.OpenWeb())

{

topLevelSiteWeb.AllowUnsafeUpdates = true;
SPWebCollection subSites = topLevelSiteWeb.Webs;

SPWeb newSubWeb = subSites.Add("news", "Title of your news site", "Description of your news site", 1033, "CMSPUBLISHING#0", false, false);

newSubWeb.Dispose(); //Clean up SPWeb memory resources.

topLevelSiteWeb.AllowUnsafeUpdates = false;

}

}

Tags What's this?: Add a tag
Flag as ContentBug
Best Practices: Using Disposable Windows SharePoint Services Objects      Sparxz   |   Edit   |   Show History

When you use SPSite.OpenWeb, you need to use 'using' or dispose to clean-up. Refer to http://msdn2.microsoft.com/en-us/library/aa973248.aspx

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker