Search This Blog

Monday, September 22, 2014

Work Flow Task- SPGridview Code

DownLoad this project Please click here Workflow Task


<%@ 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="MyTaskEventUserControl.ascx.cs"Inherits="MyWorkFLowTask.MyTaskEvent.MyTaskEventUserControl" %>
<style>
    .pagination td table
    {
        floatright;
    }
</style>
<asp:UpdatePanel ID="upMyTasks" runat="server">
    <ContentTemplate>
        <div class="panelSection actioncontainerfixed">
            <h2 class="headercolor">
                <span class="actionrequired-img"></span>My Task</h2>
            <div class="actionrequired-table">
                <asp:GridView ID="spgrdMyTasks" runat="server" CssClass="clsTable" AutoGenerateColumns="False"
                    RowStyle-CssClass="minutesrow" AlternatingRowStyle-CssClass="minutesrow" AllowPaging="true"
                    AllowSorting="true" PageSize="4" PagerSettings-Mode="NextPrevious" PagerSettings-NextPageImageUrl="../_layouts/15/Branding/images/right-nav-arrow.png"
                    PagerSettings-PreviousPageImageUrl="../_layouts/15/Branding/images/left-nav-arrow.png"
                    PagerStyle-CssClass="pagination" OnSorting="spgrdMyTasks_Sorting"OnPageIndexChanging="spgrdMyTasks_PageIndexChanging"
                    EmptyDataText="No tasks to show" OnRowDataBound="spgrdMyTasks_RowDataBound"OnRowCreated="spgrdMyTasks_RowCreated">
                    <Columns>
                        <asp:TemplateField HeaderText="Request Name" SortExpression="Title">
                            <HeaderStyle />
                            <ItemTemplate>
                                <%# checkNull(Eval("Title"))%>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Created Date" SortExpression="StartDate">
                            <HeaderStyle />
                            <ItemTemplate>
                                <%# checkDateNull(Eval("StartDate"))%>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Due Date" SortExpression="DueDate">
                            <HeaderStyle />
                            <ItemTemplate>
                                <%# checkDateNull(Eval("DueDate"))%>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Assigned To">
                            <HeaderStyle />
                            <ItemTemplate>
                                <%# checkNull(Eval("AssignedTo"))%>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Current Status" SortExpression="Status">
                            <HeaderStyle />
                            <ItemTemplate>
                                <%# checkNull(Eval("Status"))%>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
            <div class="pagination_container">
                <div>
                    <asp:Label ID="lblPageCount" Visible="false" CssClass="result" runat="server"></asp:Label>
                </div>
            </div>
            <div class="clear">
            </div>
        </div>
    </ContentTemplate>
</asp:UpdatePanel>


---------------------------------------------------------------------------
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using Microsoft.SharePoint;

namespace MyWorkFLowTask.MyTaskEvent
{
    public partial class MyTaskEventUserControl : UserControl
    {
        #region Declarations
        DataTable myTasks = new DataTable();


        public class Task
        {

            public string Subject { getset; }
            public string ActionItem { getset; }
            public string AssignedTo { getset; }
            public int MinuteID { getset; }
            public int TaskID { getset; }
            public DateTime DueDate { getset; }
            public int Perc { getset; }
        }

        #endregion

        /// <summary>
        /// Refresh Function
        /// </summary>
        public int Refresh
        {
            get { return 1; }
        }

        /// <summary>
        /// Custom Method to check Date null value
        /// </summary>
        /// <param name="myValue"></param>
        /// <returns></returns>
        public string checkDateNull(object myValue)
        {
            string value = Convert.ToString(myValue);

            if (string.IsNullOrEmpty(value))
            {
                return string.Empty;
            }
            else
            {
                return Convert.ToDateTime(myValue).ToString("MM/dd/yyyy");
            }
        }

