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, March 20, 2013

How to Retrieve SharePoint Quick Launch Links to GridView or SpGridView using Server Side Linq




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="QuickLaunchProgramUserControl.ascx.cs" Inherits="QuickLaunchProgram.QuickLaunchProgram.QuickLaunchProgramUserControl" %>
<asp:GridView ID="dgvQuickLaunch" runat="server" AutoGenerateColumns="False"
    Width="100%" CellPadding="4" EnableModelValidation="True" ForeColor="#333333"
    GridLines="None" onrowdatabound="dgvQuickLaunch_RowDataBound">
    <AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="QuickLaunch Links">
<ItemTemplate>
<div>
<a href=<%# Eval("QuickLaunchMainHeadingURL") %>><%# Eval("QuickLaunchMainHeadingTitle")%></a>
<asp:Label ID="lblMainHeading" runat="server" Visible="false" Text=<%# Eval("QuickLaunchMainHeadingTitle")%>></asp:Label>
</div>
<div style="padding-left:20px;">
<asp:GridView ID="dgvQuickLaunchChildren" runat="server"
    AutoGenerateColumns="False" CellPadding="4" EnableModelValidation="True"
    ForeColor="#333333" GridLines="Both" 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" />
</asp:GridView>
</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>
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;
using Microsoft.SharePoint.Navigation;

namespace QuickLaunchProgram.QuickLaunchProgram
{
    public partial class QuickLaunchProgramUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                getQuickLaunchLinks();
            }
        }

        public void getQuickLaunchLinks()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                string QuickLaunchMainHeadingTitle = string.Empty;
                string QuickLaunchMainHeadingURL = string.Empty;
                var strQuery = from SPNavigationNode node in SPContext.Current.Web.Navigation.QuickLaunch
                               select new { QuickLaunchMainHeadingTitle = node.Title, QuickLaunchMainHeadingURL = node.Url };
                dgvQuickLaunch.DataSource = strQuery;
                dgvQuickLaunch.DataBind();
            });
        }

        protected void dgvQuickLaunch_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lbl = (Label)e.Row.Cells[0].FindControl("lblMainHeading");
                GridView dgvQuickLaunchChildren1 = (GridView)e.Row.Cells[0].FindControl("dgvQuickLaunchChildren");
                HyperLinkField bTitle = new HyperLinkField();
                //bTitle.HeaderText = "";
                bTitle.DataTextField = "QuickLaunchChildHeadingTitle";
                bTitle.DataNavigateUrlFields = new String[] { "QuickLaunchChildHeadingURL" };
                bTitle.DataNavigateUrlFormatString = "{0}";
                dgvQuickLaunchChildren1.Columns.Add(bTitle);
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    string QuickLaunchChildHeadingTitle = string.Empty;
                    string QuickLaunchMainHeadingURL = string.Empty;
                    var strChildItemsQuery = from SPNavigationNode heading in SPContext.Current.Web.Navigation.QuickLaunch
                                        from SPNavigationNode child in heading.Children
                                        where heading.Title == lbl.Text
                                        select new { QuickLaunchChildHeadingTitle = child.Title, QuickLaunchChildHeadingURL = child.Url };
                    dgvQuickLaunchChildren1.DataSource = strChildItemsQuery;
                    dgvQuickLaunchChildren1.DataBind();
                });
                            
            }
        }
    }
}


No comments:

Post a Comment