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.

Thursday, August 09, 2012

Binding Data from Document Library to SpGridView using Client Object Model without Dataset


 
<%@ 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="VisualWebPart1UserControl.ascx.cs" Inherits="COMGridDocuments.VisualWebPart1.VisualWebPart1UserControl" %>

<SharePoint:SPGridView runat="server" ID="sgvDocuments" AutoGenerateColumns="false">
<Columns>
<asp:HyperLinkField HeaderText="Document Name" DataTextField="LinkFilename" DataNavigateUrlFields="EncodedAbsUrl" DataNavigateUrlFormatString="{0}" Target="_blank" />
</Columns>
</SharePoint:SPGridView>

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Linq;
using Microsoft.SharePoint.Client;
using System.Collections.Generic;
using Microsoft.SharePoint;

namespace COMGridDocuments.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            sgvDocuments.DataSource = getData();
            sgvDocuments.DataBind();
        }

        public class DocumentsData
        {
            public string LinkFilename { get; set; }
            public string EncodedAbsUrl { get; set; }
        }

        public List<DocumentsData> getData()
        {
            List<DocumentsData> MyDocumentsData = new List<DocumentsData>();
            using (ClientContext clientContext = new ClientContext(SPContext.Current.Web.Url))
            {
                List list = clientContext.Web.Lists.GetByTitle("NewDocuments");
                var camlQuery = new CamlQuery { ViewXml = "<View Scope='Recursive'><Query><OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy></Query><ViewFields><FieldRef Name='LinkFilename' /><FieldRef Name='EncodedAbsUrl' /></ViewFields></View>" };
                Microsoft.SharePoint.Client.ListItemCollection myColl = list.GetItems(camlQuery);
                clientContext.Load(myColl,li => li.Include(pi => pi["LinkFilename"], pi => pi["EncodedAbsUrl"]));
                clientContext.ExecuteQuery();
                foreach (Microsoft.SharePoint.Client.ListItem item in myColl)
                {
                    MyDocumentsData.Add(new DocumentsData()
                    {
                        LinkFilename = item["LinkFilename"].ToString(),
                        EncodedAbsUrl = item["EncodedAbsUrl"].ToString()
                    });
                }
            }
            return MyDocumentsData;


        }
    }
}




No comments:

Post a Comment