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.

There are various classes that we can use.

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 Components | Server Object Model Class | Namespace |
| Farm | SPFarm | Microsoft.SharePoint.Administration |
| Server | SPServer | Microsoft.SharePoint.Administration |
| Web Application | SPWebApplication | Microsoft.SharePoint.Administration |
| Content Database | SPContentDatabase | Microsoft.SharePoint.Administration |
| Site Collection | SPSite | Microsoft.SharePoint |
| Site | SPWeb | Microsoft.SharePoint |
| List/Library | SPList | Microsoft.SharePoint |
| Item | SPItem | Microsoft.SharePoint |
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:

- 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:

Below is the SharePoint server object model code to create the SharePoint document library programmatically, which you can write in the button click event.
using Microsoft.SharePoint;
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace Document_Library_Creation.Document_Library_Creation
{
public partial class Document_Library_CreationUserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void BtnCreateDocumentLibrary_Click(object sender, EventArgs e)
{
CreateDocumentLibrary();
}
public void CreateDocumentLibrary()
{
try
{
SPSite siteCollection = SPContext.Current.Site;
SPWeb web = SPContext.Current.Web;
web.AllowUnsafeUpdates = true;
SPList spList = web.Lists.TryGetList(txtDocumentLibraryName.Text.Trim());
if (spList == null)
{
web.Lists.Add(txtDocumentLibraryName.Text.Trim(), txtDescription.Text.Trim(), SPListTemplateType.DocumentLibrary);
//SPList newList = web.Lists["TestList"];
web.AllowUnsafeUpdates = false;
CreateColumn(web);
lblMessage.Text = "Your document library created successfully";
}
else
{
lblMessage.Text = "This document library already exists";
}
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
}
}
}
}
Once you fill in all the field values and click the “Create” button, the document library will be created on the SharePoint site. It will display a successful message like the one below:

You can also verify the document library in the SharePoint site contents page.

This is how to create a document library programmatically c#.net using SharePoint server object model code.
Check out SharePoint Rest API
2. Add Columns to a SharePoint document library
In the example below, we will see how to add columns (Choice columns and Single line of text columns) to a SharePoint document library. We are adding the three columns below programmatically to the SharePoint document library.
- DocumentLocation (Single line of text): This column is a Single line of text.
- DocumentLanguage (Choice column): This column is a Choice field. This language field column has some choice values:
- DocumentDepartment (Choice Column): This column is also a Choice field. This department field column has some choice values as:
- IT
- HR
- Finance
Below is the SharePoint server object model code that you can use to create (a Single line of text and a Choice column) in a SharePoint Document Library programmatically.
string libraryDocumentLocationColumnName = "DocumentLocation";
string libraryLanguageColumnName = "DocumentLanguage";
string languageChoiceColumn1 = "USA English";
string languageChoiceColumn2 = "UK English";
string libraryDepartmentColumnName = "DocumentDepartment";
string departmentChoiceColumn1 = "IT";
string departmentChoiceColumn2 = "HR";
string departmentChoiceColumn3 = "Finance";
public void CreateColumn(SPWeb web)
{
try
{
web.AllowUnsafeUpdates = true;
SPList spList = web.Lists.TryGetList
(txtDocumentLibraryName.Text.Trim());
spList.Fields.Add
(libraryDocumentLocationColumnName, SPFieldType.Text, false);
spList.Fields.Add(libraryLanguageColumnName,
SPFieldType.Choice, false);
spList.Fields.Add(libraryDepartmentColumnName, SPFieldType.Choice, false);
/* get the newly added choice field instance */
SPFieldChoice chFldDocLanguage =
(SPFieldChoice)spList.Fields[libraryLanguageColumnName];
/* set field format type i.e. radio / dropdown */
chFldDocLanguage.EditFormat =
SPChoiceFormatType.Dropdown;
/* set the choice strings and update the field */
chFldDocLanguage.Choices.Add
(languageChoiceColumn1);
chFldDocLanguage.Choices.Add
(languageChoiceColumn2);
chFldDocLanguage.Choices.Add
(languageChoiceColumn3);
chFldDocLanguage.Update();
/* get the newly added choice field instance */
SPFieldChoice chFldDocDepartment =
(SPFieldChoice)spList.Fields[libraryDepartmentColumnName];
/* set field format type i.e. radio / dropdown */
chFldDocDepartment.EditFormat =
SPChoiceFormatType.Dropdown;
/* set the choice strings and update the field */
chFldDocDepartment.Choices.Add
(departmentChoiceColumn1);
chFldDocDepartment.Choices.Add
(departmentChoiceColumn2);
chFldDocDepartment.Choices.Add
(departmentChoiceColumn3);
chFldDocDepartment.Update();
spList.Update();
web.AllowUnsafeUpdates = false;
}
catch (Exception exc)
{
lblMessage.Text = exc.Message;
}
}
Now, deploy the code and go to the existing SharePoint document library; you will see that one single line of the text column and other choice columns will be available, as shown below.

Once you go to the “Columns” section of “Library Settings” of that SharePoint Document Library, then you can see all the Column name with their “Type“.

While you click on the Choice Column name, you can see all the choice field values in the “Type each choice on a separate line” option, as in the screenshot below.

This is how to add columns to a SharePoint document library using SSOM.
Check out JSOM SharePoint With Examples
3. Upload documents to the SharePoint document library programmatically with metadata
Now, I will show you how to upload documents to a document library using SSOM code in a Visual Web Part.
In this case, I will show you how to set up metadata while uploading the document, which allows us to also set values for a few select columns (Language and Department).
In the Visual Web Part, we can design the form in the .ascx file. Here, we will add the controls below.
- fileUpload: This control is used for uploading a file.
- Button: This control enables the creation of a button. So that when a user uploads a file and then clicks on the button, the file will be saved in the particular document library.
- Label: If an error occurs after deploying the code, this control helps display the error message.
<table>
<tr>
<td>Select File:</td>
<td><asp:FileUpload ID="fuUploadFile" runat="server" </td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="btnSave" runat="server" Text="Save" Width="105px" OnClick="BtnSave_Click1" /></td>
</tr>
<tr>
<td><asp:Label ID="lblMessage" runat="server" Text=""></asp:Label></td>
</tr>
<tr>
<td><asp:Label ID="lblCheckMesaage" runat="server" Text=""></asp:Label></td>
</tr>
</table>
Below is the complete code you can write on the button click.
using Microsoft.SharePoint;
using System;
using System.IO;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace TSInfo_Intranet.File_Upload
{
public partial class File_UploadUserControl : UserControl
{
string siteURL = "";
string listTitle = "Company Document";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void BtnSave_Click1(object sender, EventArgs e)
{
uploadDocument();
}
public void uploadDocument()
{
if (fuUploadFile.PostedFile != null &&
fuUploadFile.HasFile)
{
try
{
SPSite siteCollection = SPContext.Current.Site;
SPWeb web = SPContext.Current.Web;
SPList spList = web.Lists.TryGetList(listTitle);
{
{
byte[] contents;
using (Stream filestream =
fuUploadFile.PostedFile.InputStream)
{
contents = new byte[filestream.Length];
filestream.Read(contents, 0,
(int)filestream.Length);
filestream.Close();
string fileName = Path.GetFileName
(fuUploadFile.PostedFile.FileName);
string fileNameWithoutExtension =
Path.GetFileNameWithoutExtension
(fuUploadFile.PostedFile.FileName);
spList.RootFolder.Files.Add
(spList.RootFolder.Url + "/" + fileName, contents, true);
spList.Update();
}
}
}
}
catch (Exception ex)
{
lblMessage.Text = ex.StackTrace;
lblCheckMesaage.Text = ex.Message;
}
}
else
{
lblMessage.Text = "Select a File";
}
}
}
}
Here, we have added two dropdown lists to the visual web part for setting up metadata programmatically.
<table>
<tr>
<td>Language:</td>
<td><asp:DropDownList ID="ddllang" runat="server">
<asp:ListItem>Select Language</asp:ListItem>
<asp:ListItem>USA/English</asp:ListItem>
<asp:ListItem>UK/English</asp:ListItem>
</asp:DropDownList></td>
</tr>
<tr>
<td>Document Type:</td>
<td><asp:DropDownList ID="ddldoctype" runat="server">
<asp:ListItem>Select Document Type</asp:ListItem>
<asp:ListItem>IT</asp:ListItem>
<asp:ListItem>HR</asp:ListItem>
<asp:ListItem>FINANCE</asp:ListItem>
</asp:DropDownList></td>
</tr>
</table>
Below is the SharePoint server object model Code that you can use to set the metadata to the choice field programmatically.
public void SetupMetadata(SPWeb spWeb, SPList spList, string title, string fileNameWithoutExtension)
{
try
{
string fullFilePath = siteURL + spList.RootFolder.Url + "/" + title;
SPFile newFile = spWeb.GetFile(fullFilePath);
SPListItem item = spList.Items[newFile.UniqueId];
item["Title"] = fileNameWithoutExtension;
item["Language"] = spList.Fields
["Language"].GetFieldValue(ddlLanguage.SelectedValue);
item["Department"] = spList.Fields
["Department"].GetFieldValue(ddlDepartment.SelectedValue);
item.Update();
spWeb.Update();
}
catch (Exception )
{
lblMessage.Text = "Error ocuured";
}
}
4. Hide SharePoint document library
We can easily hide the SharePoint list or document library using the SharePoint server object model code. Below is the code to hide the list or document library. You can write in a visual web part or in a feature in SharePoint On-Premises.
using (SPSite site = new SPSite(“http://SiteURL”))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists[“MyList”];
list.Hidden = true;
list.Update();
}
}
Once you execute the code, MyList will not be visible in the Site Contents page in SharePoint. This way, we can also hide the document library in SharePoint on-premises versions.
5. Get Current User Details using SharePoint Server Object Model
We can easily retrieve logged-in user details using the SharePoint Server object model (SSOM). Remember the code below, which you need to run on the SharePoint server, as it requires Microsoft.SharePoint.dll.
Code:
SPSite site = new SPSite("http://Site URL");
SPWeb web = site.OpenWeb();
SPUser user = web.CurrentUser;
Console.Write("SSOM\nUser Information\nUser ID : " + user.ID + "\nUser Login Name : " + user.LoginName + "\nUser Title: " + user.Name);
Console.ReadLine();

Conclusion
I hope you now have a better understanding of how to work with the SharePoint server-side object model, and I am sure all the examples will be helpful to you.
You may also like:
- Get All SharePoint List Items using REST API Pagination in Power Automate
- Add SharePoint Site Owners using REST API in Power Automate
- Add Multiple WebParts To Single SPFx Solution
- Create SharePoint List View Using REST API in Power Automate
- Rename SharePoint Folder Or File Using REST API Power Automate

Hey! I’m Bijay Kumar, founder of SPGuides.com and a Microsoft Business Applications MVP (Power Automate, Power Apps). I launched this site in 2020 because I truly enjoy working with SharePoint, Power Platform, and SharePoint Framework (SPFx), and wanted to share that passion through step-by-step tutorials, guides, and training videos. My mission is to help you learn these technologies so you can utilize SharePoint, enhance productivity, and potentially build business solutions along the way.
Thank you, Kumar for your great help.