In this post I'm going to show how to retrieve Task details using Client Object model.
Step: 1 Add the SharePoint Reference
Refer this article
Step: 2 Add the SharePoint namespace
Step: 1 Add the SharePoint Reference
Refer this article
Step: 2 Add the SharePoint namespace
using Microsoft.SharePoint.Client;Create table
Retrieving Task
void CreateTaskTable(){try{dtTask = new DataTable();dtTask.Columns.Add("TaskID", typeof(string));dtTask.Columns.Add("Title", typeof(string));dtTask.Columns.Add("Body", typeof(string));dtTask.Columns.Add("ListName", typeof(string));dtTask.Columns.Add("Status", typeof(string));dtTask.Columns.Add("StartDate", typeof(string));dtTask.Columns.Add("DueDate", typeof(string));dtTask.Columns.Add("AssignedTo", typeof(string));dtTask.Columns.Add("Priority", typeof(string));dtTask.Columns.Add("PercentComplete", typeof(string));dtTask.Columns.Add("Created", typeof(string));dtTask.Columns.Add("Modified", typeof(string));dtTask.Columns.Add("Created By", typeof(string));dtTask.Columns.Add("Modified By", typeof(string));}catch (Exception){throw;}}
public DataTable GetTaskCollection(string Url, string UserName, string Password, string SiteName, string taskList)
{try{ClientContext context = new ClientContext(Url);context.Credentials = new NetworkCredential(UserName, Password);context.Load(web);List list = web.Lists.GetByTitle(taskList);CamlQuery camlQuery = new CamlQuery();camlQuery.ViewXml = "<View/>";ListItemCollection listItems = list.GetItems(camlQuery);CreateTaskTable();context.Load(listItems,items => items.Include(item => item.Id,item => item["Title"],item => item["Status"],item => item["StartDate"],item => item["Modified"],item => item["Created"],item => item["Priority"],item => item["PercentComplete"],item => item["AssignedTo"],item => item["Body"],item => item["DueDate"]));context.ExecuteQuery();foreach (ListItem item in listItems){DataRow dr = dtTask.NewRow();dr["TaskID"] = item.Id;dr["Title"] = item["Title"];dr["Status"] = item["Status"];if (item["StartDate"] != null && item["StartDate"].ToString() != ""){DateTime dt = Convert.ToDateTime(item["StartDate"]);}if (item["DueDate"] != null && item["DueDate"].ToString() != ""){DateTime dt = Convert.ToDateTime(item["DueDate"]);}dr["Created"] = item["Created"];dr["Modified"] = item["Modified"];dr["Priority"] = item["Priority"];dr["PercentComplete"] = item["PercentComplete"];dr["Body"] = item["Body"];dr["Reference"] = item["Reference"];dr["ListName"] = taskList;if (item["AssignedTo"] != null){if (item["AssignedTo"].ToString() != ""){FieldUserValue uservalue = (FieldUserValue)item["AssignedTo"];dr["AssignedTo"] = uservalue.LookupValue;}}dtTask.Rows.Add(dr);}}catch (Exception){return null;}return dtTask;}
No comments:
Post a Comment