        /// <summary>
        /// Custom Method to check object null value
        /// </summary>
        /// <param name="myValue"></param>
        /// <returns></returns>
        public string checkNull(object myValue)
        {
            string value = Convert.ToString(myValue);

            if (string.IsNullOrEmpty(value))
            {
                return string.Empty;
            }
            else
            {
                return myValue.ToString();
            }
        }
        /// <summary>
        /// Page Load Event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            SPWeb web = SPContext.Current.Web;
            try
            {
                if (!Page.IsPostBack)
                {
                    BindMyTasks();
                }
            }
            catch (Exception exp)
            {
                //
            }
            finally
            {
                //
            }
        }

        /// <summary>
        /// Custom Method to get Tasks from SP List
        /// </summary>
        /// <returns></returns>
        DataTable GetMyTasks()
        {
            DataTable itmcoll = new DataTable();
            DataTable dt = new DataTable();

            try
            {
                SPWeb web = SPContext.Current.Web;
                SPUser user = web.CurrentUser;

                SPList task = web.Lists.TryGetList("Workflow Tasks");
                SPListItemCollection items = task.Items;
                itmcoll = items.GetDataTable();
                var query = from c in itmcoll.AsEnumerable()
                            // where Convert.ToString(c["AssignedTo"]) == SPContext.Current.Web.CurrentUser.Name
                            select c;
                dt = query.CopyToDataTable();

                if (dt != null)
                {
                    foreach (DataRow item in dt.Rows)
                    {
                        item["PercentComplete"] = Convert.ToInt32(Convert.ToDecimal(item["PercentComplete"]) * 100);
                        string Title = Convert.ToString(item["Title"]);
                        if (Title.Length > 22)
                        {
                            Title = Title.Substring(0, 22) + "...";
                            item["Title"] = Title;
                        }
                    }
                }
            }
            catch (Exception exp)
            {
                //
            }
            finally
            {
                //
            }

            return dt;
        }

        /// <summary>
        /// Custom Method to bind tasks to the grid
        /// </summary>
        protected void BindMyTasks()
        {
            try
            {
                spgrdMyTasks.PagerTemplate = null;
                myTasks = GetMyTasks();

                if (ViewState["SortExpression"] != null && !string.IsNullOrEmpty(Convert.ToString(ViewState["SortExpression"])))
                {
                    myTasks = SortTable(myTasks);
                }

                spgrdMyTasks.DataSource = myTasks;
                spgrdMyTasks.DataBind();

                if (myTasks != null && myTasks.Rows.Count > 0)
                {
                    lblPageCount.Text = Convert.ToString(myTasks.Rows.Count) + " Records";
                }
                else
                {
                    lblPageCount.Text = "No Records Found";
                }
            }
            catch (Exception exp)
            {
                //
            }
            finally
            {
                //
            }
        }

        /// <summary>
        /// Custom method to sort Grid Data
        /// </summary>
        /// <param name="dtGridData"></param>
        /// <returns></returns>
        DataTable SortTable(DataTable dtGridData)
        {
            try
            {
                DataView dvGridDataView = dtGridData.DefaultView;
                string strSortOrder = string.Empty;

                if (gridViewSortDirection == SortDirection.Ascending)
                {
                    strSortOrder = "asc";
                }
                else
                {
                    gridViewSortDirection = SortDirection.Ascending;
                    strSortOrder = "desc";
                }

                dvGridDataView.Sort = ViewState["SortExpression"] + " " + strSortOrder;
                dtGridData = dvGridDataView.ToTable();

            }
            catch (Exception exp)
            {
                //
            }
            finally
            {
                //
            }

            return dtGridData;
        }

        /// <summary>
        /// Page Index Changing Event for the Grid
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void spgrdMyTasks_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
        {
            try
            {
                spgrdMyTasks.PageIndex = e.NewPageIndex;
                BindMyTasks();
            }
            catch (Exception exp)
            {
                //
            }
            finally
            {
                // 
            }
        }

