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

Retrieving SharePoint List Attachments Programmatically


Ascx
<asp:Panel ID="pnlAttachments" runat="server"></asp:Panel>

Ascx.Cs
public void getAttachments()
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPWeb currentWeb = SPContext.Current.Web;
                    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);
                    string strAttachments = string.Empty;
                    strAttachments += "<Table width='100%' cellpadding='0' cellspacing='0' bgcolor='LightGreen'>";
                    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)
                                {
                                    strAttachments += "<Tr>";
                                    strAttachments += "<Td width='10px'>";
                                    strAttachments += "<Img src='/_layouts/Images/" + file.IconUrl + "' Title='" + file.Name + "' />";
                                    strAttachments += "</Td>";
                                    strAttachments += "<Td width='10px'>";
                                    strAttachments += "&nbsp;";
                                    strAttachments += "</Td>";
                                    strAttachments += "<Td width='25px'>";
                                    strAttachments += item.Title.ToString();
                                    strAttachments += "</Td>";
                                    strAttachments += "<Td width='25px'>";
                                    strAttachments += "<A href='/" + file.Url + "'><Strong>" + file.Name + "</Strong></a>";
                                    strAttachments += "</Td>";
                                    strAttachments += "</Tr>";
                                }
                            }

                            if (item["Attachments"].ToString() == "False")
                            {
                                strAttachments += "<Tr>";
                                strAttachments += "<Td width='10px'>";
                                strAttachments += "&nbsp;";
                                strAttachments += "</Td>";
                                strAttachments += "<Td width='25px'>";
                                strAttachments += item.Title.ToString();
                                strAttachments += "</Td>";
                                strAttachments += "<Td width='25px'>";
                                strAttachments += "<Strong>";
                                strAttachments += "No Attachments Available";
                                strAttachments += "</Strong>";
                                strAttachments += "</Td>";
                                strAttachments += "</Tr>";
                            }
                        }
                    }
                    strAttachments += "</Table>";
                    pnlAttachments.Controls.Add(new LiteralControl(strAttachments));
                });
            }
            catch (Exception Ex)
            {
                Response.Write(Ex.ToString());
            }
        }

No comments:

Post a Comment