TechBubbles Microsoft Technology BLOG

SharePoint 2010 Server Object Model

This post explains the new object model hierarchy in SharePoint 2010, which describes about each object in below diagram.SP2010

image 

Create a console project in Visual Studio and write a code snippet for each object to better understand the purpose. The classes in above diagram used for administration and configuration purpose and these are used for implementing service applications.

SPFarm

SPFarm represents the SharePoint Server Farm, You can use the below code to list the servers in SharePoint Farm

   1: class Program

   2: {

   3: static void Main(string[] args)

   4: {

   5:     Program p = new Program();

   6:     p.GetServersInFarm();

   7: }

   8: void GetServersInFarm()

   9: {

  10:     Console.WriteLine("Servers in Farm are:");

  11:     foreach (SPServer server in SPFarm.Local.Servers)

  12:     {

  13:         Console.WriteLine(server.DisplayName);

  14:     }

  15: }

  16: }

SPServer

SPServer object represents a server in SharePoint form, You can get a current reference to the SPServer object as follows

   1: foreach (SPServiceInstance service in SPServer.Local.ServiceInstances)

   2: {

   3:     Console.WriteLine(service.TypeName);

   4: }

SPService

Number of services run in SharePoint platform which can include features such as web services or search  services which provides search functionality to other services. SharePoint 2010 provides several services out of the box, each of these services represented by an object that derives from SPService class.

SPServiceInstance

SharePoint farm can have multiple servers, each server can have more than one instance. SPServiceInstance object represents an instance of a service that is running on a particular server.

 

SPWebService

SPWebService is the parent service that hosts all the front-end web sites within a SharePoint farm.

The following classes allows you to programmatically access the sites and data contained in the sites, lists and libraries. These are the commonly used classes in SharePoint Development.

  image

SPWebApplication

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

   1: SPWebService webService = SPFarm.Local.Services.

   2: OfType<SPWebService>().First();

   3: foreach (SPWebApplication app in webService.WebApplications)

   4: {

   5:     Console.WriteLine(app.Name);

   6: }

SPSite

In SharePoint each site collection is represented by SPSite object and is very frequently used in the SharePoint Application Development. Code to create a SPSite object as below

To Display the Sites in Site Collection,

   1: using (SPSite site = new SPSite("www.techbubbles.com"))

   2: {

   3:     foreach (SPWeb web in site.AllWebs)

   4:     {

   5:         Console.WriteLine(web.Title);

   6:         web.Dispose();

   7:     }

   8: }

SPWeb

Within the object model , sites are represented by SPWeb objects. To obtain a reference to the root site in SharePoint site collection you can write the below code

   1: using (SPSite site = new SPSite("www.techbubbles.com "))

   2: {

   3:      using (SPWeb root = site.RootWeb)

   4:         {

   5:            //access the properties of root here

   6:         }

   7: }

SPList

SharePoint content is stored in lists or document libraries. Both lists and document libraries are represented as SPList in object model.

   1: using (SPSite site = new SPSite("www.techbubbles.com "))

   2: {

   3:   using (SPWeb root = site.RootWeb)

   4:     {

   5:         foreach (SPList list in root.Lists)

   6:         {

   7:             Console.WriteLine(list.Title);

   8:         }

   9:      }

  10: }

SPListItem

Each item in SharePoint list or library is represented by an SPListItem object which can be accessed via the SPList.Items collection. Listing the all master pages in site collection

   1: using (SPSite site = new SPSite("www.techbubbles.com"))

   2: {

   3:     using (SPWeb root = site.RootWeb)

   4:     {

   5:         SPList masterPages = root.Lists.TryGetList("Master Page Gallery");

   6:         if (masterPages != null)

   7:         {

   8:             SPListItemCollection items = masterPages.Items;

   9:             foreach (SPListItem fileItem in items)

  10:             {

  11:            

  12:             }

  13:          }

  14:      }

  15: }

SPFile

SPListItem object represents only metadata for the file, To perform work on file you need to use SPFile object as shown below

   1: foreach (SPListItem fileItem in items)

   2: {

   3:     SPFile file = fileItem.File;

   4:     Console.WriteLine(file.Name);

   5: }

Share this post :

About the author

Kalyan Bandarupalli

My name is kalyan, I am a software architect and builds the applications using Microsoft .NET technologies. Here I am trying to share what I feel and what I think with whoever comes along wandering to Internet home of mine.I hope that this page and its contents will speak for me and that is the reason I am not going to say anything specially about my self here.

Add Comment

TechBubbles Microsoft Technology BLOG

Follow me

Archives

Tag Cloud