Ashton
Ashton

Reputation: 1365

Read custom column with EWS managed API

I need to read and write a custom column that I created in a process mailbox (public mailbox?) within Outlook.

I think I'm supposed to use the ExtendedPropertyDefinition somehow, however, I do not know how. I do not have the GUID for the column, if this makes sense.

I've named the custom column 'Engineer', and used this code, but I get 0 count for the ext props.

ExtendedPropertyDefinition myExtDef= 
       new ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings,
                                      "Engineer",
                                      MapiPropertyType.String);

And then

PropertySet propertySet = 
                 new PropertySet(BasePropertySet.FirstClassProperties, myExtDef);

Finally

foreach (Item item in findResults)
{ 
    EmailMessage message = 
               EmailMessage.Bind(service, item.Id, new PropertySet(propertySet));
}

As I debug and check an 'item' in findResults, I see the subject property and all that, but the ExtendedProperties count is 0. Looks like a problem with the way I define my extended properties to me.

Can anybody help me how to read and write the custom column?

Edit: I'm now not so sure if this can be achieved at all using the managed API.. If there's anyone out there with some ideas, I'll take it. I've checked all the below, but to no avail.

Accessing custom contacts using EWS managed API http://msdn.microsoft.com/en-us/library/office/dd633697(v=exchg.80).aspx

And others...

Upvotes: 0

Views: 2511

Answers (1)

Bob Bunn
Bob Bunn

Reputation: 613

Marton,

It looks as though you have the basic concept correct for working with extended properties. It's hard to say where the issue is because there are only a few snippets of your code. Here is an example that you should be able to build from. It creates a new mail message, sets the extended property and then saves the message (to the drafts folder).

// Create a definition for the extended property.
ExtendedPropertyDefinition extendedPropertyDefinition = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings, "Engineer", MapiPropertyType.String);

// Create an e-mail message that you will add the extended property to.
EmailMessage message = new EmailMessage(service);
message.Subject = "Saved with custom ExtendedPropertyDefinition.";
message.Body = "The Engineer custom value is stored within the extended property.";
message.ToRecipients.Add("[email protected]");

// Add the extended property to an e-mail message object.
message.SetExtendedProperty(extendedPropertyDefinition, "Save some customer value");
message.Save();

Now to verify the message was created with the extended property you can use FindItems. The following example will search the drafts folder for messages with the "Engineer" extended property.

ItemView view = new ItemView(10);
// Create a definition for the extended property.
ExtendedPropertyDefinition extendedPropertyDefinition = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings, "Engineer", MapiPropertyType.String);

// Create a search filter the filters email based on the existence of the extended property.
SearchFilter.Exists customPropertyExistsFilter = new SearchFilter.Exists(extendedPropertyDefinition);

// Create a property set that includes the extended property definition.
view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, extendedPropertyDefinition);

// Search the drafts folder with the defined view and search filter.
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Drafts, customPropertyExistsFilter, view);

// Search the e-mail collection returned in the results for the extended property.
foreach (Item item in findResults.Items)
{
    Console.WriteLine(item.Subject);

    // Check whether the item has the custom extended property set.
    if (item.ExtendedProperties.Count > 0)
    {
        // Display the extended name and value of the extended property.
        foreach (ExtendedProperty extendedProperty in item.ExtendedProperties)
        {
            Console.WriteLine(" Extended Property Name: " + extendedProperty.PropertyDefinition.Name);
            Console.WriteLine(" Extended Property Value: " + extendedProperty.Value);
        }
    }
    else
    {
        Console.WriteLine(" This email does not have the 'Engineer' extended property set on it");
    }
}

In order to view and update these extended properties in a custom form in Outlook there is some additional work that needs to be done. Outlook forms use an additional property to store extended properties as a binary field. The PidLidPropertyDefinitionStream needs to be modified as well as the extended properties. Unfortunately EWS Managed API does not do this for you, so you'll have to write some code to read/update this property yourself. I don't have any example code to show you, but here are some links about the property structure that will help you along the way:

Upvotes: 2

Related Questions