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.

Wednesday, February 06, 2013

Building a Group By with Simple HTML Table using SharePoint Object Model



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="GroupBYLogicUserControl.ascx.cs" Inherits="GroupBYLogic.GroupBYLogic.GroupBYLogicUserControl" %>
<style type="text/css">
.HeadingClass
{
       font-size:13px;
       background-color:blue;
       color:white;
       font-weight:bold;
}
.LoopRecordsClass
{
       font-size:13px;
       background-color:Green;
       color:white;
}

.BlankTr
{
    background-color:Red;
    height:2px;
}

.GroupByClass
{
    background-color:pink;
    color:Green;
    font-weight:bold;
    font-size:13px;
}

.LoopRecordsClass a
                     {
                           text-decoration:none;
                           font-size:13px;
                           color:white;
                     }
                    
                     .LoopRecordsClass a:hover
                      {
                           text-decoration:underline;
                           font-size:13px;
                           color:white;
                      }
                      
                      .LoopRecordsClass a:after
                      {
                           text-deocration:none;
                           font-size:13px;
                           color:white;
                      }
</style>
<script language="javascript" type="text/javascript" src="/UserPages/JS/jquery.min.1.8.3.js"></script>
<script language="javascript" type="text/javascript">
    function OpenDiv(strRegionID)
    {
        $("#divOpenDiv" + strRegionID).hide();
        $("#divCloseDiv" + strRegionID).show();
        $("#divCountryData" + strRegionID).show();
    }
    function CloseDiv(strRegionID)
    {
        $("#divOpenDiv" + strRegionID).show();
        $("#divCloseDiv" + strRegionID).hide();
        $("#divCountryData" + strRegionID).hide();
    }
</script>
<asp:Panel ID="pnlGroupByHTML" runat="server"></asp:Panel>
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 GroupBYLogic.GroupBYLogic
{
    public partial class GroupBYLogicUserControl : UserControl
    {
        string strGroupBy = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
          
            SPWeb currentWeb = SPContext.Current.Web;
            SPList lst = currentWeb.Lists["Region"];
            SPQuery sQuery = new SPQuery();
            sQuery.Query = "<OrderBy><FieldRef Name='Title' /></OrderBy>";
            SPListItemCollection myColl = lst.GetItems(sQuery);
            if (myColl.Count > 0)
            {
                strGroupBy += "<Div class='HeadingClass'>";
                strGroupBy += "<Table width='100%' cellpadding='0' celspacing='0'>";
                strGroupBy += "<Tr>";
                strGroupBy += "<Td Class='BlankTr'>";
                strGroupBy += "</Td>";
                strGroupBy += "</Tr>";
                strGroupBy += "<Tr>";
                strGroupBy += "<Td Class='HeadingClass' Align='Center'>";
                strGroupBy += "<Strong>";
                strGroupBy += "Building a simple Group By Table using SharePoint Object Model";
                strGroupBy += "</Strong>";
                strGroupBy += "</Td>";
                strGroupBy += "</Tr>";
                strGroupBy += "<Tr>";
                strGroupBy += "<Td Class='BlankTr'>";
                strGroupBy += "</Td>";
                strGroupBy += "</Tr>";
                strGroupBy += "<Tr>";
                strGroupBy += "<Td class='HeadingClass'>";
                strGroupBy += "Country";
                strGroupBy += "</Td>";
                strGroupBy += "</Tr>";
                strGroupBy += "<Tr>";
                strGroupBy += "<Td Class='BlankTr'>";
                strGroupBy += "</Td>";
                strGroupBy += "</Tr>";
                strGroupBy += "</Table>";
                strGroupBy += "</Div>";
                foreach (SPListItem item in myColl)
                {
                    string strRegionTitle = item.Title.ToString();
                    string strRegionID = item.ID.ToString();
                    string strGroupByOpenDivImage = currentWeb.Url + "/_layouts/images/plus.gif";
                    string strGroupByCloseDivImage = currentWeb.Url + "/_layouts/images/minus.gif";
                    strGroupBy += "<Div class='GroupByClass'>";
                    strGroupBy += "<Table width='100%' cellpadding='0' celspacing='0'>";
                    strGroupBy += "<Tr>";
                    strGroupBy += "<Td width='10px'>";
                    strGroupBy+="<Div id='divOpenDiv"+strRegionID+"'>";
                    strGroupBy += "<A href=\"Javascript:OpenDiv("+item.ID.ToString()+")\"><img src='" + strGroupByOpenDivImage + "' border='0'></a>";
                    strGroupBy += "</Div>";
                    strGroupBy += "<Div id='divCloseDiv" + strRegionID + "' style='display:none;'>";
                    strGroupBy += "<A href=\"Javascript:CloseDiv(" + item.ID.ToString() + ")\"><img src='" + strGroupByCloseDivImage + "' border='0'></a>";
                    strGroupBy += "</Div>";
                    strGroupBy += "</Td>";
                    strGroupBy += "<Td>";
                    strGroupBy += "Region :"+strRegionTitle;
                    strGroupBy += "</Td>";
                    strGroupBy += "</Tr>";
                    strGroupBy += "</Table>";
                    strGroupBy += "</Div>";
                    strGroupBy += getCountryData(strRegionID,strRegionTitle);
                }
            }
            pnlGroupByHTML.Controls.Add(new LiteralControl(strGroupBy));
           
        }

        public string getCountryData(string strRegionID,string strRegionTitle)
        {
            string strCountry = "";
            strCountry += "<Div id='divCountryData" + strRegionID + "' style='display:none;padding-left:60px;' class='LoopRecordsClass'>";
            strCountry += "<Table width='100%' cellpadding='0' celspacing='0'>";
            strCountry += "<Tr>";
            strCountry += "<Tr>";
            strCountry += "<Tr>";
            strCountry += "<Td Class='BlankTr'>";
            strCountry += "</Td>";
            strCountry += "</Tr>";
            strCountry += "<Tr>";
            strCountry += "<Td class='HeadingClass' Align='Center'>";
            strCountry += "The Countries of Region "+strRegionTitle+" is given below.";
            strCountry += "</Td>";
            strCountry += "</Tr>";
            SPWeb currentWeb = SPContext.Current.Web;
            SPList lst = currentWeb.Lists["lst_Country"];
            SPQuery sQuery = new SPQuery();
            sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy><Where><Eq><FieldRef Name='Region' LookupId='TRUE' /><Value Type='Lookup'>" + strRegionID + "</Value></Eq></Where>";
            SPListItemCollection myColl = lst.GetItems(sQuery);
            if (myColl.Count > 0)
            {
                foreach (SPListItem item in myColl)
                {
                    string strItemURL=currentWeb.Url+"/Lists/lst_Country/DispForm.aspx?ID="+item.ID.ToString()+"&Source="+Page.Request.Url;
                    strCountry += "<Tr>";
                    strCountry += "<Td Class='BlankTr'>";
                    strCountry += "</Td>";
                    strCountry += "</Tr>";
                    strCountry += "<Tr>";
                    strCountry += "<Td class='LoopRecordsClass'>";
                    strCountry += "<A href='" + strItemURL + "'><Span class='LoopRecordsClass'>" + item.Title.ToString() + "</span></a>";
                    strCountry += "</Td>";
                    strCountry += "</Tr>";
                }
            }
            strCountry += "<Tr>";
            strCountry += "<Td Class='BlankTr'>";
            strCountry += "</Td>";
            strCountry += "</Tr>";
            strCountry += "</Table>";
            strCountry += "</Div>";
            return strCountry;
        }
    }
}

No comments:

Post a Comment