using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Microsoft.SharePoint;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = ShowSPListAttachments(“1?, “http://SERVER:PORT/sites/NAME/”, “LISTNAME”, “TestList2010?, “TestList2010?, “http://SERVER:PORT/sites/NAME/LISTNAME/Lists/TestList2010/”);
GridView1.DataBind();
}
private DataTable ShowSPListAttachments(string listItemID, string spSiteCollection, string spTopLevelSite, string spListName, string spListSubFolderName, string spListAttachmentLocation)
{
SPSite spSite = null;
SPWeb spWeb = null;
SPList spList = null;
SPListItem spListItem = null;
SPAttachmentCollection spAttachmentCollection = null;
SPFolder spFolder = null;
DataTable dtAttachments = new DataTable();
string fileName = string.Empty;
int docID = 0;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (spSite = new SPSite(spSiteCollection))
{
using (spWeb = spSite.OpenWeb())
{
using (spWeb = spSite.AllWebs[spTopLevelSite]) { }
}
//spWeb = spSite.AllWebs[spTopLevelSite];
//spSite.OpenWeb();
}
});
spWeb.AllowUnsafeUpdates = true;
spList = spWeb.Lists[spListName];
spListItem = spList.GetItemById(int.Parse(listItemID));
spAttachmentCollection = spListItem.Attachments;
DataColumn dc;
dc = new DataColumn(“FileName”, typeof(string));
dtAttachments.Columns.Add(dc);
dc = new DataColumn(“FilePath”, typeof(string));
dtAttachments.Columns.Add(dc);
if (spAttachmentCollection.Count > 0)
{
spFolder = spWeb.Folders["Lists"].SubFolders[spListSubFolderName].SubFolders["Attachments"].SubFolders[spListItem["ID"].ToString()];
HyperLink objHyperLink = null;
string FileExt = String.Empty;
foreach (SPFile objSPFile in spFolder.Files)
{
if (spFolder.Files.Count != 0)
{
fileName = spListAttachmentLocation + objSPFile.ToString().Replace(” “, “%20?);
objHyperLink = new HyperLink();
objHyperLink.NavigateUrl = fileName.Trim();
objHyperLink.Text = objSPFile.Name;
objHyperLink.ID = docID.ToString();
docID++;
FileExt = objSPFile.Name.ToString().Substring(objSPFile.Name.LastIndexOf(“.”) + 1);
if (string.Compare(FileExt.Trim().ToUpper(), “PDF”, true) == 0)
{
objHyperLink.Target = “_blank”;
}
dtAttachments.Rows.Add(objHyperLink.Text, objHyperLink.NavigateUrl);
FileExt = string.Empty;
}
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
if (spWeb != null)
{
spWeb.AllowUnsafeUpdates = false;
spWeb.Dispose();
spWeb = null;
}
if (spSite != null)
{
spSite.Dispose();
spSite = null;
}
spList = null;
spListItem = null;
spAttachmentCollection = null;
spFolder = null;
}
return dtAttachments;
}
}