Share via


SPSite.OpenWeb Method (String)

Returns the Web site that is located at the specified server-relative or site-relative URL.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
<ClientCallableExceptionConstraintAttribute(FixedId := "System.ArgumentException$0", ErrorType := GetType(ArgumentException),  _
    ErrorCode := , Condition := "The specified Url MUST be a valid web url.")> _
<ClientCallableMethodAttribute(ReturnObjectIdentity := True, CacheReturnValue := True,  _
    CaseInsensitiveCache := True)> _
Public Function OpenWeb ( _
    strUrl As String _
) As SPWeb
'Usage
Dim instance As SPSite
Dim strUrl As String
Dim returnValue As SPWeb

returnValue = instance.OpenWeb(strUrl)
[ClientCallableExceptionConstraintAttribute(FixedId = "System.ArgumentException$0", ErrorType = typeof(ArgumentException), 
    ErrorCode = , Condition = "The specified Url MUST be a valid web url.")]
[ClientCallableMethodAttribute(ReturnObjectIdentity = true, CacheReturnValue = true, 
    CaseInsensitiveCache = true)]
public SPWeb OpenWeb(
    string strUrl
)

Parameters

  • strUrl
    Type: System.String

    A string that contains either the server-relative or site-relative URL of the Web site or of an object within the Web site. A server-relative URL begins with a forward slash ("/"), while a site-relative URL does not begin with a forward slash.

Return Value

Type: Microsoft.SharePoint.SPWeb
An SPWeb object that represents the Web site.

Remarks

If a site-relative URL is passed to the OpenWeb method, the URL is relative to the highest-level Web site that is represented in the URL that is passed to an SPSite constructor. For example, assume that the following sites exist in the SharePoint Foundation deployment:

  • https://Server/Subsite1/Subsite2

  • https://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

https://Server/Folder/File

There is no parameter (OpenWeb method overload)

https://Server

https://Server/Subsite1/Subsite2/Folder/File

There is no parameter (OpenWeb method overload)

https://Server/Subsite1/Subsite2

https://Server/Subsite1/Subsite2/Folder/File

/

https://Server

https://Server/Subsite1/Subsite2/Folder/File

/Subsite1

https://Server/Subsite1

https://Server/Folder/File

Subsite1/Subsite2

https://Server/Subsite1/Subsite2

https://Server/Folder/File

/SiteCollection/Subsite3

Error: There is no site named /SiteCollection/Subsite3

https://Server/Folder/File

/Subsite4

Error: There is no site named /Subsite4

https://Server/sites/SiteCollection/Subsite3/Folder/File

No parameter (OpenWeb method overload)

https://Server/sites/SiteCollection/Subsite3

https://Server/sites/SiteCollection/Folder/File

/sites/SiteCollection/Subsite3

https://Server/sites/SiteCollection/Subsite3

https://Server/sites/SiteCollection/Folder/File

Subsite3

https://Server/sites/SiteCollection/Subsite3

https://Server/sites/SiteCollection/Folder/File

/

Error: Invalid URL

https://Server/sites/SiteCollection/Subsite3/Folder/File

/sites/SiteCollection

https://Server/sites/SiteCollection

For information about the forms of URLs that are used in SharePoint Foundation, see Describing Forms of URL Strings.

Examples

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.

Dim strUrl As String = "http://MyServer/MyWebSite/Lists/Announcements/AllItems.aspx"

Using oSiteCollection As New SPSite(strUrl)
    Using oWebsite As SPWeb = oSiteCollection.OpenWeb("MyWebSite/MySubSite")
        Console.WriteLine(("Website: " + oWebsite.Url))
    End Using
End Using
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 Disposing Objects.

See Also

Reference

SPSite Class

SPSite Members

OpenWeb Overload

Microsoft.SharePoint Namespace