SPSite.OpenWeb Method
| 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.
|
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;
}
}
- 3/30/2007
- Dave The SharePoint Engineer
- 12/20/2007
- Sparxz
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
- 12/20/2007
- Sparxz