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

Retrieve SharePoint List Attachments Dynamically to a GridView or SharePoint 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="AttachmentsPrgUserControl.ascx.cs" Inherits="AttachmentsPrg.AttachmentsPrg.AttachmentsPrgUserControl" %>
<asp:GridView ID="dgvAttachments" runat="server" AutoGenerateColumns="False"
    BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px"
    CellPadding="4" EnableModelValidation="True" GridLines="Both" Width="100%">
    <FooterStyle BackColor="White" ForeColor="#333333" />
    <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="White" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
    <Columns>
    <asp:BoundField HeaderText="Title" DataField="Title" />
    <asp:HyperLinkField HeaderText="Attachments" DataTextField="AttachmentTitle" DataNavigateUrlFields="AttachmentURL" DataNavigateUrlFormatString="{0}" />
    </Columns>
</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 System.Text;
using System.Collections.Generic;

namespace AttachmentsPrg.AttachmentsPrg
{
    public partial class AttachmentsPrgUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dgvAttachments.DataSource = getAttachmentsData();
                dgvAttachments.DataBind();
            }
        }

        public class AttachmentsData
        {

            public string Title { get; set; }
            public string AttachmentTitle { get; set; }
            public string AttachmentURL { get; set; }
        }

        public List<AttachmentsData> getAttachmentsData()
        {
            List<AttachmentsData> AttachmentsData = new List<AttachmentsData>();
            using (SPSite site = new SPSite(SPContext.Current.Web.Url))
            {
                using (SPWeb currentWeb = site.OpenWeb())
                {
                    SPList lst = currentWeb.Lists["List Attachments Test"];
                    SPQuery sQuery = new SPQuery();
                    sQuery.Query = "<OrderBy><FieldRef Name=\"ID\" Ascending=\"False\" /></OrderBy>";
                    sQuery.ViewFields = "<FieldRef Name=\"ID\" /><FieldRef Name=\"Title\" /><FieldRef Name=\"Attachments\" />";
                    sQuery.IncludeAttachmentUrls = true;
                    SPListItemCollection myColl = lst.GetItems(sQuery);
                    if (myColl.Count > 0)
                    {
                        foreach (SPListItem item in myColl)
                        {
                            if (item["Attachments"].ToString() == "True")
                            {
                                SPAttachmentCollection attachments = item.Attachments;
                                SPFolder folder = currentWeb.Folders["Lists"].SubFolders[lst.Title].SubFolders["Attachments"].SubFolders[item.ID.ToString()];
                                foreach (SPFile file in folder.Files)
                                {
                                    AttachmentsData.Add(new AttachmentsData()
                                    {
                                        Title = item["Title"].ToString(),
                                        AttachmentTitle = file.Name.ToString(),
                                        AttachmentURL = currentWeb.Url + "/" + file.Url.ToString()
                                    });
                                }
                            }
                            else if (item["Attachments"].ToString() == "False")
                            {
                                AttachmentsData.Add(new AttachmentsData()
                                {
                                    Title = item["Title"].ToString(),
                                    AttachmentTitle = "--",
                                    AttachmentURL = Page.Request.Url.ToString() + "#"
                                });
                            }

                        }

                    }
                }
            }
            return AttachmentsData;
        }
    }
}

No comments:

Post a Comment