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.

Monday, March 25, 2013

How to retrieve all SharePoint Lists and Libraries using Server Side Linq to 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="SiteListsGridViewLinqUserControl.ascx.cs" Inherits="SiteListsGridViewLinq.SiteListsGridViewLinq.SiteListsGridViewLinqUserControl" %>
<asp:Label ID="lblMessage" runat="server"></asp:Label>
<asp:GridView ID="dgvLists" runat="server" AutoGenerateColumns="False"
    Width="100%" CellPadding="4" EnableModelValidation="True" ForeColor="#333333"
    GridLines="Both">
    <AlternatingRowStyle BackColor="White" />
<Columns>
<asp:HyperLinkField HeaderText="List Name" DataTextField="ListTitle" DataNavigateUrlFields="ListURL" DataTextFormatString="{0}" />
</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;

namespace SiteListsGridViewLinq.SiteListsGridViewLinq
{
    public partial class SiteListsGridViewLinqUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                getLists();
            }
        }

        public void getLists()
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    string ListTitle, ListURL = string.Empty;
                    var strQuery = from SPList lst in SPContext.Current.Web.Lists
                                   select new
                                   {
                                       ListTitle = lst.Title,
                                       ListURL = lst.DefaultViewUrl
                                   };
                    dgvLists.DataSource = strQuery;
                    dgvLists.DataBind();
                });
               
            }
            catch (Exception Ex)
            {
                lblMessage.Text = Ex.ToString();
            }
        }
    }
}

No comments:

Post a Comment