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.

Monday, March 11, 2013

Binding Data from SharePoint List to GridView using Three Tier Architecture in SharePoint 2013 Visual WebPart without a Single Line of Loop



Ascx
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.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=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="BindGridThreeTier.ascx.cs" Inherits="BindGridThreeTier.BindGridThreeTier.BindGridThreeTier" %>
 <asp:GridView ID="dgvTasks" runat="server" CellPadding="4" ForeColor="#333333" AutoGenerateColumns="False" width="100%">
    <AlternatingRowStyle BackColor="White" />
    <EditRowStyle BackColor="#2461BF" />
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <sortedascendingcellstyle backcolor="#F5F7FB" />
    <sortedascendingheaderstyle backcolor="#6D95E1" />
    <sorteddescendingcellstyle backcolor="#E9EBEF" />
    <sorteddescendingheaderstyle backcolor="#4870BE" />
    <columns>
        <asp:BoundField DataField="Title" HeaderText="Title"></asp:BoundField>
        <asp:BoundField DataField="Status" HeaderText="Status"></asp:BoundField>
        <asp:BoundField DataField="Priority" HeaderText="Priority"></asp:BoundField>
        <asp:BoundField DataField="StartDate" DataFormatString="{0:G}"
HeaderText="Start Date"></asp:BoundField>
        <asp:BoundField DataField="DueDate" DataFormatString="{0:G}"
HeaderText="End Date"></asp:BoundField>
        <asp:BoundField DataField="AssignedTo" HeaderText="Assigned To"></asp:BoundField>
    </columns>
</asp:GridView>
Ascx.cs
using System;
using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Security;
using System.Data;

namespace BindGridThreeTier.BindGridThreeTier
{
    [ToolboxItemAttribute(false)]
    public partial class BindGridThreeTier : WebPart
    {
        // Uncomment the following SecurityPermission attribute only when doing Performance Profiling on a farm solution
        // using the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready
        // for production. Because the SecurityPermission attribute bypasses the security check for callers of
        // your constructor, it's not recommended for production purposes.
        // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]
        public BindGridThreeTier()
        {
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    getData();
                }
                catch (Exception Ex)
                {

                     Page.Response.Write(Ex.ToString());
                }
               
            }
        }

        public void getData()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                DataBaseLayer db = new DataBaseLayer();
                BusinessLayer bl = new BusinessLayer();
                bl.strTitle = "Title";
                bl.strStatus = "Status";
                bl.strPriority = "Priority";
                bl.strStartDate = "StartDate";
                bl.strEndDate = "DueDate";
                bl.strAssignedTo = "AssignedTo";
                DataTable dt = db.getTasksData(bl.strTitle, bl.strStatus, bl.strPriority, bl.strStartDate, bl.strEndDate, bl.strAssignedTo);
                if (dt.Rows.Count > 0)
                {
                    dgvTasks.DataSource = dt;
                    dgvTasks.DataBind();
                }
            });
        }
    }
}
BusinessLayer.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BindGridThreeTier
{
    class BusinessLayer
    {
        string Title;
        string Status;
        string Priority;
        string StartDate;
        string EndDate;
        string AssignedTo;

        public string strTitle
        {
            get
            {
                return Title;
            }

            set
            {
                Title = value;
            }
        }

        public string strStatus
        {
            get
            {
                return Status;
            }

            set
            {
                Status = value;
            }
        }

        public string strPriority
        {
            get
            {
                return Priority;
            }

            set
            {
                Priority = value;
            }
        }

        public string strStartDate
        {
            get
            {
                return StartDate;
            }

            set
            {
                StartDate = value;
            }
        }

        public string strEndDate
        {
            get
            {
                return EndDate;
            }

            set
            {
                EndDate = value;
            }
        }

        public string strAssignedTo
        {
            get
            {
                return AssignedTo;
            }

            set
            {
                AssignedTo = value;
            }
        }
    }
}
DataBaseLayer.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint;
using System.Security;
using System.Data;

namespace BindGridThreeTier
{
    class DataBaseLayer
    {
        public DataTable getTasksData(string Title, string Status, string Priority, string StartDate, string EndDate, string AssignedTo)
        {
            SPQuery sQuery = new SPQuery();
            sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
            sQuery.ViewFields = "<FieldRef Name='" + Title + "' /><FieldRef Name='" + Status + "' /><FieldRef Name='" + Priority + "' /><FieldRef Name='" + StartDate + "' /><FieldRef Name='" + EndDate + "' /><FieldRef Name='" + AssignedTo + "' />";
            sQuery.ViewFieldsOnly = true;
            SPListItemCollection myColl = SPContext.Current.Web.Lists["Tasks"].GetItems(sQuery);
            return myColl.GetDataTable();
        }
    }
}

No comments:

Post a Comment