SPSite.OpenWeb Method (String)
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Parameters
- strUrl
A string that contains either the server-relative or site-relative URL of the site. A server-relative URL begins with a forward slash ("/"), while a site-relative URL does not begin with a forward slash.
Return Value
An SPWeb object that represents the site.If a site-relative URL is passed to the OpenWeb method, the URL is relative to the highest-level site that is represented in the URL that is passed to an SPSite constructor. For example, assume that the following sites exist in the Windows SharePoint Services deployment:
-
http://Server/Subsite1/Subsite2
-
http://Server/sites/SiteCollection/Subsite3
The following table shows the results of passing various URLs to an SPSite constructor and the OpenWeb method.
|
SPSite Constructor |
OpenWeb Method |
Site Returned |
|---|---|---|
|
There is no parameter (OpenWeb method overload) |
http://Server |
|
|
http://Server/Subsite1/Subsite2/Folder/File |
There is no parameter (OpenWeb method overload) |
http://Server/Subsite1/Subsite2 |
|
http://Server/Subsite1/Subsite2/Folder/File |
/ |
http://Server |
|
http://Server/Subsite1/Subsite2/Folder/File |
/Subsite1 |
http://Server/Subsite1 |
|
http://Server/Folder/File |
Subsite1/Subsite2 |
http://Server/Subsite1/Subsite2 |
|
http://Server/Folder/File |
/SiteCollection/Subsite3 |
Error: There is no site named /SiteCollection/Subsite3 |
|
http://Server/Folder/File |
/Subsite4 |
Error: There is no site named /Subsite4 |
|
http://Server/sites/SiteCollection/Subsite3/Folder/File |
No parameter (OpenWeb method overload) |
http://Server/sites/SiteCollection/Subsite3 |
|
http://Server/sites/SiteCollection/Folder/File |
/sites/SiteCollection/Subsite3 |
http://Server/sites/SiteCollection/Subsite3 |
|
http://Server/sites/SiteCollection/Folder/File |
Subsite3 |
http://Server/sites/SiteCollection/Subsite3 |
|
http://Server/sites/SiteCollection/Folder/File |
/ |
Error: Invalid URL |
|
http://Server/sites/SiteCollection/Subsite3/Folder/File |
/sites/SiteCollection |
http://Server/sites/SiteCollection |
For information about the forms of URLs that are used in Windows SharePoint Services, see Forms of URL Strings.
The following code example displays the URL for a specified Web site in a console application using a site-relative URL for the OpenWeb method. The example assumes the existence of a Web site located at http://MyServer/MyWebSite/MySubSite.
string strUrl = "http://MyServer/MyWebSite/Lists/Announcements/AllItems.aspx"; using (SPSite oSiteCollection = new SPSite(strUrl)) { using(SPWeb oWebsite = oSiteCollection.OpenWeb("MyWebSite/MySubSite")) { Console.WriteLine("Website: " + oWebsite.Url); } }
Note: |
|---|
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. |
Note: