Live Developer Training: next batch coming soon. Be the first to know.Live training: next batch soonJoin the waitlist →Join →

SharePoint Server Object Model Tutorial

If you want to become a SharePoint developer, especially working in the SharePoint on-premises versions, then read this complete tutorial on the SharePoint server object model, or SSOM in SharePoint.

In this tutorial, I will explain how to work with SharePoint SSOM, the various classes to use, and a few server-side object model examples.

SharePoint Server Side Object Model

As the name suggests, the code runs on the SharePoint server, which is why Microsoft does not allow server-side object model code in SharePoint Online.

You can only use these codes in on-premises versions of SharePoint, like SharePoint Server versions, where you have installed SharePoint and are using it.

To work with the SharePoint server object model, we need to refer to the Microsoft.SharePoint.dll that is present in the C:\Program Files\Common Files\microsoft shared\Web Server Extensions\19\ISAPI directory. The path will vary depending on which version of SharePoint you are using.

Using SSOM code, you can develop custom solutions and visual web parts, which can then be deployed using PowerShell commands.

Various SharePoint Server Object Model Classes

Microsoft provides a lot of namespaces and classes to work with SharePoint objects using server object model code.

Here are two diagrams to understand the object model hierarchy of these classes.

sharepoint server object model

There are various classes that we can use.

sharepoint server side object model

Today, to use any class, you first need to import the DLL and the namespaces. To make it simple and easy to understand, I have made a table. Follow this:

SharePoint ComponentsServer Object Model ClassNamespace
FarmSPFarmMicrosoft.SharePoint.Administration
ServerSPServerMicrosoft.SharePoint.Administration
Web ApplicationSPWebApplicationMicrosoft.SharePoint.Administration
Content DatabaseSPContentDatabaseMicrosoft.SharePoint.Administration
Site CollectionSPSiteMicrosoft.SharePoint
SiteSPWebMicrosoft.SharePoint
List/LibrarySPListMicrosoft.SharePoint
ItemSPItemMicrosoft.SharePoint
SharePoint Server object model classes

Check out CAML Query Builder in SharePoint Online

Create a Windows Or Console Application

To use the classes or methods, let us create a Windows or Console application using Visual Studio. You can use the latest version of Visual Studio.

But make sure to create this on the SharePoint server where you have installed SharePoint, or else it will not work. You cannot run the code from a client machine.

Here, I have created a Windows application.

  • Open Visual Studio and then click on File -> New -> Project. Then, from the templates, choose Windows and then choose Windows Forms Application.
  • Next, we need to add a reference to the Microsoft.SharePoint.dll. So that we can use the classes or methods, for this, right-click on the References and then click on Add Reference… like below:
sharepoint 2013 server side object model
  • Then go to the below path: C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI. Then, add Microsoft.SharePoint.dll from the folder.

Now, you can write the code. For this case, I wrote a sample code to retrieve all SharePoint site collections presented under a web application in SharePoint.

string s = string.Empty;
            SPWebApplication web = SPWebApplication.Lookup(new Uri("http://mypc:4120"));
            foreach (SPSite site in web.Sites)
            {
                s += "Site URL: " + site.Url + " Site RootWeb Title " + site.RootWeb.Title + " Rootweb URL " + site.RootWeb.Url + "\n";

            }
            label2.Text = s;

Once you execute the code, it will display all the site collection URLs, Titles, and Root Web URLs.

Now, let us check a few examples of server-side object model in SharePoint.

Another popular use of object model code is inside SharePoint visual web parts. Later in this tutorial, I will show an example of how to create a visual web part in SharePoint.

Read CSOM in SharePoint Online

SharePoint Object Model Examples

Now, let’s examine a few examples of how the SSOM code can be utilized in on-premises SharePoint versions.

1. Create a SharePoint document library

I will explain here how to create a SharePoint document library by using server object model code.

Let’s see how to programmatically create a Document Library in SharePoint using the SharePoint Server Object Model. For this example, I have created a visual web part.

In the visual web part, I have created a form for users to enter library details, such as name and description.

Below are the controls we will use to develop the form.

  • TextBox: I have taken two textboxes, one for the document library name and another for the library description.
  • Button: Add the Create button.
  • Label: If an error occurs after deploying the code, this control helps display the error message.
<table>
    <tr>
        <td>Document Library Name:</td>
        <td><asp:TextBox ID="txtDocumentLibraryName" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td>Description:</td>
        <td><asp:TextBox ID="txtDescription" runat="server" TextMode="MultiLine"></asp:TextBox></td>
    </tr>
    <tr>
        <td></td>
        <td><asp:Button ID="btnCreateDocumentLibrary" runat="server" Text="Create" Width="90px" OnClick="BtnCreateDocumentLibrary_Click" /></td>
    </tr>
    <tr>
        <td><asp:Label ID="lblMessage" runat="server" Text=""></asp:Label></td>
    </tr>
</table>

The input design form looks like below: