Saturday, May 29, 2010

Client Object Model – Managed .NET Applications

Accessing Client Object Model

  • For .NET client applications (Located in ISAPI folder)
    • DLLs required for SharePoint client applications are located under SharePoint Root folder (under SharePoint root)
      • Microsoft.SharePoint.Client
      • Microsoft.SharePoint.Client.Runtime
    • Add references to the above DLLs in Visual Studio as shown below


//Initialize the context object using constructor
ClientContext _context = new ClientContext("http://localhost:9999/ittp/training/");

Web _site = _context.Web;
            
_context.Load(_site);
_context.ExecuteQuery();

//Access the _site properties here

//.............

//Dispose off the object
_context.Dispose();


One important note that, after loading the site context as above, the object underneath are not yet available. For example, lists under the _site, are not available for us to access immediately. We will have to load lists using the context object. Refer to the following code snippet


ListCollection _lists = _context.Web.Lists;
_context.Load(_lists);
_context.ExecuteQuery();

//Now we can access properties on _lists object using _lists.Count, for example

A point to remember here is, when an object is loaded using either Load() or LoadQuery() methods, all the properties of the object in question will be loaded. Sometimes, this may not be necessary. Constructors of Load() or LoadQuery() methods also allows us to load objects along with specified properties. This greatly improves the performance while retriving the object as only teh specified properties will be returned. Here is an example.

_context.Load(_lists, _l => _l.Include(_l => _l.Title);
_context.ExecuteQuery();





Technorati Tags: ,,,

,

No comments:

Post a Comment