Question

I cannot work out how I can do what I need to do.

I have a method called 'MarkAsRead' that takes an ItemID and should mark the mail item as read.

But it seems that the Item doesn't have an 'IsRead' proeprty, only an email does, so I need to cast my Exchange WebServices mail item to an email message.

here is the code:

try {

            System.Diagnostics.Debugger.Break();

            //creates an object that will represent the desired mailbox
            Mailbox mb = new Mailbox(common.strInboxURL); 

            //creates a folder object that will point to inbox fold
            FolderId fid = new FolderId(WellKnownFolderName.Inbox, mb);

            //this will bind the mailbox you're looking for using your service instance
            Microsoft.Exchange.WebServices.Data.Folder inbox = Microsoft.Exchange.WebServices.Data.Folder.Bind(service, fid);

            //// if the property is not loaded yet, first load it
            //mail.Load(PropertySet(BasePropertySet.IdOnly, EmailMessageSchema.IsRead));

            //if (!mail.IsRead) // check that you don't update and create unneeded traffic
            //{
            //    mail.IsRead = true; // mark as read
            //    mail.Update(ConflictResolutionMode.AutoResolve); // persist changes
            //}

            // As a best practice, limit the properties returned to only those that are required.
            PropertySet propSet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject);

            // Bind to the existing item by using the ItemId.
            // This method call results in a GetItem call to EWS.
            Item item = Item.Bind(service, itemId, propSet);


            EmailMessage mail = Item.Bind(service, itemId, propSet);

            item.Load(new PropertySet(BasePropertySet.IdOnly,EmailMessageSchema.IsRead));
            item.IsRead = true;

            item.Update(ConflictResolutionMode.AlwaysOverwrite);

            return true;

        }

I am trying to do something like this:

                // if the property is not loaded yet, first load it
            mail.Load(PropertySet(BasePropertySet.IdOnly, EmailMessageSchema.IsRead));

            if (!mail.IsRead) // check that you don't update and create unneeded traffic
            {
                mail.IsRead = true; // mark as read
                mail.Update(ConflictResolutionMode.AutoResolve); // persist changes
            }

unfortunately I need to be able to get a unique email message from the item.

how can I do that please?

Was it helpful?

Solution

Philip-

  1. You can add EmailMessageSchema.IsRead to your property set, so that you can get it in your Bind call, and then you don't have to call Load at all.

    PropertySet propSet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, EmailMessageSchema.IsRead);

  2. The EmailClass derives from the Item class, so Bind works on both. So you can do the following:

    EmailMessage mail = EmailMessage.Bind(service, itemId, propSet);

Put that together and you've got this:

PropertySet propSet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, EmailMessageSchema.IsRead);

EmailMessage mail = EmailMessage.Bind(service, itemId, propSet);

if (!mail.IsRead) // check that you don't update and create unneeded traffic
{
    mail.IsRead = true; // mark as read
    mail.Update(ConflictResolutionMode.AutoResolve); // persist changes
}

OTHER TIPS

Philip,

When testing my code I added just a little bit to the Item.Bind() from your code to make this work:

EmailMessage mail = Item.Bind(service, itemId, propSet) as EmailMessage;

The as EmailMessage will cast it to the appropriate type for you. After that I was able to see the isRead property.

I hope this helps. If this does resolve your problem, please mark the post as answered.

Thanks,

--- Bob ---

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top