The Client OM works by sending an XML Request. The server will return a JSON response which is converted to the appropriate Object Model.
For example:
You can see that the Titles are modified for those with even number IDs.
Here we can try inserting a new item into the Tasks list.
On executing the above code and retrieving the results, you can see the output as below:
Supported Languages
Following are the programming language/platforms supported for Client Object Model:- .NET Languages (C#, VB.NET etc.)
- Silverlight
- Scripting Languages (JavaScript, Jscript)
Core Assemblies
There are two assemblies to be referred for working with the Client Object Model.- Microsoft.SharePoint.Client.dll
- Microsoft.SharePoint.Client.Runtime.dll
Classes inside Client Object Model
In C#, comparing with classes of the Server Object Model, we can see that Client Object Model has similar classes with a suffix in the namespace and no SP prefix in the class name.For example:
SPSite
in the Server Object Model is represented in the Client OM as Site
with namespace Microsoft.SharePoint.Client
.Client Object Model | Server Object Model |
Microsoft.SharePoint.Client.ClientContext | SPContext |
Microsoft.SharePoint.Client.Site | SPSite |
Microsoft.SharePoint.Client.Web | SPWeb |
Microsoft.SharePoint.Client.List | SPList |
More Examples with Client Object Model
Here I would like to list some examples using the Client Object Model. For starting with the examples, please do the following:- Create a Windows Application
- Change the Target Framework to .NET 4.0
- Add reference to Microsoft.SharePoint.Client, Microsoft.SharePoint.Client.Runtime
Tasks
list. We will be changing the data items during our session.1. Get List Items
Here we are querying the list items of theTasks
list.
Collapse | Copy Code
ClientContext context = new ClientContext(ServerText.Text);
List list = context.Web.Lists.GetByTitle("Tasks");
CamlQuery query = new CamlQuery();
query.ViewXml = "<View/>";
ListItemCollection items = list.GetItems(query);
context.Load(list);
context.Load(items);
context.ExecuteQuery();
After executing the code, the result can be stored into a DataTable
as shown below.
Collapse | Copy Code
DataTable table = new DataTable();
table.Columns.Add("Id");
table.Columns.Add("Title");
foreach (ListItem item in items)
table.Rows.Add(item.Id, item["Title"]);
datagrid.DataSource = table;
On my machine, the data retrieved is shown below:2. Update List Items
Here I would like to show the modification code. All the titles are appended with two asterisks whose IDs are even number.
Collapse | Copy Code
ClientContext context = new ClientContext(ServerText.Text);
List list = context.Web.Lists.GetByTitle("Tasks");
CamlQuery query = new CamlQuery();
query.ViewXml = "<View/>";
ListItemCollection items = list.GetItems(query);
context.Load(items);
context.ExecuteQuery();
foreach(ListItem item in items)
if ((item.Id % 2) == 0)
{
item["Title"] += "**";
item.Update();
}
context.ExecuteQuery();
After executing the query, please refresh the data grid using the Get Data button. You can see the following result.You can see that the Titles are modified for those with even number IDs.
3.Insert an item
Here we can try inserting a new item into the Tasks list.
Collapse | Copy Code
ClientContext context = new ClientContext(ServerText.Text);
Web web = context.Web;
List list = web.Lists.GetByTitle("Tasks");
ListItemCreationInformation newItem = new ListItemCreationInformation();
ListItem listItem = list.AddItem(newItem);
listItem["Title"] = "New Item Created through C#";
listItem.Update();
context.ExecuteQuery();
You can see that we are using a new class named ListItemCreationInformation
along with the ListItem
class. This information will be recorded and passed to the server once the ExecuteQuery()
method is called.On executing the above code and retrieving the results, you can see the output as below:
6. Update an Item
The Update operation is next in the series of the CRUD pattern. Already we have seen updating the Title. Here you can see how to update the Status.
Collapse | Copy Code
ClientContext context = new ClientContext(ServerText.Text);
List list = context.Web.Lists.GetByTitle("Tasks");
CamlQuery query = new CamlQuery();
query.ViewXml = "<View/>";
ListItemCollection listItems = list.GetItems(query);
context.Load(listItems);
context.ExecuteQuery();
ListItem item = listItems[listItems.Count - 1];
item["Status"] = "In Progress";
item.Update();
context.ExecuteQuery();
On executing the code, you can see that the last item was updated.7. Delete an Item
Now we can try deleting an item from the List. Here is the code to achieve that.
Collapse | Copy Code
ClientContext context = new ClientContext(ServerText.Text);
List list = context.Web.Lists.GetByTitle("Tasks");
ListItemCollection listItems = list.GetItems(new CamlQuery() { ViewXml = "<View/>" });
context.Load(listItems);
context.ExecuteQuery();
listItems[listItems.Count - 1].DeleteObject();
context.ExecuteQuery();
We need to call the DeleteObject()
method of the item followed by the ExecuteQuery()
.10. Specifying Credentials
You can specify user credentials while accessing the SharePoint server. The propertyCredentials
is for this purpose.
Collapse | Copy Code
ClientContext context = new ClientContext(ServerText.Text);
context.Credentials = new NetworkCredential("User", "Password");
Web web = context.Web;
context.Load(web);
context.ExecuteQuery();
MessageBox.Show(web.Title);
No comments:
Post a Comment