1

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
                }
       }
Simon
  • 377
  • 2
  • 11
  • 30
  • Do your attachments have different created times? Have you tried `TimeLastModified`? If the times are the **same** as the underlying ListItem (which might very well be), you are out of luck as the `SPAttachmentCollection` does not have anything like order. – Dennis G Sep 29 '13 at 12:34
  • 1
    @moontear Thanks for your answer. Yes, the attachments do have different created times. But this I can also not use for sorting because this given order would be also not correct. I got another idea to take the "Attachment Adding" Event and try to Change the filename of each attachment... But seems also not so easy... – Simon Sep 29 '13 at 13:17
  • Not an answer - just a comment ;-) `ItemAttachmentAdding` is a very good idea and maybe your only solution. Take note of the asynch nature of some event receivers - this event occures **before** the attachment is added, so you might look into the before and after property bags. – Dennis G Sep 29 '13 at 13:36
  • @moontear or maybe not such a good idea... Because how I could handle here a renaming function? The whole context will be Zero for each attachment? If I like to insert a number in the Name then the number will be always the same because increment i++ will be lost for next one? – Simon Sep 29 '13 at 13:59
  • 1
    omg, I can use the Count of the item collection. Will Play a Little bit with this and come back here as soon as I have more Information. – Simon Sep 29 '13 at 14:24
  • The increment will be lost yes - you would always have to check the files again and check for existing numbers. The count, sure that can help with the increment number. Renaming files is not even possible with attachments - at least not for end users. – Dennis G Sep 29 '13 at 16:31

2 Answers2

0

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;
        }
Abhinav Singh Maurya
  • 3,313
  • 8
  • 33
  • 51
0

As an alternative approach you could use your receiver to store the attachments in a picture library and add two fields to this library: a lookup column to your original custom list item and an option column with "default", "front view", "back view" (or similar).

One advantage is that you can easily update your images in the future and another is that SharePoint automatically creates two preview miniatures in convenient sizes for your images which allows you to reduce bandwidth.

Steve T
  • 570
  • 4
  • 13