My SharePoint Blog

Blogs On SharePoint Technologies


Search Site


My SharePoint Blog recommends any of the following books...


Recent comments

Tags

None

    Categories


    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2010

    How To Iterate SharePoint Attachments Using The API

    Looking for a way to iterate a SharePoint list item (SPListItem) for all attachments and render them on your page?  Here is a nice example....

    private String x_renderAttachmentData(SPListItem listItem)
    {
        String strAttachmentData = String.Empty;

        try
        {

            if (listItem.Attachments.Count > 0)
            {

                foreach (string fileName in listItem.Attachments)
                {
                    SPFile spFile = listItem.ParentList.ParentWeb.GetFile(listItem.Attachments.UrlPrefix + fileName);
                    int fileSize = (int)spFile.Length / 1000;
                    strAttachmentData += "<div class='attachment'>"
                                       + "<img src='_layouts/images/" + spFile.IconUrl + "'> "
                                       + "<a target='_blank' href='" + URL_ROOT + "/" + spFile.Url + "'>"
                                       + spFile.Name + "</a> &nbsp; (" + fileSize + " KB)"
                                       + "<div class='hr'></div>Attachment version: " + spFile.UIVersionLabel
                                       + "<br/>Created at " + spFile.TimeCreated.ToString() + " by "
                                       + spFile.Author.ToString().Substring(spFile.Author.ToString().IndexOf('\\') + 1)
                                       + "<br/>Last modified at " + spFile.TimeLastModified.ToString() + " by "
                                       + spFile.ModifiedBy.ToString().Substring(spFile.ModifiedBy.ToString().IndexOf('\\') + 1)
                                       + "</div><div style='clear:left;'></div>";  // just call me a css genius.
                   
                }
            }
            return strAttachmentData;

        }
        catch (Exception exp)
        {
            // handle exp.
        }
    }

     

     

    Output for this function looks like this... 

     

     

    Here is the css if you are interested....

    .attachment
    {
     color:#999999;
     font-size:9px;
     margin-bottom:0px;
     float:left;
    }
    .attachment img{margin-bottom:-2px;}
    .attachment .hr
    {
     border-top:1px solid #bbbbbb;
     margin-top:2px;
    }
    .attachment a:link,
    .attachment a:visited,
    .attachment a:hover
    {
     font-size:14px;
     line-height:18px;
    }

     

     


    Categories: SharePoint
    Posted by Kevin on Friday, September 26, 2008 3:07 PM
    Permalink | Comments (2) | Post RSSRSS comment feed

    Comments