Social Icons

Pages

Thursday, November 29, 2012

SharePoint List C# - Read the task details using OM

In this article I am going to discuss how you can retrieve the task details from share point task list. Here is the sample code

            Create data table for task details
void CreateTaskTable()
        {
            try
            {
                dtTask = new DataTable();
                dtTask.Columns.Add("Tittle", typeof(string));
                dtTask.Columns.Add("Status", typeof(string));
                dtTask.Columns.Add("StartDate", typeof(string));
                dtTask.Columns.Add("Modified", typeof(string));
                dtTask.Columns.Add("Created", typeof(string));
                dtTask.Columns.Add("Priority", typeof(string));
                dtTask.Columns.Add("PercentComplete", typeof(string));
                dtTask.Columns.Add("AssignedTo", typeof(string));
                dtTask.Columns.Add("Body", typeof(string));
                dtTask.Columns.Add("DueDate", typeof(string));
            }
            catch (Exception)
            {
               
                throw;
            }
        }
            Retrieve the task details
public void RetriveTask()
        {
            CreateTaskTable();
            SPSite site = new SPSite(SiteURL);
            SPWeb web = site.AllWebs[0];
            SPList list = web.Lists[List];
            SPQuery myquery = new SPQuery();
            myquery.Query = "";

            // if you don’t insert query (myquery.Query ="") you will get all items
            SPListItemCollection ItemCollection = list.GetItems(myquery);
            foreach (SPItem item in ItemCollection)
            {
                DataRow dr= dtTask.NewRow();
                dr["Tittle"] = item["Title"];
                dr["Status"] = item["Status"];
                dr["StartDate"] = item["StartDate"];
                dr["Modified"] = item["Modified"];
                dr["Created"] = item["Created"];
                dr["Priority"] = item["Priority"];
                dr["PercentComplete"] = item["PercentComplete"];
                dr["AssignedTo"] = item["AssignedTo"];
                dr["Body"] = item["Body"];
                dr["DueDate"] = item["DueDate"];
                dtTask.Rows.Add(dr);
            }
 

No comments:

Post a Comment