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, October 30, 2012

Real Time Task Binding Count Based on Colors in SharePoint 2007 and SharePoint 2010 Dynamically.

Back End Database


using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Security;

namespace CHSample1.CHSample1
{
    [ToolboxItemAttribute(false)]
    public class CHSample1 : WebPart
    {
        string strError = string.Empty;
        string strHTML = string.Empty;
        protected override void Render(HtmlTextWriter writer)
        {
            try
            {
                SPWeb currentWeb = SPContext.Current.Web;
                SPList lst = currentWeb.Lists["Color"];
                SPQuery sQuery = new SPQuery();
                sQuery.Query = "<OrderBy><FieldRef Name='ID' /></OrderBy>";
                SPListItemCollection myColl = lst.GetItems(sQuery);
                if (myColl.Count > 0)
                {
                    
                    string strTable = string.Empty;
                    strTable += "<Table>";
                    foreach (SPListItem item in myColl)
                    {
                        string strColor = string.Empty;
                        strColor = item.Title.ToString();
                        int i = BindTaskCount(strColor);
                        strTable += "<Tr>";
                        strTable += "<Td style='background-color:"+strColor+"' Title='"+strColor+"' width='10'>";
                        strTable += "&nbsp;";
                        strTable += "</Td>";
                        strTable += "<Td>";
                        strTable += "&nbsp;";
                        strTable += "</Td>";
                        strTable += "<Td>";
                        strTable += i;
                        strTable += "</Td>";
                        strTable += "</Tr>";
                    }
                    strTable += "</Table>";
                    writer.Write(strTable);
                }
            }
            catch (Exception ex)
            {
               
                writer.Write(ex.ToString());
            }
        }
     
        public int BindTaskCount(string strColor)
        {
                SPWeb currentWeb = SPContext.Current.Web;
                SPList lst = currentWeb.Lists["New Tasks"];
                SPQuery sQuery = new SPQuery();
                sQuery.Query = "<Where><Eq><FieldRef Name='Color' /><Value Type='Lookup'>" + strColor + "</Value></Eq></Where><OrderBy><FieldRef Name='Color' /></OrderBy>";
                int count = 0;
                SPListItemCollection myColl = lst.GetItems(sQuery);
                if (myColl.Count > 0)
                {
                    count = myColl.Count;
                }
                return count;
        }
    }
}

No comments:

Post a Comment