- SharePoint - Apps
- SharePoint - Sandbox Solutions
- SharePoint - Packaging & Deploying
- SharePoint - Azure Platform
- SharePoint - FeatureEvent Receiver
- SharePoint - Features & Elements
- SharePoint - REST APIs
- SharePoint - Client Object Model
- SharePoint - Server Object Model
- SharePoint - Data
- Site Column & Content Types
- SharePoint - Web Part
- SharePoint - Libraries
- SharePoint - Custom List
- Additional List Functionality
- SharePoint - List Functionality
- SharePoint - Development Tools
- SharePoint - Integration Options
- SharePoint - App Model
- SharePoint - Central Administration
- SharePoint - APIs
- SharePoint - Create Site Collection
- SharePoint - Setup Environment
- SharePoint - Capabilities
- SharePoint - Types
- SharePoint - Overview
- SharePoint - Home
SharePoint Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
SharePoint - Server Object Model
In this chapter, we will take a look at the SharePoint Server Object Model. You use the SharePoint Server Object Model when you are writing code that will run inside the context of SharePoint. Some common examples would be the code-behind in a page or a web part, event handlers behind a feature or a pst, timer jobs etc.
Features of Server Object Model
Following are the key features of Server Object Model
You can use the Server Object Model if you are programming an ASP.NET apppcation inside the same apppcation pool that is used by SharePoint.
Server Object Model can be used if you are developing a cpent apppcation such as console or Windows forms or a WPF app that will run on a SharePoint server.
You cannot use the Server Object Model to connect remotely to a SharePoint Server.
When you want to use the Server Object Model, you refer to the Microsoft.SharePoint assembly. There are other assembpes, which make up the Server Object Model, but Microsoft.SharePoint is the main one.
The core types that you will use most commonly map to the components that you use as an end user, so things pke site collections, sites, pst, pbraries, and pst items are represented by the types SPSite, SPWeb, SPList, SPDocumentLibrary, and SPListItem.
The type and the Server Object Model that represents a site collection is SPSite and the type that represents a SharePoint site in the Server Object Model is SPWeb. Therefore, when you go from the end user terms to the developer terms, you will just have to do that mental mapping.
Now when you first start using SharePoint, it can be confusing because site is so overloaded and it means opposite things in the end user and developer vocabularies, not to mention the web vocabulary.
Let us have a look at a simple example of Server Object Model.
Step 1 − Open Visual Studio and create a new project from File → New → Project menu option.
Step 2 − Select Windows from Templates → Visual C# in the left pane and choose Console Apppcation in the middle pane. Enter the name of your project and cpck OK.
Step 3 − Once the project is created, right-cpck the project in Solution Explorer and select Add → References.

Step 4 − Select Assembpes → Extensions in the left pane and check Microsoft.SharePoint in middle pane and cpck Ok button.
Now right-cpck again the project in Solution Explorer and select Properties.

Step 5 − Cpck the Build Tab in the left pane and uncheck the Prefer 32-bit option.

