This article presents the SharePoint 2010 (SP10) Objects Hierarchy, their relationships and how to use them programmatically.
 |
| Figure 1: Hierarchy of the core objects in SharePoint 2010. |
I- The SPFarm Object is the highest object in the hierarchy; it is a class and global static object.
It has no constructor and must be set to a static object (for example SPFarm.Local). The SPFarm has
properties, methods and event that impacts all the farm settings, for exampleServices, OnBackup event.
To create a SPFarm object and set it to the local farm write:
###########################
SPFarm myFarm = SPFarm.Local;
II- The SPService Object is a base class and represents a service operating on the entire farm,
for example: Access service, Excel calculation service, Business Data connectivity service, User Profile
service, SPWebService. A Web application always belongs to a SPWebService.
The sample code shows how to identify the SPWebServices in the farm services. It also check if the user has administrator rights:
###########################
SPFarm myFarm = SPFarm.Local;
If (myFarm.CurrentUserIsAdministrator)
{
foreach (SPService srvc in myFarm.Services)
{
if (srvc is SPWebService)
{
SPWebService webservc = (SPWebService) srvc;
….
….
}
}
}
III- The SPWebApplication Objects map to IIS web sites.
It is different from SharePoint Site or SharePoint Web. A SPWebApplication has its own web.config.
A SPWebApplication can be created or deleted using the Central Administration. To create: add a
newWeb Application in the Web application collection of the SPWebService, A IIS web site is
automatically created, look in IIS Manager. Typically, developers work with existing Web application.
The sample code shows how to iterate through the WebApplications collection of a SPWebService Object, and check if a WebApplication is a SharePoint Central Administration application.
###########################
SPFarm myFarm = SPFarm.Local;
If (myFarm.CurrentUserIsAdministrator)
{
foreach (SPService srvc in myFarm.Services)
{
if (srvc is SPWebService)
{
SPWebService webServc = (SPWebService) srvc;
foreach (SPWebApplication webApp in webServc.WebApplications)
{
if (webApp.IsAdministrationWebApplication)
{
…
}
}
}
}
}
IV- The SPSite Object is a class and represents a site collection, a security boundary.
A SPSite has a RootWeb property that contains users, groups, rights, permissions. It can have features
and solutions that impact all the site collection. A SPSite can be created by passing a url to its constructor,
by adding a site in the site collection of a Web Application or by referencing the current context site.
The deletion of a Site depends on the chosen instantiation approach.
The sample code shows how to instantiate a SPSite Object.
###########################
…
mySite.Dispose();
or
foreach (SPSite mySite in webAppl.Sites)
{
try
{
…
}
finally
{
mySite.Dispose();
}
or
SPSite mySite = SPContext.Current.Site;
…
// this site must not be dispose, then “SPContext.Current” is a global static class.
V- The SPWeb Object represents a SharePoint Site that can contain pages, lists, libraries, other
SPWeb objects as subwebs. A SPWeb object can be instantiated as the RootWeb property of a SPSite
object, as a member of AllWebs collection of a SPSite object, as the return value of the OpenWeb
method, or by referencing the current context Web.
The sample code shows how to instantiate a SPWeb Object.
###########################
SPWeb myWeb = mySite.RootWeb;
…
myWeb.Dispose();
mySite.Dispose();
or
SPWeb myWeb = mySite.OpenWeb(“/mySiteProject/”); // sous–site relatif au serveur
…
myWeb.Dispose();
mySite.Dispose();
or
…
foreach (SPWeb myWeb in mySite.RootWeb.Webs)
{
try
{
…
}
finally
{
myWeb.Dispose();
}
}
or
SPWeb myWeb = SPContext.Current.Web;
…
// this web must not be dispose, then “SPContext.Current” is a global static class.
VI- The SPList Object contains data: native data, ducuments, images and other media, lookups.
A SPList object can be referenced as member of the Lists collection from a SPWeb object, by adding to
the Lists collection. SPList Objects have many properties, for example content type support, versioning
settings, visibility.
The sample code shows how to iterate through the Lists collection of an SPWeb object, and handle the visible Lists.
…
foreach (SPWeb myWeb in mySite.RootWeb.Webs)
{
try
{
foreach (SPList list in childWeb.Lists)
{
If (!list.Hidden) // Check if the list is visible
{
…
}
}
}
finally
{
myWeb.Dispose();
}
}