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, February 07, 2013

Retrieving Data from Subsite List and Hyperlink for Group by without a Single Line of Loop with Asp.Net GridView



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="SubSiteDataUserControl.ascx.cs" Inherits="SubSiteData.SubSiteData.SubSiteDataUserControl" %>
 <script language="javascript" type="text/javascript" src="/UserPages/JS/jquery.min.1.8.3.js"></script>
 <script language="javascript" type="text/javascript">
     function OpenDiv(strSubWebID) {
         $("#divOpenDiv" + strSubWebID).hide();
         $("#divCloseDiv" + strSubWebID).show();
         $("#divSubWebData" + strSubWebID).show();
     }
     function CloseDiv(strSubWebID) {
         $("#divOpenDiv" + strSubWebID).show();
         $("#divCloseDiv" + strSubWebID).hide();
         $("#divSubWebData" + strSubWebID).hide();
     }
</script>
<table width="100%" cellpadding="0" cellspacing="0">
<tr><td align="center"><strong>Subsite Data</strong></td></tr>
<tr>
<td>
 <asp:GridView ID="dgvSubWebData" runat="server" AutoGenerateColumns="False"
    Width="100%" DataKeyNames="ID" CellPadding="4" EnableModelValidation="True"
    ForeColor="#333333" GridLines="None"
        onrowdatabound="dgvSubWebData_RowDataBound">
    <AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<div>
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td align="center">
Retrieving Tasks List Data from SubSite Lists
</td>
</tr>
</table>
</div>
</HeaderTemplate>
<ItemTemplate>
<div>
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td width="10px">
<div id="divOpenDiv<%# Eval("ID") %>"><a href="javascript:OpenDiv('<%# Eval("ID") %>')"><img src="/_layouts/images/plus.gif" border="0" /></a></div>
<div id="divCloseDiv<%# Eval("ID") %>" style="display:none;"><a href="javascript:CloseDiv('<%# Eval("ID") %>')"><img src="/_layouts/images/minus.gif" border="0" /></a></div>
</td>
<td>
<asp:HyperLink ID="hlnkSubWebUrlTitle" runat="server" Text=<%# Eval("SubWebTitle") %> NavigateUrl=<%# Eval("SubWebUrl") %>></asp:HyperLink>
    <asp:Label ID="lblSubWebUrl" runat="server" Text=<%# Eval("SubWebUrl") %> Visible="false"></asp:Label>
</td>
</tr>
</table>
</div>
<div id="divSubWebData<%# Eval("ID") %>" style="display:none;">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td style='padding-left:60px;'>
<div  style="height: 200px; overflow: auto;">
<asp:GridView ID="dgvSubWebTasks" runat="server" CellPadding="4"
    EnableModelValidation="True" ForeColor="#333333" GridLines="Both" AutoGenerateColumns="false" Width="100%">
    <AlternatingRowStyle BackColor="White" />
    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
    <Columns></Columns>
</asp:GridView>
</div>
</td>
</tr>
</table>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
    <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" />
</asp:GridView>
</td>
</tr>
</table>
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;
using System.Linq;

namespace SubSiteData.SubSiteData
{
    public partial class SubSiteDataUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetSubWebs();
            }
        }

        public void GetSubWebs()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                string SubWebtitle = string.Empty;
                string subwebUrl = string.Empty;
                string ID = string.Empty;
                var sQuery = from SPWeb objSite in SPContext.Current.Web.GetSubwebsForCurrentUser()
                             select new
                             {
                                 SubWebtitle = objSite.Title,
                                 subwebUrl = objSite.Url,
                                 ID = objSite.ID
                             };
                dgvSubWebData.DataSource = sQuery;
                dgvSubWebData.DataBind();
            });
        }

        protected void dgvSubWebData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Label lblSubWebUrl = (Label)e.Row.Cells[0].FindControl("lblSubWebUrl");
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using(SPSite objSite=new SPSite(lblSubWebUrl.Text))
                        {
                            using (SPWeb SubWeb = objSite.OpenWeb())
                            {
                                SubWeb.AllowUnsafeUpdates = true;
                                SPList lst = SubWeb.Lists["Tasks"];
                                SPQuery sQuery = new SPQuery();
                                sQuery.Query = "";
                                SPListItemCollection subWebColl = lst.GetItems(sQuery);
                                if (subWebColl.Count > 0)
                                {
                                    GridView dgvSubWebTasks1 = (GridView)e.Row.Cells[0].FindControl("dgvSubWebTasks");
                                    HyperLinkField hTitle = new HyperLinkField();
                                    hTitle.HeaderText = "Task Name";
                                    hTitle.DataNavigateUrlFields = new string[] { "ID" };
                                    hTitle.DataNavigateUrlFormatString = SubWeb.Url + "/Lists/Tasks/DispForm.aspx?ID={0}&Source=" + Page.Request.Url;
                                    hTitle.DataTextField = "Title";
                                    dgvSubWebTasks1.Columns.Add(hTitle);

                                    BoundField bStatus = new BoundField();
                                    bStatus.HeaderText = "Status";
                                    bStatus.DataField = "Status";
                                    dgvSubWebTasks1.Columns.Add(bStatus);

                                    BoundField bPriority = new BoundField();
                                    bPriority.HeaderText = "Priority";
                                    bPriority.DataField = "Priority";
                                    dgvSubWebTasks1.Columns.Add(bPriority);

                                    dgvSubWebTasks1.DataSource = subWebColl.GetDataTable();
                                    dgvSubWebTasks1.DataBind();
                                }
                            }
                        }
                    });
                }
            }
            catch (Exception Ex)
            {
                Response.Write(Ex.ToString());
            }
        }
    }
}

No comments:

Post a Comment