Tuesday, January 3, 2012

Modified Code ReadDocLibraryData Using SpQuery inside the folders as well as subfolders.

using System;
using System.Runtime.InteropServices;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Security;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.Collections.Specialized;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Configuration;
[assembly: AllowPartiallyTrustedCallers]
namespace ReadDocLibraryData
{
    [Guid("5c9f3c7a-40ef-4255-8409-4b4d83c13301")]
    public class ReadDocLibraryData : System.Web.UI.WebControls.WebParts.WebPart
    {
        public ReadDocLibraryData()
        {
        }
        protected override void Render(HtmlTextWriter writer)
        {
            try
            {
                SPWeb currentWeb = SPControl.GetContextWeb(Context);
                SPList lstDoc = currentWeb.Lists["Shared Documents"];
                SPQuery sQuery = new SPQuery();
                sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
                sQuery.ViewAttributes = "Scope='RecursiveAll'";
                SPListItemCollection myColl = lstDoc.GetItems(sQuery);
                writer.Write("<Table>");
                writer.Write("<Tr>");
                writer.Write("<Td>");
                writer.Write("<Strong>");
                writer.Write("Document Name");
                writer.Write("</Strong>");
                writer.Write("</Td>");
                writer.Write("</Tr>");
                /*foreach (SPListItem item in myColl)
                {
                    string strDocUrl = item["FileRef"].ToString();
                    strDocUrl = strDocUrl.Substring(strDocUrl.IndexOf('#') + 1);
                    string strOriginalUrl = currentWeb.Url + "/" + strDocUrl;
                    string strFileName = item["LinkFilename"].ToString();
                    writer.Write("<Tr>");
                    writer.Write("<Td>");
                    writer.Write("<A href='"+strOriginalUrl+"'>"+strFileName+"</A>");
                    writer.Write("</Td>");
                    writer.Write("</Tr>");
                }*/
                string strDocUrl, strOriginalUrl, strFileName = string.Empty;
                if (myColl.Count > 0)
                {
                    for (int i = 0; i < myColl.Count; i++)
                    {
                        SPListItem item = myColl[i];
                        strDocUrl = item["FileRef"].ToString();
                        strDocUrl = strDocUrl.Substring(strDocUrl.IndexOf('#') + 1);
                        strOriginalUrl = currentWeb.Url + "/" + strDocUrl;
                        strFileName = item["LinkFilename"].ToString();
                        writer.Write("<Tr>");
                        writer.Write("<Td>");
                        writer.Write("<A href='" + strOriginalUrl + "'>" + strFileName + "</A>");
                        writer.Write("</Td>");
                        writer.Write("</Tr>");
                    }
                }
                writer.Write("</Table>");
            }
            catch (Exception ex)
            {
                writer.Write(ex.ToString());
            }
        }
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            // TODO: add custom rendering code here.
            // Label label = new Label();
            // label.Text = "Hello World";
            // this.Controls.Add(label);
        }
    }
}

No comments:

Post a Comment