        /// <summary>
        /// Row Created Event for the Grid
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void spgrdMyTasks_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.Header)
                {
                    int sortColumnIndex = getSortColumnIndex();

                    if (sortColumnIndex != -1)
                    {
                        addSortImage(sortColumnIndex, e.Row);
                    }
                }
            }
            catch (Exception exp)
            {
                //
            }
            finally
            {
                //
            }
        }

        /// <summary>
        /// Grid Data Sorting Event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void spgrdMyTasks_Sorting(object sender, System.Web.UI.WebControls.GridViewSortEventArgs e)
        {
            try
            {
                DataTable dtGridData = GetMyTasks();
                ViewState["SortExpression"] = e.SortExpression;
                DataView dvGridDataView = dtGridData.DefaultView;
                string strSortOrder = "";

                if (gridViewSortDirection == SortDirection.Ascending)
                {
                    gridViewSortDirection = SortDirection.Descending;
                    strSortOrder = "desc";
                }
                else
                {
                    gridViewSortDirection = SortDirection.Ascending;
                    strSortOrder = "asc";
                }

                dvGridDataView.Sort = ViewState["SortExpression"] + " " + strSortOrder;
                dtGridData = dvGridDataView.ToTable();
                spgrdMyTasks.DataSource = dtGridData;
                spgrdMyTasks.DataBind();
            }
            catch (Exception exp)
            {
                //
            }
            finally
            {
                //
            }
        }

        /// <summary>
        /// Gets the Column Index of the Columns based on which the Sorting should be done.
        /// </summary>
        /// <returns></returns>
        private int getSortColumnIndex()
        {
            try
            {
                foreach (DataControlField field in spgrdMyTasks.Columns)
                {
                    if (field.SortExpression == (string)ViewState["SortExpression"])
                    {
                        return spgrdMyTasks.Columns.IndexOf(field);
                    }
                }
            }
            catch (Exception exp)
            {
                //
            }
            finally
            {
                //
            }

            return -1;
        }

        /// <summary>
        /// This method swaps the sort direction of the Grid based upon the current sorted direction.
        /// If the current sorted order is Ascending then the new sort direction will be Descending
        /// or Vice versa.
        /// </summary>
        private SortDirection gridViewSortDirection
        {
            get
            {
                if (ViewState["SortOrder"] == null)
                {
                    ViewState["SortOrder"] = SortDirection.Ascending;
                }
                return (SortDirection)ViewState["SortOrder"];
            }
            set
            {
                ViewState["SortOrder"] = value;
            }
        }

        /// <summary>
        /// Adds Sort Image to Sorted Column.
        /// </summary>
        /// <param name="columnIndex"></param>
        /// <param name="headerRow"></param>
        private void addSortImage(int columnIndex, GridViewRow headerRow)
        {
            try
            {
                // Create the sorting image based on the sort direction.
                Image sortImage = new Image();
                if (gridViewSortDirection == SortDirection.Ascending)
                {
                    sortImage.ImageUrl = "~/_layouts/15/Branding/images/up_arrow.png";
                    sortImage.AlternateText = "Ascending Order";
                }
                else
                {
                    sortImage.ImageUrl = "~/_layouts/15/Branding/images/Down_arrow.png";
                    sortImage.AlternateText = "Descending Order";
                }
                // Add the image to the appropriate header cell.
                headerRow.Cells[columnIndex].Controls.Add(sortImage);
            }
            catch (Exception exp)
            {
                //
            }
            finally
            {
                // 
            }
        }

        protected void spgrdMyTasks_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Pager)
            {
                int count = e.Row.Cells[0].Controls[0].Controls[0].Controls.Count;

                if (count == 1)
                {
                    if (spgrdMyTasks.PageIndex == 0)
                    {
                        TableCell next = (TableCell)e.Row.Cells[0].Controls[0].Controls[0].Controls[0];
                        next.ToolTip = "Next";
                    }
                    else
                    {
                        TableCell prev = (TableCell)e.Row.Cells[0].Controls[0].Controls[0].Controls[0];
                        prev.ToolTip = "Previous";
                    }
                }
                else if (count == 2)
                {
                    TableCell prev = (TableCell)e.Row.Cells[0].Controls[0].Controls[0].Controls[0];
                    prev.ToolTip = "Previous";
                    TableCell next = (TableCell)e.Row.Cells[0].Controls[0].Controls[0].Controls[1];
                    next.ToolTip = "Next";
                }
            }
        }
    }
}

No comments:

Post a Comment