通过扩展属性,可以将自定义键值对附加到Exchange服务器上的项目和文件夹。有多个联机资源描述如何使用扩展属性,并列出了现有Exchange客户端用于存储公共和自定义属性的许多神奇值。以下不是一个的全面描述,但我们确实打算支持EWS提供的所有可能性。
# 如果文件夹项具有扩展属性,则需要先注册再访问
# 创建ExtendedProperty的子类,然后定义一组值:
from exchangelib import Account, ExtendedProperty, CalendarItem, Folder, Message
a = Account(...)
class LunchMenu(ExtendedProperty):
property_set_id = '12345678-1234-1234-1234-123456781234'
property_name = 'Catering from the cafeteria'
property_type = 'String'
# 在您选择的类型上注册该属性
CalendarItem.register('lunch_menu', LunchMenu)
# 和其他属性一样,新注册的属性可以作为属性“lunch_menu”使用
item = CalendarItem(..., lunch_menu='Foie gras et consommé de légumes')
item.save()
for i in a.calendar.all():
print(i.lunch_menu)
# 如果不需要,可以删除
CalendarItem.deregister('lunch_menu')
# 您也可以创建命名属性,例如从Outlook中的用户定义字段创建:
class LunchMenu(ExtendedProperty):
distinguished_property_set_id = 'PublicStrings'
property_name = 'Catering from the cafeteria'
property_type = 'String'
# 我们支持带标记的扩展属性。这是可以添加到Outlook中的项目的“已完成”和“后续”标志的定义:
class Flag(ExtendedProperty):
property_tag = 0x1090
property_type = 'Integer'
# 或者使用属性ID:
class MyMeetingArray(ExtendedProperty):
property_set_id = '00062004-0000-0000-C000-000000000046'
property_type = 'BinaryArray'
property_id = 32852
# 或使用可分辨属性集与属性ID组合使用,这是对Outlook投票请求的响应的定义:
class VoteResponse(ExtendedProperty):
distinguished_property_set_id = 'Common'
property_id = 0x00008524
property_type = 'String'
# 扩展属性也适用于文件夹。对于文件夹,一次只能注册所有文件夹类型的自定义字段
# 这是因为当某些文件夹具有自定义字段而其他文件夹没有自定义字段时,很难提供一致的API
# 自定义字段必须注册到通用文件夹或层次文件夹类的根目录上
#
# 以下是获取文件夹大小(以字节为单位)的示例:
class FolderSize(ExtendedProperty):
property_tag = 0x0e08
property_type = 'Integer'
Folder.register('size', FolderSize)
print(a.inbox.size)
# 以下代码是如何使用中列出的任何MAPI属性
# https://docs.microsoft.com/en-us/office/client-developer/outlook/mapi/mapi-properties.
# 以PidLidTaskDueDate为例,这是Outlook中使用后续标志生成的邮件的截止日期
#
# PidLidTaskDueDate文档位于
# https://docs.microsoft.com/en-us/office/client-developer/outlook/mapi/pidlidtaskduedate-canonical-property.
# 属性ID为`0x00008105`,值设置为`PSETID_Task`. 但EWS需要`PSETID_Task`的UUID
# 所以,在PDF文件中查找
# https://docs.microsoft.com/en-us/openspecs/exchange_server_protocols/ms-oxprops/f6ab1613-aefe-447d-a49c-18217230b148
# UUID为`00062003-0000-0000-C000-000000000046`。该属性为`PT_SYSTIME`类型,也就是 `SystemTime` (see https://docs.microsoft.com/en-us/dotnet/api/microsoft.exchange.webservices.data.mapipropertytype )
#
# 总之,到期日的定义为:
class FlagDue(ExtendedProperty):
property_set_id = '00062003-0000-0000-C000-000000000046'
property_id = 0x8105
property_type = 'SystemTime'
Message.register('flag_due', FlagDue)