简体   繁体   中英

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

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
                }
       }

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;
        }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM