Understanding client object model fundamentals
SharePoint 2010 introduced the CSOM as a way to program against a Windows Communication Foundation (WCF) endpoint in SharePoint by using a style that mimicked server-side API development. Prior to the introduction of CSOM, SharePoint developers had only a limited set of web services available for use from client-side code. With the introduction of CSOM, developers had a way to access a significant portion of core SharePoint functionality from C# (called the “managed” client object model), JavaScript, and Silverlight.
Understanding client object model fundamentals
In SharePoint 2013, CSOM has been greatly expanded to include functionality from workloads outside of SharePoint Foundation. Using CSOM, app developers now have client-side access to Enterprise Search, Business Connectivity Services, Managed Metadata, Social, and much more. This additional functionality is made available through separate assemblies and libraries that can be referenced in your apps.
image: http://tutorial.programming4.us/image/1411/Working%20with%20the%20CSOM_1.jpg
![The client object model provides a programmatic interface to make web service calls against SharePoint by passing in an XML request and receiving a JSON response.](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_spPdF3OJoCpBRabltdXFYfKT1RjeJKNdH-ca74XZsrVFUgGR2L2rrFyKu-P3MQpX9CxrDqDOxNCHAvJX3sq46me_Q1EjOAIV9zah_cjmGqrdLjPSm4vkmPNtJyPT3uSwIsGbMnVhpzFbF956fqAJs=s0-d)
Figure 1. The client object model provides a programmatic interface to make web service calls against SharePoint by passing in an XML request and receiving a JSON response.
Understanding contexts
Example . Creating contexts
//Managed Client Object Model string appWebUrl = Page.Request["SPAppWebUrl"]; using (ClientContext ctx = new ClientContext(appWebUrl)) { Site siteCollection = ctx.Site; ctx.Load(siteCollection); ctx.ExecuteQuery(); string url = siteCollection.Url; } //JavaScript Client Object Model var siteCollection; var ctx = new SP.ClientContext.get_current(); siteCollection = ctx.get_site(); ctx.load(site); ctx.executeQueryAsync(success, failure); function success() { string url = siteCollection.get_url(); } function failure() { alert("Failure!"); }
The SP.ClientContext object in the JavaScript client object model inherits from the SP.ClientContext Runtime object and provides equivalent functionality to the ClientContext class found in the managed client object model. Like the managed model, you can get a runtime context in the JavaScript model by using the SP.ClientContext class and passing a URL. In Example 1, the context is created by using the get_current method, which returns a client context for the app web.
Loading and executing operations
Executing the Load or LoadQuery method does not cause the client to communicate with the server. Instead, it adds the load operation to a batch that will be executed on the server. In fact, you can execute multiple load methods (as well as other operations) before calling the server. Each operation is batched waiting for your code to initiate communication with server. To execute the batched operations, your code must call the ExecuteQuery method in managed code or the Execute QueryAsync method in JavaScript. The ExecuteQuery method creates an XML request and passes it to the Client.svc service synchronously. The ExecuteQueryAsync method sends the request asynchronously. Designated success and failure callback methods receive notification when the asynchronous batch operation is complete.
The sample code in Example 1 uses the Load method to request an object representing the current site collection. After an object is returned, you can generally access any of the scalar properties associated with the object. In cases for which you do not want to return all of the scalar properties for a given object, you can designate the properties to return. In the managed object, properties are designated by providing a series of lambda expressions. In the JavaScript object model, properties are designated by name. This technique helps to minimize the amount of data sent between the client and server. The following code shows how to request only the Title and ServerRelativeUrl properties for a site collection object:
//Managed CSOM references properties via lambda expressions ctx.Load(site, s=>s.Title, s=>s.ServerRelativeUrl); //JavaScript CSOM references properties by name ctx.Load(site, "Title", "ServerRelativeUrl");
Read more at http://tutorial.wmlcloud.com/windows_server/sharepoint-2013---working-with-the-csom-(part-1)---understanding-client-object-model-fundamentals.aspx#FHgS2tvTXDctHk8z.99
No comments:
Post a Comment