SharePoint 2010 – Server Object Model

For programming against the SharePoint items, we need to retrieve the properties and the methods to operate on them. The SharePoint Object Model provides various classes to accomplish this. In this article we can discuss the Object Model with the core classes involved with little amount of coding.

Please note that there are 2 Object Models in SharePoint 2010

1. Server Object Model

2. Client Object Model

Here we are discussing only about the Server Object Model. Server Object Model classes are used for server side programming and will be executed on the SharePoint Server Farm.

Namespace

The Server Object Model classes are residing in the Microsoft.SharePoint assembly. The general classes are available inside Microsoft.SharePoint namespace and the administration classes inside Microsoft.SharePoint.Administration namespace.

Core Classes

Following are the important classes and their corresponding SharePoint items.

Class

SharePoint Item

SPFarm

Farm

SPServer

Server

SPSite

Site Collection

SPWeb

Web site

SPControl

Control

SPList

List

SPDocumentLibrary

Document Library

SPContentType

Content Type

SPUser

User

SPException

SharePoint Exception

Programming

Now we can start with experimenting some of the above classes. For starting with we can create a SharePoint Console Application. Use the New Project > SharePoint > Console Application project item.

clip_image002

Accessing the List Items in a List

For proceeding with this example create a list derived from the Contacts template and name it as “My Contacts”. Add some items inside it with First Name and Last Name set.

Use the following code inside the Console application and execute it.

using (SPSite site = new SPSite("http://appes-pc/my/personal/dotnet")) // Site Collection

{

using (SPWeb web = site.OpenWeb()) // Site

{

SPList list = web.Lists["My Contacts"]; // List

int i = 1;

foreach (SPListItem item in list.Items)

{

Console.WriteLine("Item: " + (i++).ToString());

Console.WriteLine(item.Name);

Console.WriteLine(item.Title);

Console.WriteLine(item.GetFormattedValue("First Name"));

Console.WriteLine(item.GetFormattedValue("Last Name"));

Console.WriteLine(string.Empty);

}

}

}

Console.ReadKey(false);

If you receive any build errors regarding namespace, change the Project Properties > Target to .Net Framework 3.5.

clip_image004

Now try building the application and it should work fine. You can see the following output.

clip_image006

Please note the usage of Title, Name properties and the GetFormattedValue() method.

Accessing the items in a Library

Now we can proceed with accessing the document library items programmatically. For proceeding with you need to create a document library inheriting from Document Library template and name it as “My Docs”. Upload one or two documents into it. We are proceeding to get the file name and length in this example.

Enter the following code in the console application.

using (SPSite site = new SPSite("http://appes-pc/my/personal/dotnet")) // Site Collection

{

using (SPWeb web = site.OpenWeb()) // Site

{

SPDocumentLibrary library = web.Lists["My Docs"] as SPDocumentLibrary; // Library

int i = 1;

foreach (SPListItem item in library.Items)

{

Console.WriteLine("Item: " + (i++).ToString());

Console.WriteLine(item.File.Name);

Console.WriteLine(item.File.Length);

Console.WriteLine(string.Empty);

}

}

}

Executing the application you can see the following results.

clip_image008

SPControl

The SPControl class acts as the base class while developing server controls. It reseides in the namespace Microsoft.SharePoint.WebControls.

SPControl provides static methods that returns reference to the current site, web, web application, module. The methods are:

SPControl.GetContextSite()

SPControl.GetContextWeb()

SPControl.GetContextWebApplication()

SPControl.GetContextModule()

SPException

The SPException class can be used to handle exception inside a try catch block. It represents the exceptions thrown by the server object model.

try

{

// Code here

}

catch (SPException ex)

{

// Handle Exception

}

SPUser

The SPUser class can be used to access user information for a SharePoint site. Enter the following code to get the number of users and their names.

using (SPSite site = new SPSite("http://appes-pc/my/personal/dotnet"))

{

using (SPWeb web = site.OpenWeb())

{

foreach (SPUser user in web.AllUsers)

Console.WriteLine("User: " + user.Name);

}

}

On running the code you can see the results based on your machine users.

clip_image010

Note

You can add a new list item or library item using the Items.Add() method. For updation and deletion use the item.Update() and item.Delete() methods respectively. More code coverage on these areas will be provided later.

References

http://msdn.microsoft.com/en-us/library/ms473633.aspx

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.spcontrol.aspx

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spuser.aspx

Summary

In this article we have explore the SharePoint Object Model and some basic coding examples with List and Library. The advanced coding examples will be provided at later point of time.

2 thoughts on “SharePoint 2010 – Server Object Model

  1. krishna says:

    hi your sharepint article on objectmdodel is simple and lucid but if it contain more about list insert update delete,quserying items of items in sharepoint list items its more helpful coz it becomes real world example

    1. Jean Paul says:

      Hi Krishna,

      Thank You for the comment.
      Regarding the list example, i have uploaded the same.

      You can refer it here: http://jeanpaulva.com/2012/06/02/sharepoint-2010-list-add-edit-delete-using-code/

      Regards,
      Jean Paul

Leave a Reply

Your email address will not be published. Required fields are marked *