1
votes

I am working with Exchange Web Services Managed API. I am adding a single extended property to mail items in inbox as they get processed based on some condition. Thus, not all mails will get this extended property attached to them.

Next I am refetching all mails in inbox and if they have this property attached to them, I process them again.

Below is the simple method getAllMailsInInbox(), I have written to refetch the mails in inbox:

class MyClass
{
    private static Guid isProcessedPropertySetId;
    private static ExtendedPropertyDefinition isProcessedPropertyDefinition = null;

    static MyClass()
    {
        isProcessedPropertySetId = new Guid("{20F3C09F-7CAD-44c6-BDBF-8FCB324244}");
        isProcessedPropertyDefinition = new  ExtendedPropertyDefinition(isProcessedPropertySetId, "IsItemProcessed", MapiPropertyType.String);
    }

    public List<EmailMessage> getAllMailsInInbox()
    {
        List<EmailMessage> emails = new List<EmailMessage>();
        ItemView itemView = new ItemView(100, 0);
        FindItemsResults<Item> itemResults = null;

        PropertySet psPropSet = new PropertySet(BasePropertySet.IdOnly);
        itemView.PropertySet = psPropSet;

        PropertySet itItemPropSet = new PropertySet(BasePropertySet.IdOnly,                                             
                                          ItemSchema.Attachments,
                                          ItemSchema.Subject,                                               
                                          ItemSchema.Importance,
                                          ItemSchema.DateTimeReceived,
                                          ItemSchema.DateTimeSent,
                                          ItemSchema.ItemClass,
                                          ItemSchema.Size,
                                          ItemSchema.Sensitivity,
                                          EmailMessageSchema.From,
                                          EmailMessageSchema.CcRecipients,
                                          EmailMessageSchema.ToRecipients,       
                                          EmailMessageSchema.InternetMessageId,                                                              
                                          ItemSchema.MimeContent,
                                          isProcessedPropertyDefinition);   //***

         itemResults = service.FindItems(WellKnownFolderName.Inbox, itemView); 
         service.LoadPropertiesForItems(itemResults.Items, itItemPropSet);

         String subject = itItem.Subject; //Exception: "You must load or assign this property before you can read its value."
         //....
    }
}

As you can see, on call service.LoadPropertiesForItems(), it does not load any properties, thus resulting in You must load or assign this property before you can read its value. exception while accessing any of those properties.

If I remove isProcessedPropertyDefinition from the itItemPropSet property set, it fetches all the properties properly.

So can I just know how can I fetch all built in EmailMessage properties along with the extended property?

2

2 Answers

3
votes

Your GUID is two digits too short after the last dash. Strange that you're not seeing a FormatException. Still, you should update your code to inspect the GetItemResponse for each item. That way if some error occurs on one item, your code can be aware of it. That means you'll need to make another collection to return.

Update your code with this:

ServiceResponseCollection<ServiceResponse> responses = service.LoadPropertiesForItems(itemResults.Items, itItemPropSet);

foreach (ServiceResponse response in responses)
{
       if (response.Result == ServiceResult.Error)
       {
           // Handle the error associated
       }

       else
       {
           String subject = (response as GetItemResponse).Item.Subject;
       }
}
0
votes

Instead of doing service.LoadPropertiesForItems(itemResults.Items, itItemPropSet);

Try doing

itemResult.LoadPropertiesForItems(itItemPropSet);

Casue once you have the item, you can load the extended property of the item by loading the specific one.