Welcome to MSDN Blogs Sign in | Join | Help

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;

    }

Published Saturday, January 10, 2009 2:43 AM by Daniel

Comments

# Sample: Howto display a list of calendar items in ListView using EWS

Friday, January 09, 2009 9:48 PM by Dan's WebDAV 101

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

Anonymous comments are disabled
 
Page view tracker