Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Tuesday, February 28, 2012

Binding Data from SharePoint List to SpGridView using Client Object Model



using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Client;
using System.Linq;
using System.Data;
namespace PrasadVWebPart.PrasadVWebPart
{
    [ToolboxItemAttribute(false)]
    public class PrasadVWebPart : WebPart
    {
        string strError = string.Empty;
        SPGridView sgvTasks;
        DataSet dSet = new DataSet();
        DataTable dt = new DataTable();
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(strError);
            try
            {
                writer.Write("<Style type='Text/Css'>");
                writer.Write(".GridClass");
                writer.Write("{");
                writer.Write("text-Align:Left;font-Weight:bold;");
                writer.Write("}");
                writer.Write("</Style>");
                sgvTasks.RenderControl(writer);
            }
            catch (Exception ex)
            {

                writer.Write(ex.ToString());
            }
        }
        protected override void CreateChildControls()
        {
            try
            {
                sgvTasks = new SPGridView();
                sgvTasks.ID = "sgvTasks";
                sgvTasks.AutoGenerateColumns = false;
                this.Controls.Add(sgvTasks);

                SPWeb currentWeb = SPContext.Current.Web;
                HyperLinkField hTaskName = new HyperLinkField();
                hTaskName.HeaderStyle.CssClass = "GridClass";
                hTaskName.HeaderText = "Task Name";
                hTaskName.DataNavigateUrlFields = new String[] { "ID" };
                hTaskName.DataTextField = "Title";
                hTaskName.DataNavigateUrlFormatString = currentWeb.Url + "/Lists/Tasks/DispForm.aspx?ID={0}&Source=" + Page.Request.Url;
                sgvTasks.Columns.Add(hTaskName);

                BoundField bStatus = new BoundField();
                bStatus.HeaderStyle.CssClass = "GridClass";
                bStatus.HeaderText = "Status";
                bStatus.DataField = "Status";
                sgvTasks.Columns.Add(bStatus);

                BoundField bPriority = new BoundField();
                bPriority.HeaderStyle.CssClass = "GridClass";
                bPriority.HeaderText = "Priority";
                bPriority.DataField = "Priority";
                sgvTasks.Columns.Add(bPriority);

                BoundField bCreatedDate = new BoundField();
                bCreatedDate.HeaderStyle.CssClass = "GridClass";
                bCreatedDate.HeaderText = "Created Date";
                bCreatedDate.DataField = "Created";
                sgvTasks.Columns.Add(bCreatedDate);
                sgvTasks.DataSource = getTasksData();
                sgvTasks.DataBind();

               
            }
            catch (Exception ex)
            {
                strError += ex.ToString();
            }
        }
        public DataSet getTasksData()
        {
            try
            {
                dt.Columns.Add("Title");
                dt.Columns.Add("Status");
                dt.Columns.Add("Priority");
                dt.Columns.Add("Created");
                dt.Columns.Add("ID");
                ClientContext context = new ClientContext(SPContext.Current.Web.Url);
                using (context)
                {
                    Web web = context.Web;
                    List list = web.Lists.GetByTitle("Tasks");
                    CamlQuery query = new CamlQuery();
                    query.ViewXml = @"<View><Query><OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy></Query></View>";
                    Microsoft.SharePoint.Client.ListItemCollection items = list.GetItems(query);
                    context.Load(items);
                    context.ExecuteQuery();
                    foreach (var item in items)
                    {
                        string strTitle = string.Empty;
                        string strStatus = string.Empty;
                        string strPriority = string.Empty;
                        string strID = string.Empty;
                        string strCreatedDate = string.Empty;
                        if (item["Title"].ToString() != "")
                        {
                            strTitle = item["Title"].ToString();
                        }
                        else
                        {
                            strTitle = "--";
                        }
                        if (item["Status"].ToString() != "")
                        {
                            strStatus = item["Status"].ToString();
                        }
                        else
                        {
                            strStatus = "--";
                        }
                        if (item["Priority"].ToString() != "")
                        {
                            strPriority = item["Priority"].ToString();
                        }
                        else
                        {
                            strPriority = "--";
                        }
                        strID = item["ID"].ToString();
                        strCreatedDate = item["Created"].ToString();
                        DataRow drow = dt.NewRow();
                        drow["Title"] = strTitle;
                        drow["Priority"] = strPriority;
                        drow["Status"] = strStatus;
                        drow["ID"] = strID;
                        drow["Created"] = strCreatedDate;
                        dt.Rows.Add(drow);

                    }
       
                }
                dSet.Tables.Clear();
                dSet.Tables.Add(dt);
                return dSet;

            }
            catch (Exception ex)
            {
                strError += ex.ToString();
                return null;
            }
        }
    }
}


No comments:

Post a Comment