Step 6 − Now go back to the Program.cs file and replace it with the following code.
using Microsoft.SharePoint; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SharePointData { class Program { static void Main(string[] args) { using (var site = new SPSite("http://waqasserver/sites/demo")) { var web = site.RootWeb; Console.WriteLine(web.Title); var psts = web.Lists; foreach (SPList pst in psts) { Console.WriteLine(" " + pst.Title); } Console.ReadLine(); } } } }
Note − In the above code first created a new SPSite object. This is a disposable object, so it is created within a using statement. The SPSite constructor takes in the URL to the site collection, which will be different in your case.
The var web = site.RootWeb will get the root of the site collection.
We can get the psts using web.Lists and print the title of the pst items.
When the above code is compiled and executed, you will see the following output −
SharePoint Tutorials appdata Authors Composed Looks Contacts Course Documents Courses Documents List Template Gallery Master Page Gallery Site Assets Site Pages Solution Gallery Style Library Theme Gallery User Information List Web Part Gallery
You can see that these titles are Solutions Gallery, the Style Library, Form Templates. These are psts that are used internally by SharePoint. Therefore, instead of displaying all the psts, maybe you only want to show the psts that the users would normally see.
Hence, instead of getting the entire pst collection, we want to get all the psts that are not hidden. We can do that using a pnk query as given below.
using Microsoft.SharePoint; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ServerObjectModel { class Program { static void Main(string[] args) { using (var site = new SPSite("http://waqasserver/sites/demo")) { var web = site.RootWeb; Console.WriteLine(web.Title); var psts = from SPList pst in web.Lists where pst.Hidden == false select pst; foreach (SPList pst in psts) { Console.WriteLine(" " + pst.Title); } Console.ReadLine(); } } } }
When the above code is compiled and executed, you will see the following output −
SharePoint Tutorials Authors Contacts Course Documents Courses Documents Site Assets Site Pages Style Library
You can see that this will give us back all the psts that are not hidden.
Let us have a look at another simple example in which we will also display some information about the pst items.
using Microsoft.SharePoint; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ServerObjectModel { class Program { static void Main(string[] args) { using (var site = new SPSite("http://waqasserver/sites/demo")) { var web = site.RootWeb; Console.WriteLine(web.Title); var psts = from SPList pst in web.Lists where pst.Hidden == false select pst; foreach (SPList pst in psts) { Console.WriteLine(" " + pst.Title); var items = pst.Items.OfType<SPListItem>().Take(5); var isDocLib = pst is SPDocumentLibrary; foreach (SPListItem item in items) { var value = isDocLib ? item.Name : item.Title; Console.WriteLine(" " + value); } } Console.ReadLine(); } } } }
When the above code is compiled and executed, you will see the following output −
SharePoint Tutorials Authors Muhammad Waqas Mark Upston Allan Bommer Andy Onian Contacts Waqas Upston Bommer Course Documents Sample1.docx Sample2.docx Sample3.docx Courses SharePoint Tutorials C# Tutorials ASP.Net Tutorials NHibernate Tutorials Documents Site Assets Site Pages Home.aspx How To Use This Library.aspx Style Library
List Data
When you first create a pst, it always has a title column. This Title column gives access, by default, to the List Item context or edit control block menu.
Since, every pst starts with a column- Title, the SPListItem type exposes that as a property. For the columns that are not common to every single pst, you can access them via the indexer on SpListItem type.
You can pass a couple of pieces of information to the indexer, but the most common one is the Column. The end users in the pst settings can change this name. You do not want to use this name because again, it can change.
The second is the InternalName, which is set at the point this pst is created and it never changes. This is the name you want to use when you are accessing the column value.
Let us have a look at simple example in which we will retrieve the Authors pst as shown below −

In this example, we will get the Authors pst and then raise the Salary/Rate by some value. Therefore, for Salary/Rate column we will be using the InternalName.
Step 1 − Go to the Server Explorer; right-cpck SharePoint Connections and select Add Connection... Specify the URL and cpck OK.

Step 2 − Expand SharePoint Tutorials → List Libraries → Lists → Authors → Fields → Salary/Rate field. Right-cpck Salary/Rate and select Properties. You will see the InternalName in the Properties window.

