Search This Blog

2009-04-30

SPSite and SPContext

SPSite:
Represents a collection of sites in a Web application, including a top-level Web site and all its subsites. Each SPSite object, or site collection, is represented within an SPSiteCollection object that consists of the collection of all site collections in the Web application.

To instantiate an SPSite object for a specific site collection on an ASP.NET page, or for a specific site collection within a console application, use the SPSite constructor as follows:
SPSite oSiteCollection = new SPSite("Absolute_URL");
Within an ASP.NET application, you can use the Site property of the SPContext class to return an SPSite object that represents the current site collection, as follows:
SPSite oSiteCollection = SPContext.Current.Site;
If you create your own SPSite object, you can use the Dispose method to close the object. You can also instead implement a using statement so that the .NET Framework common language runtime (CLR) automatically releases the memory that is used to store the site collection as follows:

using (SPSite oSiteCollection = new SPSite("Absolute_URL")
{
...
}

However, if you have a reference to a shared resource, such as when the object is provided by the GetContextSite method in a Web Part, do not use either method to close the object. Using either method on a shared resource causes an Access Violation error to occur. In scenarios where you have a reference to a shared resource, instead let Windows SharePoint Services or your portal application manage the object.

Note:-
Some Important properties are AllWebs ,WebApplication ,Owner etc


SPContext :-
Represents the context of an HTTP request in Windows SharePoint Services

Use the SPContext class to return context information about such objects as the current Web application, site collection, site, list, or list item.
The following examples illustrate how to use properties of the SPContext class to return the current list, site, and site collection.

SPList oListCur = SPContext.Current.List;
SPWeb oWeb = SPContext.Current.Web;
SPSite oSite = SPContext.Current.Site;
SPWebApplication oWebApplicationCur = SPContext.Current.Site.WebApplication;
Cast the value of the Item property as an SPListItem object to return the current list item, as follows:
SPListItem item = (SPListItem)SPContext.Current.Item;

Properties:-

Name
Description
Current
Gets the context of the current HTTP request in Windows SharePoint Services.
Fields
Gets the fields metadata associated with the item or content type of the Windows SharePoint Services context.
File
Gets the file that is associated with the list item object of the given Windows SharePoint Services context.
Item
Gets either the SPListItem object that is determined by the given list and item ID, or the SPItem object that is set when the SPContext object is created.
ItemId
Gets the ID of the item that is associated with the Windows SharePoint Services context.
List
Gets the list that is associated with the Windows SharePoint Services context.
ListId
Gets the GUID of the list that is associated with the Windows SharePoint Services context.
ListItem
Gets the list item associated with the Windows SharePoint Services context.
ListItemServerRelativeUrl
Gets or sets the server-relative URL for the list item in the current HTTP context of Windows SharePoint Services.
Site
Gets the site collection that is associated with the Windows SharePoint Services context.
SiteFeatures
Gets the activated site collection features of the Windows SharePoint Services context.
Web
Gets the Web site that is associated with the Windows SharePoint Services context.
WebFeatures
Gets the activated site features of the Windows SharePoint Services context.

No comments: