Friday, 19 October 2012

Sharepoint 2010 Server Object Model


Sharepoint 2010: Server Object Model
This article explains the new object model hierarchy in SharePoint 2010, which describes about each object in below diagram.


















 We can broadly divide the object model in two parts:
Administration and Configuration Classes: These classes are generally used for administration and configuration purposes.
·         SPForm
·         SPServer
·         SPService
·         SPServiceInstance
·         SPWebService
Site Provisioning and Content Access Classes: These classes are used for programmatically provisioning sites and accessing data contained within sites, lists, and libraries.
·         SPWebApplication
·         SPSite
·         SPWeb
·         SPList
·         SPListItem
·         SPFile
·         SPFolder

SPFormSPFarm object is top level object in object model hierarchy and represents the SharePoint farm. All server side code must be executed on a server that is a member of a SharePoint farm.

We can get the instance of server form as in below code:
SPFarm sFarm = SPFarm.Local;

SPServer: The SPServer object represents a specific server within a SharePoint farm.

To list all servers in the form, we can use below code:
foreach (SPServer server in SPFarm.Local.Servers)
{
Console.WriteLine(server.DisplayName);
}

SPService: SharePoint 2010 provide platform for running services across a farm of servers. All services are derived from SPService class. Example of such services are Search service, Performance point services, Business connectivity service (BCS) etc.

SPServiceInstance: The SPServiceInstance object represents an instance of a service that
is running on a particular server.

To get the service instance, following code can be used:
foreach (SPServiceInstance service in SPServer.Local.ServiceInstances)
{
Console.WriteLine(service.TypeName);
}

SPWebService: SPWebService represents a Web service that contains one or more Web applications. This Web service allows a Web browser to access the content in SharePoint sites.


SPWebApplication: WebApplication is the topmost object in the site hierarchy, Each Web Application configured in SharePoint is represented by SPWebApplication object in object model

SharePoint runs on top of Internet Information Services (IIS) and ASP.NET. A SharePoint farm
can host many web applications, each of which is a stand-alone ASP.NET application running in its own
Application Pool.
Following code will list all the web applications running on the server form:
SPWebService webService = SPFarm.Local.Services.
OfType<SPWebService>().First();
foreach (SPWebApplication app in webService.WebApplications)
{
Console.WriteLine(app.Name);
}

SPSite: Within a web application exists one or more site collections, which are collections of
web sites. The SPSite object is one of the primary entry points to the Server Object Model and represents a collection of sites in a Web application, including a top-level Web site and all its subsites..

SPSite object can be instanciated as below:

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

SPWeb: Within the object model, individual sites are represented by SPWeb objects. Please note here that SPSite object represent site collection and SPWeb represent individual sites within site collection.

Code for accessing all sites (SPWeb) in site collection (SPSite is given below:
using (SPSite site = new SPSite("YourSiteCollectionURL"))
{
foreach (SPWeb web in site.AllWebs)
{
Console.WriteLine(web.Title);
web.Dispose();
}
}

SPList: In the Server Object Model, both lists and document libraries are represented by an SPList object.
Please note that document libraries are also represented by SPDocumentLibrary
Objects and is derived from the SPList class.

Code to access all list in site collection is given below:

using (SPSite site = new SPSite("YourSiteCollectionURL "))
{
using (SPWeb root = site.RootWeb)
{
foreach (SPList list in root.Lists)
{
Console.WriteLine(list.Title);
}
} 

SPListItem: Each item in a list or library is represented by an SPListItem object  and accessed through SPList.Items collection. Please note that the Items property returns all the files in a document library, including files in subfolders, but not the folders themselves. In a document library, folders are not considered items.

Code to access list items (SPListItem) is given below:

using (SPWeb oWebsite = new SPSite("http://Server/sites/SiteCollection").OpenWeb())
{

    SPList oList = oWebsite.Lists["Projects"];

    SPListItemCollection collItem = oList.Items;

    for (int i = 0; i < oList.ItemCount; i++)
    {
        string itemName = collItem[i].Name;

        Label1.Text += itemName + "<BR>";

    }
}

SPFile: If we create a document library and upload a Word document, the SPListItem
object that represents the document will contain only the document title and a few
metadata fields. To perform work on the document, we need to use an SPFile object.


SPFolder: document libraries can also contain folders that operate in the same
way as folders in the file system.

To list all folders in the site, we can use following code:
using (SPSite site = new SPSite("YourSiteCollectionURL"))
{
using (SPWeb root = site.RootWeb)
{
foreach (SPFolder folder in folders)
{
Console.Write(folder.Name + "\t");
if (folder.DocumentLibrary != null)
{
Console.WriteLine("Corresponding library: " + folder.DocumentLibrary.Title);
}
else
Console.WriteLine(string.Empty);
}
listFolders(folder.SubFolders);
}
}
}

Hope this is informative to you. Please let me know  your feedback/comments.