Compartilhar via


Establishing Site Context

To work with a deployment of Microsoft Windows SharePoint Services programmatically in an HTTP context, your code must first establish the site or site collection context for requests that are made to the server.

ASPX pages and Web applications

Create custom ASPX pages and Web applications in the following directory:

  • \\Local_Drive\Program Files\Common Files\Microsoft Shared\Web Server Extensions\60\TEMPLATE\LAYOUTS

Pages or applications located in this directory are accessible from all sites on the Web server by browsing to a URL that is in one of the following forms:

  • http://Server_Name/[sites]/[Site_Name]/[SubSite_Name]/[...]/_layouts/Page_Name.aspx
  • http://Server_Name/[sites]/[Site_Name]/[SubSite_Name]/[...]/_layouts/Application_Name/WebForm1.aspx

Establish the context of a request that is made to the server by using the GetContextSite or GetContextWeb method of the SPControl class to return an SPSite or SPWeb object that represents the context. If you create an ASPX page or Web application within the LAYOUTS directory and include the following snippet within the code, the page or application becomes available for use in any site or subsite on the Web server by browsing to the appropriate URL:

[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);

Both methods are used in the snippet for the sake of illustration, but they can also be used individually.

The functionality of a page or application located within the LAYOUTS directory is thus available to any site on the server that links to it. The following code, for example, displays the number of users on a site:

[Visual Basic .NET]

Dim site As SPWeb = SPControl.GetContextWeb(Context)
Response.Write(site.Users.Count.ToString())

[C#]

SPWeb site = SPControl.GetContextWeb(Context);
Response.Write(site.Users.Count.ToString());

When the page or application that contains this code is browsed to through a URL that combines the path for a specific site with the path for the page or application within the LAYOUTS directory, the number of users on the current site is displayed. For example, each of the following URLs could be used for an ASPX page:

  • http://Server_Name/_layouts/1033/MyApplication.aspx
  • http://Server_Name/sites/Site_Collection/_layouts/1033/MyApplication.aspx
  • http://Server_Name/Subsite/_layouts/1033/MyApplication.aspx

Variations of the SPControl methods can also be used depending on the need. For example, the following line returns the context of a specified site by using an indexer with the AllWebs property:

[Visual Basic .NET]

Dim site As SPWeb = SPControl.GetContextSite(Context).AllWebs("Site_Name")

[C#]

SPWeb site = SPControl.GetContextSite(Context).AllWebs["Site_Name"];

The next example returns the top-level Web site of the current site collection by using the RootWeb property:

[Visual Basic .NET]

Dim topSite As SPWeb = SPControl.GetContextSite(Context).RootWeb

[C#]

SPWeb topSite = SPControl.GetContextSite(Context).RootWeb;

The GetContextSite and GetContextWeb methods are also used in custom Web services and Web Parts to return the HTTP context.

Console applications

If you are writing code within a console application or custom executable application and you want to work with a specific site collection, you must instead use the SPSite constructor to instantiate an object that represents the site collection, as follows:

[Visual Basic .NET]

Dim siteCollection As New SPSite("http://Server_Name/[sites]/Site_Name")
Dim site As SPWeb

site = siteCollection.AllWebs("Site_Name")

[C#]

SPSite siteCollection = new SPSite("http://Server_Name/[sites]/Site_Name");
SPWeb site = siteCollection.AllWebs["Site_Name"];