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, March 06, 2012

Binding Data from Tasks List To SpgridView using Client Object Model without DataSet


Ascx Code
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="COMSpGridViewSampleUserControl.ascx.cs" Inherits="COMSpGridViewSample.COMSpGridViewSample.COMSpGridViewSampleUserControl" %>

<SharePoint:SPGridView ID="sgvTasks" runat="server" AutoGenerateColumns=false>
<Columns>
<asp:HyperLinkField HeaderText="Task Name" DataTextField="Title" DataNavigateUrlFields="TaskURL" DataNavigateUrlFormatString="{0}" />
<asp:BoundField HeaderText="Status" DataField="Status" />
<asp:BoundField HeaderText="Priority" DataField="Priority" />
</Columns>
</SharePoint:SPGridView>

Logic Implementation
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.Client;
using System.Security;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using Microsoft.SharePoint;
namespace COMSpGridViewSample.COMSpGridViewSample
{
    public partial class COMSpGridViewSampleUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            sgvTasks.DataSource = getData();
            sgvTasks.DataBind();
        }

       public class TasksData
       {
           public string Title {get; set;}
           public string TaskURL {get; set;}
           public string Status {get; set;}
           public string Priority {get; set;}
       }

       public List<TasksData> getData()
       {

           List<TasksData> MyTasksData = new List<TasksData>();
           using (ClientContext clientContext = new ClientContext(SPContext.Current.Web.Url))
           {
                List list = clientContext.Web.Lists.GetByTitle("Tasks");
               var camlQuery = new CamlQuery { ViewXml = "<View/>" };

            Microsoft.SharePoint.Client.ListItemCollection listItems = list.GetItems(camlQuery);
               clientContext.Load(listItems);
            clientContext.ExecuteQuery();
            foreach (Microsoft.SharePoint.Client.ListItem item in listItems)
            {
                MyTasksData.Add(new TasksData()
                {
                 Title=item["Title"].ToString(), 
                 Status=(item["Status"]==null) ? "Data UnAvailable" : item["Status"].ToString(),
                 Priority = (item["Priority"] == null) ? "Data UnAvailable" : item["Priority"].ToString(),
                 TaskURL=SPContext.Current.Web.Url +"/Lists/Tasks/DispForm.aspx?ID="+item["ID"].ToString()+"&Source="+Page.Request.Url
                });
            }
          }
           return MyTasksData;


       }
    }
}




No comments:

Post a Comment