SharePoint 2013 Server Object Model (SOM) Programming - SharePoint 2013 Tutorial


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

SharePoint 2013 Server Object Model

In this blog, I will demonstrate the Server Object Model (SOM) programming in SharePoint 2013 using Visual Studio 2015. You can use the Server object model in a console application, windows form and other ASP.Net Applications.

In the SharePoint Server Object Model it is necessary to reference Microsoft.SharePoint dll in the working project. You can implement a SOM model in a system only if the 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 is the list of classes used in the SharePoint Server Object Model
  • SPFarm
  • SPWeb
  • SPSite
  • SPList
  • SPNavigationNode
  • SPUser
  • SPWebApplication

SharePoint Server Object Model Architecture


SharePoint Server Object Model

SharePoint 2013 Server Object Model with 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", "administrator@corp.sharepointcafe.net");
            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.


Comments

Popular