Example: Returning a list of attachments using EWS


Here is a sample on getting and returning a list of attachments on an item using Exchange Web Services (EWS).


        //


        //—————————————————————————————–


        // GetAttachmentsList.


        // Gets a list of file attachemnts on an item and puts them into <FileAttachmentType>.


        // Returns the number of attchments found.


        //


        // Here is how  you might call this code:


        //    List<FileAttachmentType> attachmentIds = null;


        //    string sLines = string.Empty;


        //    int iAttachments = 0;


        //    iAttachments = GetAttachmentsList(binding, currentCalendarItem.ItemId, ref attachmentIds);


        //    Debug.WriteLine(string.Format(“{0} Attachments”, iAttachments));


        //    foreach (FileAttachmentType oFileAttachmentType in attachmentIds)


        //    {


        //        sLines += “        Id:                ” + oFileAttachmentType.AttachmentId.Id + “\r\n”;


        //        sLines += “        RootItemId:        ” + oFileAttachmentType.AttachmentId.RootItemId + “\r\n”;


        //        sLines += “        RootItemChangeKey: ” + oFileAttachmentType.AttachmentId.RootItemChangeKey + “\r\n”;


        //        sLines += “        ContentId:         ” + oFileAttachmentType.ContentId + “\r\n”;


        //        sLines += “        ContentLocation:   ” + oFileAttachmentType.ContentLocation + “\r\n”;


        //        sLines += “        ContentType:       ” + oFileAttachmentType.ContentType + “\r\n”;


        //        sLines += “        Name:              ” + oFileAttachmentType.Name + “\r\n”;


        //        Debug.WriteLine(sLines);


        //    }


        //—————————————————————————————–


        public static int GetAttachmentsList


            (


                ExchangeServiceBinding binding,


                ItemIdType id,


                ref List<FileAttachmentType> attachmentIds


            )


        {


 


            int iAttachmentCount = 0;


            attachmentIds = new List<FileAttachmentType>();


 


            GetItemType getItemRequest = new GetItemType();


            getItemRequest.ItemIds = new ItemIdType[] { id };


            getItemRequest.ItemShape = new ItemResponseShapeType();


 


            getItemRequest.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;


            PathToUnindexedFieldType hasAttachPath = new PathToUnindexedFieldType();


            hasAttachPath.FieldURI = UnindexedFieldURIType.itemHasAttachments;


            PathToUnindexedFieldType attachmentsPath = new PathToUnindexedFieldType();


            attachmentsPath.FieldURI = UnindexedFieldURIType.itemAttachments;


 


            // Add  additional properties…? 


 


            //


            getItemRequest.ItemShape.AdditionalProperties = new BasePathToElementType[]{


                 hasAttachPath, attachmentsPath };


 


            GetItemResponseType getItemResponse = binding.GetItem(getItemRequest);


            ItemInfoResponseMessageType getItemResponseMessage = getItemResponse.ResponseMessages.Items[0] as ItemInfoResponseMessageType;


 


            if (getItemResponseMessage.ResponseCode == ResponseCodeType.NoError)


            {


 


                ItemType item = getItemResponseMessage.Items.Items[0];


                // Never rely on item.HasAttachments – its mostly likely not set as you would think.


                if ((item.Attachments != null) && (item.Attachments.Length > 0))


                    {


 


                    for (int attachmentIndex = 0; attachmentIndex < item.Attachments.Length; attachmentIndex++)


                    {


                        // For now, let’s only consider file attachments instead of item attachments.


                        //


                        FileAttachmentType oFoundAttachment = item.Attachments[attachmentIndex] as FileAttachmentType;


                        if (oFoundAttachment != null)


                        {


                            attachmentIds.Add(oFoundAttachment);


                            //Debug.WriteLine(” Id: ” + oFoundAttachment.AttachmentId.Id);


                            iAttachmentCount++;


                        }


                    }


                }


            }


 


            return iAttachmentCount;


        }

Skip to main content