SharePoint - Client Side Object Model(CSOM) - Managed - Getting Started

Problem Definition

Getting started with Client Side Object Model(CSOM) for SharePoint 2013.

What does Client Object Model Covers?

  • Administration
  • Files, Folders
  • Users, Roles, Groups, User Profiles, Feeds
  • Web parts
  • Search
  • Sites, Webs, Features, Event Receivers, Site Collections
  • Lists, List Items, List Fields, Content Types, Views, Forms
  • Taxonomy
  • Analytics
  • Work Flow
  • E-Discovery
  • Business Data

How Request gets processed 

  • Communication with SharePoint Server is 'batched' i.e. Get, Insert, Update and Delete operations will get batched before they are executed.
  • Batch wont get executed unless we want them to be executed.
  • Client will create an XML proxy with details of operations that have been batched and send it to Client.svc a WCF process on Server. Service then processes the request and results are sent back to client in JSON format.
  • We can desalinize or directly use the results based on Managed or JavaScript use.

Prerequisites

"Microsoft.SharePoint.Client" and "Microsoft.SharePoint.Client.Runtime" are very important to retrieve and add data to SharePoint. We need reference to these two in order to get started with SharePoint Client programming.
Sharepoint Client references can be installed in either of following ways:
1) Download from Microfot website



2)  Install using NuGet

For Sharepoint 2013 the version used will be 15.0.

Add References to your project


Managed Code Example



/// <summary>
/// Print if my List Exists
/// </summary>
/// <param name="listTitle"> List Title </param>
public static void DoesListExistis(string listTitle)
{
           //var spContext = new ClientContext("http://<your server>/<site>/");
           using (spContext)
           {
                // Client side SP Web Conext
                var web = spContext.Web;

                // Initilize web and load web object
                spContext.Load(web );

                // Initilize and load lists object
                spContext.Load(web.Lists);

                // Execute the query, which in this case is retrieve lists
                spContext.ExecuteQuery();

                // Filter the list to get the desired list
                var myList = web .Lists.Where(x => x.Title == listTitle);

                // Is my list there in the filtered lists object
                var IsMyListThere = myList.Count() == 1 ? "Yes" : "No";

                // Print the results
                Console.WriteLine($"{listTitle} Exists: {IsMyListThere}");
           }
}

Code Result


Comments

Popular posts from this blog

Azure - Manage Blob Storage - Part #7 - Add Metadata to an Existing Container using C#

Azure - Manage Blob Storage - Part #5 - Create Folder Structure and upload a file to folder using an Existing Container using C#

Algorithm - Breadth First Search(BFS) - Python(3)