Sample: How to get the number of file attachments with EWS.


Since HasAttachments does not really give you the number of file attachments, we you will find that you need to work around it.  You can use code like that below to get the real count of file attachments on an item.


 


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


    // GetFileAttachmentsCount


    //   Returns number of attachments on an item.


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


    public static int GetFileAttachmentsCount(ExchangeServiceBinding binding, ItemIdType id)


    {


        int iAttachmentCount = 0;


 


        // Use GetItem on the Id to get the Attachments collection 


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


            // Don’t rely on  HasAttachments – It does not mean what you thing it would.


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


            {


 


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


                {


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


                    if (almostAnAttachment != null)


                    {


                        iAttachmentCount += 1;


                    }


                }


 


            }


 


        }


        return iAttachmentCount;


    }

Comments (3)

  1. Here is a helpful sample of using EWS to get a list of calendar items and displaying them in a ListView.

  2. Pradip Bobhate says:

    Thanks Dan for this post.

    I am new to EWS development, could you please tell me how can I get ItemIDType. which i need to pass in this function GetFileAttachmentsCount()

  3. Pradip Bobhate says:

    hi Dan,

    Please ignore my previous comment now I am able to get Attachment count.

    Thanks for our post.

    Now I want to download attachment on local file system, Could  you please tell me how can we do this?

Skip to main content