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, December 27, 2012

Horizontal Bar Graph Based on Task List Status Column

Ascx
<%@ 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="TaskStatusBGHUserControl.ascx.cs" Inherits="TaskStatusBGH.TaskStatusBGH.TaskStatusBGHUserControl" %>
<asp:Panel ID="pnlBar" runat="server">

</asp:Panel>
<asp:Label ID="lblMessage" runat="server"></asp:Label>

Ascx.cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Security;

namespace TaskStatusBGH.TaskStatusBGH
{
    public partial class TaskStatusBGHUserControl : UserControl
    {

        int AllItemsCount;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                AllItemsCount = getAllItemsCount();
                double InProgressCount = getStatusCount("In Progress");
                double CompletedCount = getStatusCount("Completed");
                double DeferredCount = getStatusCount("Deferred");
                double waitingonSomeOneElseCount = getStatusCount("Waiting on someone else");
                string strBarGraph = "";
                strBarGraph += "<Table width='100%' cellpading='0' cellspacing='0'>";
                strBarGraph += "<Tr>";
                strBarGraph += "<Td width='20%'>";
                strBarGraph += "<Strong>";
                strBarGraph += "In Progress";
                strBarGraph += "</Strong>";
                strBarGraph += "</Td>";
                strBarGraph += "<Td>";
                strBarGraph += "<Div style='background-color:Yellow;height:10px;width:" + InProgressCount + "%'></Div>";
                strBarGraph += "</Td>";
                strBarGraph += "</Tr>";

                strBarGraph += "<Tr>";
                strBarGraph += "<Td width='20%'>";
                strBarGraph += "<Strong>";
                strBarGraph += "Completed";
                strBarGraph += "</Strong>";
                strBarGraph += "</Td>";
                strBarGraph += "<Td>";
                strBarGraph += "<Div style='background-color:Yellow;height:10px;width:" + CompletedCount + "%'></Div>";
                strBarGraph += "</Td>";
                strBarGraph += "</Tr>";

                strBarGraph += "<Tr>";
                strBarGraph += "<Td width='20%'>";
                strBarGraph += "<Strong>";
                strBarGraph += "Deferred";
                strBarGraph += "</Strong>";
                strBarGraph += "</Td>";
                strBarGraph += "<Td>";
                strBarGraph += "<Div style='background-color:Yellow;height:10px;width:" + DeferredCount + "%'></Div>";
                strBarGraph += "</Td>";
                strBarGraph += "</Tr>";

                strBarGraph += "<Tr>";
                strBarGraph += "<Td width='20%'>";
                strBarGraph += "<Strong>";
                strBarGraph += "Waiting on someone else";
                strBarGraph += "</Strong>";
                strBarGraph += "</Td>";
                strBarGraph += "<Td>";
                strBarGraph += "<Div style='background-color:Yellow;height:10px;width:" + waitingonSomeOneElseCount + "%'></Div>";
                strBarGraph += "</Td>";
                strBarGraph += "</Tr>";

                strBarGraph += "</Table>";
                pnlBar.Controls.Add(new LiteralControl(strBarGraph));
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.ToString();
            }
        }

        public double getStatusCount(string strStatus)
        {
            double TotalCount = 0;
            try
            {
                double StatusItemCount = 0;
                SPWeb currentWeb = SPContext.Current.Web;
                SPList lst = currentWeb.Lists["Tasks"];
                SPQuery sQuery = new SPQuery();
                sQuery.Query = "<Where><Eq><FieldRef Name='Status' /><Value Type='Choice'>"+strStatus.ToString().ToLower()+"</Value></Eq></Where>";
                SPListItemCollection myColl = lst.GetItems(sQuery);
                if (myColl.Count > 0)
                {
                    StatusItemCount = myColl.Count;
                    TotalCount = Math.Round((double)((StatusItemCount / AllItemsCount) * 100),2);
                }
               
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.ToString();
               
            }
            return TotalCount;
        }

        public int getAllItemsCount()
        {
            int count = 0;
            try
            {
                SPWeb currentWeb = SPContext.Current.Web;
                SPList lst = currentWeb.Lists["Tasks"];
                SPListItemCollection myColl = lst.Items;
                if (myColl.Count > 0)
                {
                    count = myColl.Count;
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.ToString();
            }
            return count;
        }
    }
}

Tuesday, December 25, 2012

How to Build a Simple Progress Bar in Ap.Net GridView or SharePoint GridView using SharePoint Tasks List Data

Note:-
Do not worry about Input String was not in a correct format. Use Try Catch to avoid these error. This error will occur because of PercentComplete is the Number Type Column in the SharePoint Tasks List.


Aspx
<%@ 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="TestPBarUserControl.ascx.cs" Inherits="TestPBar.TestPBar.TestPBarUserControl" %>
<asp:GridView ID="dgvProgressBarSample" runat="server" CellPadding="4"
    EnableModelValidation="True" ForeColor="#333333" GridLines="Both" AutoGenerateColumns="false">
    <AlternatingRowStyle BackColor="White" />
    <EditRowStyle BackColor="#7C6F57" />
    <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#E3EAEB" />
    <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
    <Columns>
    <asp:BoundField HeaderText="Title" DataField="Title" />
    <asp:BoundField HeaderText="% Complete" DataField="PercentComplete" />  
    </Columns>
</asp:GridView>



Aspx.Cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Security;

namespace TestPBar.TestPBar
{
    public partial class TestPBarUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            dgvProgressBarSample.RowDataBound += new GridViewRowEventHandler(dgvProgressBarSample_RowDataBound);
            SPWeb currentMeb = SPContext.Current.Web;
            SPList lst = currentMeb.Lists["Tasks"];
            SPQuery sQuery = new SPQuery();
            sQuery.Query = "";
            SPListItemCollection myColl = lst.GetItems(sQuery);
            if (myColl.Count > 0)
            {
                dgvProgressBarSample.DataSource = myColl.GetDataTable();
                dgvProgressBarSample.DataBind();
            }

        }

        void dgvProgressBarSample_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                double  i = double.Parse(e.Row.Cells[1].Text) * 100;
                string strGenrateDynamicDiv = string.Empty;
                strGenrateDynamicDiv = "<div><table width='" + i + "%' style=\"Background-Color:Yellow;height:10px;\"><tr><td title='Task completed up to " + i + "%'>"+i+"%</td></tr></table></div>";
                e.Row.Cells[1].Text = strGenrateDynamicDiv;
            }
            catch (Exception ex)
            {
                //Response.Write(ex.ToString());
            }

        }
    }
}