Example: Populate a ListView with a list of attachments using EWS
I thought it would be fun to blog another sample of getting a list of attachments using EWS. So, here is a handly method for populating a list of attachments.
// -----------------------------------------------------------------------------------------
// GetFileAttachmentsListLv
// Sets a listview to the attachments on an item.
// Returns number of attachments.
// -----------------------------------------------------------------------------------------
public static int GetFileAttachmentsListLv(ExchangeServiceBinding binding, ItemIdType id, ref ListView oListView)
{
int iAttachmentCount = 0;
//List<RequestAttachmentIdType> attachmentIds = new List<RequestAttachmentIdType>();
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;
oListView.Clear();
oListView.View = View.Details; // Set the view to show details.
oListView.GridLines = true; // Display grid lines.
oListView.Columns.Add("Name", 90, HorizontalAlignment.Left);
oListView.Columns.Add("Id", 120, HorizontalAlignment.Left);
oListView.Columns.Add("RootItemId", 120, HorizontalAlignment.Left);
oListView.Columns.Add("RootItemChangeKey", 120, HorizontalAlignment.Left);
oListView.Columns.Add("ContentType", 80, HorizontalAlignment.Left);
oListView.Columns.Add("ContentId", 80, HorizontalAlignment.Left);
oListView.Columns.Add("ContentLocation", 80, HorizontalAlignment.Left);
if (getItemResponseMessage.ResponseCode == ResponseCodeType.NoError)
{
ItemType item = getItemResponseMessage.Items.Items[0];
// dont 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 almostAnAttachment = item.Attachments[attachmentIndex] as FileAttachmentType;
if (almostAnAttachment != null)
{
// Note: Use GetAttachment to get the actual attachment.
RequestAttachmentIdType requestId = new RequestAttachmentIdType();
requestId.Id = almostAnAttachment.AttachmentId.Id;
ListViewItem lvItem = new ListViewItem(String.Format("{0}", almostAnAttachment.Name));
lvItem.SubItems.Add(String.Format("{0}", almostAnAttachment.AttachmentId.Id));
lvItem.SubItems.Add(String.Format("{0}", almostAnAttachment.AttachmentId.RootItemId));
lvItem.SubItems.Add(String.Format("{0}", almostAnAttachment.AttachmentId.RootItemChangeKey));
lvItem.SubItems.Add(String.Format("{0}", almostAnAttachment.ContentId));
lvItem.SubItems.Add(String.Format("{0}", almostAnAttachment.ContentLocation));
lvItem.SubItems.Add(String.Format("{0}", almostAnAttachment.ContentType));
oListView.Items.Add(lvItem);
lvItem = null;
iAttachmentCount++;
}
}
}
}
return iAttachmentCount;
}
Comments
- Anonymous
January 09, 2009
PingBack from http://www.codedstyle.com/example-populate-a-listview-with-a-list-of-attachments-using-ews/