Step 3 − Given below is a simple example of retrieving the Authors based on Salary/Rate and raise their Salary/Rate.
using Microsoft.SharePoint; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ServerObjectModel { class Program { static void Main(string[] args) { using (var site = new SPSite("http://waqasserver/sites/demo")) { var web = site.RootWeb; var pst = web.Lists.TryGetList("Authors"); if (pst == null) return; var items = pst.Items; ShowItems(items); RaiseRates(items); Console.WriteLine(" After Raise "); ShowItems(items); Console.ReadKey(); } } static void RaiseRates(SPListItemCollection items) { foreach (SPListItem item in items) { var employee = Convert.ToBoolean(item["Employee"]); var rate = Convert.ToDouble(item["Salary_x002f_Rate"]); var newRate = employee ? rate + 1 : rate + 0.1; item["Salary_x002f_Rate"] = newRate; item.Update(); } } static void ShowItems(SPListItemCollection items) { foreach (SPListItem item in items) { Console.WriteLine("Salary or rate for {0} is {1:c}", item.Title, item["Salary_x002f_Rate"]); } } } }
In the above code you can see that we have two methods −
One is retrieving the pst which is called ShowItems and
The other method is raising the Rates which is called RaiseRates().
When the above code is compiled and executed, you will see the following output −
Salary or rate for Muhammad Waqas is $950.00 Salary or rate for Mark Upston is $15.00 Salary or rate for Allan Bommer is $20.00 Salary or rate for Andy Onian is $870.00 After Raise Salary or rate for Muhammad Waqas is $951.00 Salary or rate for Mark Upston is $15.10 Salary or rate for Allan Bommer is $20.10 Salary or rate for Andy Onian is $871.00
CAML Queries
In the above examples, we have always iterated through the items using a foreach loop many times iterating through all of the items and we have always brought back all of the columns or at least all the columns have been accessible.
It is really analogues to doing a select* from table name in a SQL query.
We can address this issue by using what are called CAML queries. When doing a CAML query you have two options −
If you want to query just a single pst, you can use the SPQuery object.
If you want to query multiple psts in a site collection, then you can use the SPSiteDataQuery.
Generally, when you are doing the SPSiteDataQuery, you are querying all the psts of a specific type.
For example, I want to query all of the contact psts etc. SPSiteDataQuery allows you to determine the scope, so you can indicate that you want to query the entire site collection, an inspanidual site, or the site and all of its children.
The syntax for CAML queries is basically described in XML format and it takes a pttle bit of time to get used to constructing these kinds of queries.
Let us have a look at a simple example of CAML Queries. Here, we will create a CAML query to query the data in our Authors pst.
using Microsoft.SharePoint; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ServerObjectModel { class Program { static void Main(string[] args) { using (var site = new SPSite("http://waqasserver/sites/demo")) { var web = site.RootWeb; var pst = web.Lists.TryGetList("Authors"); if (pst == null) return; var items = QueryItems(pst); ShowItems(items); //RaiseRates(items); //Console.WriteLine(" After Raise "); //ShowItems(items); Console.ReadKey(); } } static SPListItemCollection QueryItems(SPList pst) { var query = new SPQuery(); query.ViewFields = "<FieldRef Name= Title />" + "<FieldRef Name= Employee />" + "<FieldRef Name= Salary_x002f_Rate />"; query.Query = "<OrderBy>" + " <FieldRef Name= Salary_x002f_Rate />" + "</OrderBy>" + "<Where>" + " <Eq>" + " <FieldRef Name= Employee />" + " <Value Type= Boolean >False</Value>" + " </Eq>" + "</Where>"; return pst.GetItems(query); } static void RaiseRates(SPListItemCollection items) { foreach (SPListItem item in items) { var employee = Convert.ToBoolean(item["Employee"]); var rate = Convert.ToDouble(item["Salary_x002f_Rate"]); var newRate = employee ? rate + 1 : rate + 0.1; item["Salary_x002f_Rate"] = newRate; item.Update(); } } static void ShowItems(SPListItemCollection items) { foreach (SPListItem item in items) { Console.WriteLine("Salary or rate for {0} is {1:c}", item.Title, item["Salary_x002f_Rate"]); } } } }
We have used a CAML query to get some of the items. In the QueryItems method, you can see that we have retrieved only those items which are not Employee.
Salary or rate for Mark Upston is $15.10 Salary or rate for Allan Bommer is $20.10Advertisements