Click to Rate and Give Feedback
Community Content
In this section
Statistics Annotations (17)
Collapse All/Expand All Collapse All
This page is specific to
The 2007 product release

Other versions are also available for the following:
SPSite Class (Microsoft.SharePoint)
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.

Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Visual Basic (Declaration)
<UpgradableAttribute> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel:=True)> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel:=True)> _
Public Class SPSite
    Implements IDisposable
Visual Basic (Usage)
Dim instance As SPSite
C#
[UpgradableAttribute] 
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel=true)] 
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel=true)] 
public class SPSite : IDisposable

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:

Visual Basic
Dim oSiteCollection As New SPSite("Absolute_URL")
C#
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:

Visual Basic
Dim oSiteCollection As SPSite = SPContext.Current.Site
C#
SPSite oSiteCollection = SPContext.Current.Site;

Use the Sites property of the SPWebApplication class to return an SPSiteCollection object that represents the collection of site collections in a SharePoint Web application. Use an indexer to return a single site collection from the collection. For example, if the collection of site collections is assigned to a variable named oSiteCollections, use oSiteCollections[index] in C#, or oSiteCollections(index) in Visual Basic, where index is either the display name or the index number of the site collection in the collection.

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. 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:

Visual Basic
Using oSiteCollection = New SPSite("Absolute_URL")
    ...
End Using
C#
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.

For more information about good coding practices, see in Best Practices: Using Disposable Windows SharePoint Services Objects.

ImportantImportant:

If you install Infrastructure Update for Windows SharePoint Services 3.0 (KB951695) custom solutions may fail if they call the SharePoint object model while impersonation is suspended. If you use Windows authentication and your code calls the SharePoint object model from an IIS worker process, the request must impersonate the calling user’s identity. Windows SharePoint Services configures ASP.NET to impersonate the calling user automatically, but your code may work unexpectedly, or fail, if you suspend impersonation--for example, by calling the RevertToSelf function of the Windows API, or by calling the System.Security.Principal.WindowsIdentity.Impersonate method and passing IntPtr.Zero as the value of the user token parameter. Even if your code does not explicitly revert to self, it might be called by ASP.NET after it reverts to self, such as happens when implementing a virtual path provider; if your code does not impersonate the calling user, it might not function properly.

System.Object
  Microsoft.SharePoint.SPSite
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Community Content   What is Community Content?
Add new content RSS  Annotations
"Best Practices: Using Disposable Windows SharePoint Service Objects."      austegard ... Bob Mixon - SharePoint MVP   |   Edit   |   Show History
See http://msdn2.microsoft.com/en-us/library/aa973248.aspx
Tags What's this?: Add a tag
Flag as ContentBug
using statement (C#) lacks a closing paren?      John Dunagan ... Thomas Lee   |   Edit   |   Show History
using (SPSite oSiteCollection = new SPSite("Absolute_URL"))
{
...
}

?
PowerShell scripts for administering Site Collections      dunxd ... Thomas Lee   |   Edit   |   Show History
I've written some PowerShell scripts which allow you to centrally edit Site Collection Owners and Secondary Contacts, and Site Collection Administrators. http://www.dunxd.com/?p=693 and http://www.dunxd.com/?p=728 have full details. Very useful if you have many Site Collections on your farm.
Absolute URL must be in Alternate Access Mapping      Rodney Viana   |   Edit   |   Show History
This is for SPSite site = new SPSite(string Url);
Using an Absolute URL that is not in the Alternate Access Mapping (AAM) will yield an error even if this URL can be resolved to the site. For example, if you have a Server called sharepoint and the full name is sharepoint.domain.local and only sharepoint.domain.local is listed in AAM then this line:

SPSite sps = new SPSite("http://sharepoint")

will throw a System.IO.FileNotFoundException - The web application at http://sharepoint cannot be found....
Even when you can access the site colletion in your browser via http://sharepoint

While:
SPSite sps = new SPSite(http://sharepoint.domain.local) will work perfectly.

Rodney Viana (MSFT)
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker