How to retrieve list item attachments with SharePoint 2013 Event Receiver in correct order

Simon picture Simon · Sep 29, 2013 · Viewed 10.9k times · Source

I've created a Standard SharePoint 2013 Event Receiver on a custom list.

Watched Event = "ItemAdded".

Later in my code I need to retrieve the attachments of the list item in the same order as the user inserted them. But unfortunately it seems that SharePoint does not do this by Default.

Example:

The User creates a list item and attach the following files

Picture_front.jpg

Picture_back.png

Picture_231.jpg

Now in my Event Receiver it is possible that I first get 'Picture_back' then 'Picture_front'... or in any other order.

How can I retrieve the attachments in the same order as they have been attached to the list item? I tried to use the SPFile Property 'TimeCreated' but this is also not working... They got the same timestamp :( (also if I am using 'Ticks')

Any ideas or I am doing something wrong?

Here is my Code:

    public override void ItemAdded(SPItemEventProperties properties)
        {

            SPAttachmentCollection attachments = properties.ListItem.Attachments;

            if (attachments.Count > 0)
            {
                int p = 1;
                Dictionary<string, string> attachementDict = new Dictionary<string, string>();

                try
                {
                    foreach (string attachement in attachments)
                    {
                        SPFile attachementFile = properties.ListItem.ParentList.ParentWeb.GetFile(properties.ListItem.Attachments.UrlPrefix + attachement);
                        string imageUrlPath = properties.WebUrl + attachementFile.ServerRelativeUrl;
                        string imageTimestamp = attachementFile.TimeCreated.Ticks.ToString();
                        // This Dict is used lator for sorting
                        // but at the Moment I get here the error that the same key already exists because of the same timestamp of the files :(
                        attachementDict.Add(imageTimestamp, imageUrlPath);
                    }
                }
                catch (Exception ex)
                {
                    // SPLog
                }
       }

Answer

Eng Feda&#39;a picture Eng Feda'a · Apr 12, 2016

here my code ..i hope it help you!

 try
            {
                string strUrl = SPContext.Current.Site.Url + "/" + subSite;
                using (SPSite Site = new SPSite(strUrl))
                {
                    using (SPWeb Web = Site.OpenWeb())
                    {
                        SPList List = Web.Lists[listName];
                        SPListItem item = List.GetItemById(ID);
                        foreach (String attachmentname in item.Attachments)
                        {
                            AnnouncementsCommon objAnnouncementsCommon = new AnnouncementsCommon();
                            String attachmentAbsoluteURL = item.Attachments.UrlPrefix + attachmentname;
                            objAnnouncementsCommon.AttachmentName = attachmentname;
                            objAnnouncementsCommon.AttachmentURL = attachmentAbsoluteURL;
                            lstAnnouncementsCommon.Add(objAnnouncementsCommon);
                        }
                    }
                }
            }
            catch (Exception Exc)
            {
                Microsoft.Office.Server.Diagnostics.PortalLog.LogString("SSC DAL Exception Occurred: {0} || {1}", Exc.Message, Exc.StackTrace);
            }
            return lstAnnouncementsCommon;
        }