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 14, 2012

Retrieving Documents from Folders and SubFolders within a SharePoint Document Library and Binding to SpGridView without a Single line of Loop.


Ascx Code


<%@ 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="VisualWebPart1UserControl.ascx.cs" Inherits="RetrieveDocuments.VisualWebPart1.VisualWebPart1UserControl" %>
<SharePoint:SPGridView ID="sgvMyDocuments" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:HyperLinkField HeaderText="Document Name" DataTextField="LinkFilename" DataNavigateUrlFields="EncodedAbsUrl" DataNavigateUrlFormatString="{0}" Target="_blank" />
</Columns>
</SharePoint:SPGridView>

CS Code
try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPWeb currentWeb = SPContext.Current.Web;
                    SPDocumentLibrary lst = (SPDocumentLibrary)currentWeb.Lists["My Documents"];
                    SPQuery sQuery = new SPQuery();
                    sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
                    sQuery.ViewFields = "<FieldRef Name='EncodedAbsUrl' /><FieldRef Name='LinkFilename' />";
                    sQuery.ViewAttributes = "Scope='Recursive'";
                    sQuery.ViewFieldsOnly = true;
                    SPListItemCollection myColl = lst.GetItems(sQuery);
                    if (myColl.Count > 0)
                    {
                        sgvMyDocuments.DataSource = myColl.GetDataTable();
                        sgvMyDocuments.DataBind();
                    }
                });
            }
            catch (Exception ex)
            {

                Response.Write(ex.ToString());
            }
        }

Note:-
     In SharePoint 2007 You will not have a ViewFieldsOnly Attribute for SpQuery.

Here in this code EncodedAbsUrl is a default SharePoint Column which is carrying the File URL in SharePoint Document Library.

No comments:

Post a Comment