Monday, July 8, 2013

What in the world is Client object Model in SPS2010


As per the MSDN......

In SharePoint 2010 there are a number of object models that can be used by developers to access the server. The Client Object Model (Client OM) is a unified model which uses the same or similar programming concepts as the Server Object Model (Server OM). The Client OM can be accessed via web services, via a client (JavaScript) API, and via REST.



These are new client APIs that allow you to interact with SharePoint sites from script that executes in the browser, from code (no earlier than Microsoft .NET Framework 3.5) that executes in a .NET managed application, or from code that executes in a Microsoft Silverlight 2.0 application. The new ECMAScript (JavaScript, JScript), .NET managed, and Silverlight client object models each provide a subset of the server object model that is defined in Microsoft.SharePoint.dll, including objects that correspond to major objects at the site-collection level or lower in the SharePoint Foundation hierarchy. To improve security and performance, the client object models focus on the most relevant APIs for client-side development, and do not contain all the types and members that are represented in the server object model. Limiting the size of the client libraries reduces the amount of time that is required to download the libraries in the Silverlight and JavaScript contexts. In addition, the new APIs are designed to minimize the number of round trips that must be implemented for common actions. The object models provide a consistent and easy-to-use, object-oriented system for interoperating with SharePoint data from a remote client or server




here i have created a console application which does various operations on a site using the managed client object model.. pls look for the comments in the code to identify each operation ..


Note: Pls add 2 DLL's

1. Microsoft.sharepoint.Client DLL
2.Microsoft .Sharepoint.Client.Runtime..

Create a console app and try the following out..



using System;
using System.Collections.Generic;using System.Linq;using System.Text;using Microsoft.SharePoint.Client; 
namespace NINOSCOM{

class Program{static void Main(string[] args){

ClientContext Ccontext = new ClientContext("http://itessk-sps2010:8888/sites/NinosTestSite");
//create web object Web objweb = Ccontext.Web;
//Load webCcontext.Load(objweb); //create List object .. Only Use GETBYTITLE OR GETBYID
List objList = objweb.Lists.GetByTitle("Employees");
CamlQuery caml = new CamlQuery();
caml.ViewXml ="<View/>";
//CREATE LIST ITEM COLLECTION (ONLY ALLOWED CAML AS ARGUMENT

ListItemCollection objlistcol = objList.GetItems(caml);
//LOAD THE OBJECTSCcontext.Load(objList);
Ccontext.Load(objlistcol);
Ccontext.ExecuteQuery();
Console.WriteLine("COM Learning");
Console.WriteLine("Title: {0}", objweb.Title);
foreach (ListItem listItem in objlistcol)
Console.WriteLine("Id: {0} Title: {1}", listItem.Id, listItem["Title"]);
Console.ReadLine();
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem olistItem = objList.AddItem(itemCreateInfo);

Ccontext.Load(olistItem);

Console.WriteLine("Enter the new employee ..."); 
olistItem["Title"] = Console.ReadLine();olistItem.Update();
Ccontext.ExecuteQuery();
 

// Create a list.

Console.WriteLine("Enter the new List Name "); 
 

ListCreationInformation listCreationInfo = new ListCreationInformation();
listCreationInfo.Title =Console.ReadLine();
Console.WriteLine("Enter the List Description ");
listCreationInfo.Description =Console.ReadLine();
listCreationInfo.TemplateType = (int)ListTemplateType.GenericList;
List newlist = objweb.Lists.Add(listCreationInfo);
Ccontext.Load(newlist);
Ccontext.ExecuteQuery();

//create custom fields in the new list // Add fields to the list.
Field field1 = newlist.Fields.AddFieldAsXml(
@"<Field Type='Choice'DisplayName='Category'
Format='Dropdown'>
<Default>Attack</Default>
<CHOICES>
<CHOICE>Attack</CHOICE>
<CHOICE>Defense</CHOICE>
<CHOICE>Keeper</CHOICE>
<CHOICE>Coach</CHOICE>
</CHOICES>
</Field>"
,
true, AddFieldOptions.DefaultValue);
Field field2 = newlist.Fields.AddFieldAsXml(
@"<Field Type='Number'DisplayName='Estimate'/>"
,
true, AddFieldOptions.DefaultValue); 

// Display all list in a site
ListCollection lists = objweb.Lists;
IEnumerable<List> newListCollection = Ccontext.LoadQuery(lists.Include(
list => list.Title,
list => list.Id,
list => list.Hidden));
Ccontext.ExecuteQuery();

foreach (List list in newListCollection)
Console.WriteLine("Title: {0} Id: {1}",list.Title.PadRight(40), list.Id.ToString("D"));
// EDIT OR UPDATE A LIST ITEM List objList1 = objweb.Lists.GetByTitle("real Madrid");Ccontext.Load(objList1);

CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml =@"<View><Query>
<Where>
<Eq>
<FieldRef Name='Category'/>
<Value Type='Text'>Attack</Value>
</Eq>
</Where>
</Query>
<RowLimit>100</RowLimit>
</View>"
;
ListItemCollection listItems = objList1.GetItems(camlQuery);
Ccontext.Load(
listItems,
items => items.Include(
item => item[
"Category"],item => item["Estimate"]));
Ccontext.ExecuteQuery();

foreach (ListItem listItem in listItems){
listItem["Estimate"] = 78;
listItem.Update();
}
Ccontext.ExecuteQuery();

Console.ReadLine(); 
}
}
}

No comments:

Post a Comment

Rename a Web Application in Sharepoint : A solution using powershell

Here is a small script that will enable you all to change a SharePoint Web Application name . We can use the following SharePoint Pow...