We have different object models in SharePoint. CSOM is one of the model.
To use this, we need an asssembly i.e. Microsoft.SharePoint.Cleint.dll.
Below are few exercises to get familiar.
All these examples are tested in Windows Application.
Example 1: Get List of Tasks from SharePoint Tasks list and bind those to GridView.
ClientContext context = new ClientContext(webUrl);
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();
DataTable table = new DataTable();
table.Columns.Add("Id");
table.Columns.Add("Title");
foreach (ListItem item in items)
table.Rows.Add(item.Id, item["Title"]);
displayGrid.DataSource = table;
Example 2: Modify the Title of Task List Items
ClientContext context = new ClientContext(webUrl);
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)
{
item["Title"] = "**";
item.Update();
}
context.ExecuteQuery();
MessageBox.Show("Updated Successfully");
Example 3: Get only two items from the Tasks list and bind those to Grid.
ClientContext context = new ClientContext(webUrl);
Web web = context.Web;
List list = web.Lists.GetByTitle("Tasks");
CamlQuery query = new CamlQuery();
query.ViewXml = "<View><RowLimit>2</RowLimit></View>";
ListItemCollection listItems = list.GetItems(query);
context.Load(listItems);
context.ExecuteQuery();
DataTable table = new DataTable();
table.Columns.Add("Id");
table.Columns.Add("Title");
foreach (ListItem item in listItems)
table.Rows.Add(item.Id, item["Title"]);
displayGrid.DataSource = table;
Note: Load() and ExecuteQuery() methods are crucial in CSOM.
To use this, we need an asssembly i.e. Microsoft.SharePoint.Cleint.dll.
Below are few exercises to get familiar.
All these examples are tested in Windows Application.
Example 1: Get List of Tasks from SharePoint Tasks list and bind those to GridView.
ClientContext context = new ClientContext(webUrl);
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();
DataTable table = new DataTable();
table.Columns.Add("Id");
table.Columns.Add("Title");
foreach (ListItem item in items)
table.Rows.Add(item.Id, item["Title"]);
displayGrid.DataSource = table;
Example 2: Modify the Title of Task List Items
ClientContext context = new ClientContext(webUrl);
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)
{
item["Title"] = "**";
item.Update();
}
context.ExecuteQuery();
MessageBox.Show("Updated Successfully");
Example 3: Get only two items from the Tasks list and bind those to Grid.
ClientContext context = new ClientContext(webUrl);
Web web = context.Web;
List list = web.Lists.GetByTitle("Tasks");
CamlQuery query = new CamlQuery();
query.ViewXml = "<View><RowLimit>2</RowLimit></View>";
ListItemCollection listItems = list.GetItems(query);
context.Load(listItems);
context.ExecuteQuery();
DataTable table = new DataTable();
table.Columns.Add("Id");
table.Columns.Add("Title");
foreach (ListItem item in listItems)
table.Rows.Add(item.Id, item["Title"]);
displayGrid.DataSource = table;
Note: Load() and ExecuteQuery() methods are crucial in CSOM.
Thanks for providing important methods in CSOM
ReplyDeleteSharePoint 2013 Online Training
Hi,
ReplyDeleteCan we use aso.net gridview here in this csom? I think, v R writing the code inside content editor WP or script editor wp