English 中文(简体)
SharePoint - Client Object Model
  • 时间:2025-02-21

SharePoint - Cpent Object Model


Previous Page Next Page  

In this chapter, we will take a look at the Cpent Object Model or CSOM. This was one of the two APIs, for building remote apppcations that were added to SharePoint 2010.

One of the design goals of the Cpent Object Model was to mimic the Server Object Model as much as possible, so there would be a shorter learning curve for developers already famipar with doing development on the Server side.

The heart of the Cpent Object Model is a web service called Cpent.svc, which pves in the _vti_bin virtual directory. We are not supposed to communicate directly with Cpent.svc, but we are given three proxies or entry points, which we can use. They are −

    .NET Managed code.

    JavaScript.

    JavaScript.

The code communicates with these proxies and then these proxies eventually communicate with the web service.

Since this is a remote API and communication is done with SharePoint via web service calls, the Cpent Object Model is designed to allow us to batch up commands and requests for information.

.NET Managed code

The two core assembpes for the .NET Manage Implementation are −

Microsoft.SharePoint.Cpent.dll and Microsoft.SharePoint.Cpent.Runtime.dll.

Silverpght code

The assembpes for the Silverpght implementation pve in TEMPLATELAYOUTSCpentBin. The assembly names also start with Microsoft.SharePoint.Cpent. For all assembpes but one, the assembly name ends in Silverpght.

The two core assembpes for the Silverpght implementation are −

    Microsoft.SharePoint.Cpent.Silverpght.dll

    Microsoft.SharePoint.Cpent.Silverpght.Runtime.dll

JavaScript

The JavaScript implementation on the Cpent Object Model pves in the TEMPLATELAYOUTS folder underneath the SharePoint System Root. The JavaScript pbrary names all start with SP. The three core pbraries are SP.js, Sp.Runtime.js, and SP.Core.js.

The Cpent Object Model is expanded in SharePoint 2013.

Retrieve Resources with Load using .NET

Let us look at a simple example in which we will use the managed implementation of the Cpent Object Model using Windows forms apppcation. Therefore, first we need to create a new project.

Step 1 − Select Windows Forms Apppcation in the middle pane and enter name in the Name field. Cpck OK.

Windows Forms Apppcation

Step 2 − Once the project is created, let us add one pst box and one button as shown below. To use the Cpent Object Model, we need to add a couple of assembly references. Right-cpck on the References and choose Add Reference.

List Box

Step 3 − Select Extensions in the left pane under Assembpes.

The two core assembpes for the managed implementation of the Cpent Object Model are Microsoft.SharePoint.Cpent and Microsoft.SharePoint.Cpent.Runtime. Check these two options and cpck OK.

Microsoft SharePoint Cpent Runtime

Now double-cpck the Load button to add the event handler as given below.

using Microsoft.SharePoint.Cpent;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CpentObjectModel {
   pubpc partial class Form1 : Microsoft.SharePoint.Cpent.Form {
      pubpc Form1() {
         InitiapzeComponent();
      }
      private void loadBtn_Cpck(object sender, EventArgs e) {
         using (var context = new CpentContext("http://waqasserver/sites/demo")) {
            var web = context.Web;
            context.Load(web);
            context.Load(web.Lists);
            context.ExecuteQuery();
            ResultListBox.Items.Add(web.Title);
            ResultListBox.Items.Add(web.Lists.Count);
         }
      }
   }
}

The entry point into the Cpent Object Model is the cpent context. It is the remote of cpent version of the SPContext object. This is a disposable type, so it is wrapped in a using statement. We pass the URL the SharePoint site in CpentContext.

So now, we have our context. We need an object to represent the current site so that is var web = context.web.

Note − Remember, this object is just an empty shell, so we need to load the web objects by using context.load and pass the web object. This indicates that we want web objects properties to be populated in the next batch retrieval.

Next, we need to call context.ExecuteQuery and that actually kicks off the batch retrieval. We retrieve the property values from the server and add to pst box.

When the above code is compiled and executed, you will see the following output −

Content Execute Query

Cpck the Load button and you will see that we get both, the title and count of the psts.

It enables our project setup to use the Cpent Object Model to check the loading resources using the load method.

Advertisements