简体   繁体   中英

List all available extended properties using EWS

I'm trying to find a way to list ALL extended properties for set of calendar items using EWS.

The problem is that all examples I managed to find online require me to know what those extended properties are, in advance. Here's the official MSDN example .

What am I supposed to do if I do not know the IDs or names of extended properties? Or if I don't even know if any extended properties exist at all?

I've tried the following code but it returns an exception...

            var calendarItems = service.FindAppointments(WellKnownFolderName.Calendar, view);
            var propertySet = new PropertySet(AppointmentSchema.ExtendedProperties);
            service.LoadPropertiesForItems(calendarItems, propertySet);

Here's the exception:

Microsoft.Exchange.WebServices.Data.ServiceResponseException: The request failed schema validation: The required attribute 'FieldURI' is missing.

There is no call in EWS to get all extended properties. The idea behind extended properties is that applications use them to store app-specific data, so only that app needs to know the specifics on their properties.

Extended MAPI can discover this information. https://github.com/stephenegriffin/mfcmapi has a ton of sample code for different tasks, including iterating named properties.

I was also looking similar and I just did a kind of reverse engineering. Since the extended property is the combination of Id (integer) and data type which we could not know as they are not documented on any MSDN. So, iterate 1 to some huge number like 15000 for string type property and find those which can be loaded successfully - this is the main trickest part which we can do by putting try-catch to bind that extended property. Then you could get the required one. Hope that helps.

  List<int> allStringIds = new List<int>();
for (int i = 0; i <= 15000; i++)
{
    allStringIds.Add(i);
}

ParallelOptions options = new ParallelOptions
{
    MaxDegreeOfParallelism = 200,
    CancellationToken = CancellationToken.None,
};

Parallel.For(0, allStringIds.Count, options, index =>
{
    try
    {
     ExtendedPropertyDefinition extendedPropertyDefinition = new ExtendedPropertyDefinition(index,
                   MapiPropertyType.String);
     latestMessage = EmailMessage.Bind(service, item.Id.UniqueId,
     new PropertySet(BasePropertySet.FirstClassProperties, extendedPropertyDefinition));
     _logger.Write("Supported string property id=" + index);
     supportedListId.TryAdd(index, index);
 }
 catch(Exception ex)
 {

 }
});

 foreach (var a in supportedListId)
 {
  ExtendedPropertyDefinition extendedPropertyDefinition = new ExtendedPropertyDefinition(a.Key,
  MapiPropertyType.String);
  allExtendedPropertyDefinitions.Add(extendedPropertyDefinition);
 }
 latestMessage = EmailMessage.Bind(service, item.Id.UniqueId,
 new PropertySet(BasePropertySet.FirstClassProperties, allExtendedPropertyDefinitions));
 
 foreach (var extendedProperty in latestMessage.ExtendedProperties)
 {
 if (extendedProperty.PropertyDefinition != null && extendedProperty.PropertyDefinition.Tag != null)
 {
  if (extendedProperty.Value != null)
  {
     _logger.Write($"OMG... extendedProperty id={extendedProperty.PropertyDefinition.Id}," +
         $" name={ extendedProperty.PropertyDefinition.Name}, value={extendedProperty.Value}");
    }
   }
}   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM