Wednesday, 27 September 2017

SharePoint 2013 Server Object Model (SOM) Programming

SharePoint Server Object Model Programming allows you to write code and communicates with SharePoint objects if you are inside SharePoint context. It provides a set of classes which is executed at server side. Server Object Model Programming is same in SharePoint 2010 and SharePoint 2013. 

SharePoint 2013 Server Object Model Tutorial

In this blog, I will demonstrate SOM programming in SharePoint 2013 using Visual Studio 2015. You can use Server object model in a console application, windows form and other ASP.Net Applications. 
In SharePoint Server Object Model it is necessary to reference Microsoft.SharePoint dll in the working project. You can implement SOM model in a system only if SharePoint Server is installed.

You may find Microsoft.SharePoint dll in Windows Drive in my case it is C:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI

Below are list of classes used in SharePoint Server Object Model

  • SPFarm
  • SPWeb
  • SPSite
  • SPList
  • SPNavigationNode
  • SPUser
  • SPWebApplication

SharePoint Server Object Model Architecture


SharePoint Server Object Model

Let's See SharePoint 2013 Server Object Model Examples


Below code snippet will give you current Farm details.


               SPFarm objFarm = SPFarm.Local;
            long farmVersion = objFarm.Version;
            Guid farmID = objFarm.Id;
            SPObjectStatus farmStatus = objFarm.Status;
            Console.WriteLine("Farm Version is:" + farmVersion);
            Console.WriteLine("Farm ID is:" + farmID);
            Console.WriteLine("Farm Status is:" + farmStatus);
            Console.ReadKey();

Below code snippet will create a site in your web application.

SPSite objSite = new SPSite("http://localhost:1000");
            SPWebApplication objWebApp = objSite.WebApplication;
            objWebApp.Sites.Add("/sites/demosite", "Demo Site", "This is a demosite", 1033, "STS#0", @"corp\administrator", "Administrator", "[email protected]");
            Console.ReadKey();


Below code snippet will add a link in top navigation and quick launch

SPSite objSite = new SPSite("http://localhost:1000");
            SPWeb objWeb = objSite.RootWeb.Webs[""];//leave it empty and run the code.
            SPNavigationNode objNode = new SPNavigationNode("SharePointCafe Blog", "http://sharepointcafe.net", true);
            objWeb.Navigation.QuickLaunch.AddAsFirst(objNode);
            objWeb.Navigation.TopNavigationBar.AddAsFirst(objNode);
            Console.WriteLine("Task Done");
            Console.ReadKey();


Please visit my Youtube Channel for these SharePoint 2013 Server Side Object Model Demonstration.

No comments:

Post a Comment

Dear Readers, Please post your valuable feedback in the comment section if you like this blog or if you have any suggestions. I would love to hear the same from you. Thanks