Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
SDK Documentation
SPControl Class
SPControl Methods
 GetContextSite Method

  Switch on low bandwidth view
Community Content
In this section
Statistics Annotations (0)
SPControl.GetContextSite Method (Microsoft.SharePoint.WebControls)
Returns the site collection that serves as context for the request.

Namespace: Microsoft.SharePoint.WebControls
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Visual Basic (Declaration)
Public Shared Function GetContextSite ( _
    context As HttpContext _
) As SPSite
Visual Basic (Usage)
Dim context As HttpContext
Dim returnValue As SPSite

returnValue = SPControl.GetContextSite(context)
C#
public static SPSite GetContextSite (
    HttpContext context
)

Parameters

context

A System.Web.HTTPContext object, which encapsulates the HTTP-specific context that is used by the server to process site requests.

Return Value

A Microsoft.SharePoint.SPSite object that represents the site collection.
  • The GetContextSite method is used in code running within an HTTP context that is integrated with a Windows SharePoint Services deployment. The code can be used in a Web Part, in a custom Web service (in _vti_bin), or in an ASPX page or Web Application (in _layouts). In each of these cases, the solution is available to any SharePoint site on the front-end Web server, and the GetContextSite method returns the SPSite object for the current HTTP request. For example, a custom ASPX page (_layouts/1033/MyItems.aspx) that returns all list items created by the user accessing the page would be available for use in all sites on the Web server by browsing to URLs like the following:

  • http:// Server_Name /_layouts/1033/MyItems.aspx

  • http:// Server_Name /sites/ Site_Collection /_layouts/1033/MyItems.aspx

  • http:// Server_Name / Subsite /_layouts/1033/MyItems.aspx

Do not use the Dispose or Close method of the SPSite class to close the object returned through the GetContextSite method. Instead let Windows SharePoint Services or your portal application manage the object.

For more information, see Establishing Site Context .

The following code example uses the GetContextSite method in conjunction with the GetContextWeb method to return both a site collection object and a site object on an .aspx page.

[Visual Basic .NET]
<% Dim siteCollection As SPSite = SPControl.GetContextSite(Context)
Dim site As SPWeb = SPControl.GetContextWeb(Context) %>
[C#]
<% SPSite siteCollection = SPControl.GetContextSite(Context); 
   SPWeb site = SPControl.GetContextWeb(Context); %>
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
WebParts need to be signed      User77   |   Edit   |   Show History
After some tests I found out that WebParts for SharePoint 2007 need to be signed to use GetContextSite.
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