You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用EWS或Graph读写M365管理员创建的外部联系人

解决方案:读写Microsoft 365组织级外部联系人

EWS 实现方式

管理员创建的域内可见外部联系人属于**全局地址列表(GAL)**对象,并非用户个人邮箱的联系人文件夹内容,因此直接遍历用户邮箱文件夹无法找到。需通过EWS访问GAL进行查询:

核心步骤

  • 初始化EWS服务时,使用具备GAL读取权限的账号(如全局管理员)
  • 针对WellKnownFolderName.Directory(GAL目录)执行查询,筛选ExternalContact类型的对象

代码示例(C#)

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2016);
service.Credentials = new WebCredentials("admin@contoso.com", "your-password");
service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");

// 查询GAL中的外部联系人
FindItemsResults<Item> findResults = service.FindItems(
    WellKnownFolderName.Directory,
    new ItemView(100)
);

foreach (Item item in findResults.Items)
{
    if (item is Contact contact && contact.ExternalContact)
    {
        // 提取外部联系人信息
        Console.WriteLine($"姓名: {contact.GivenName} {contact.Surname}, 邮箱: {contact.EmailAddresses[EmailAddressKey.EmailAddress1]}");
    }
}

Microsoft Graph 实现方式(推荐)

Graph API提供更简洁的接口操作组织级外部联系人,分为读取和创建两种场景:

读取外部联系人

使用/me/people或/users/{userId}/people端点,筛选personType/classification为ExternalContact的对象,需申请People.Read或People.Read.All权限。

创建外部联系人

使用Beta版Graph API的/externalContacts端点,需申请ExternalContact.ReadWrite.All权限,仅管理员账号可操作。

代码示例(C#)

// 初始化Graph客户端(需提前配置身份验证提供者)
var graphClient = new GraphServiceClient(authProvider);

// 读取当前用户可见的外部联系人
var people = await graphClient.Me.People
    .Request()
    .Filter("personType/classification eq 'ExternalContact'")
    .Select("displayName,emailAddresses")
    .GetAsync();

foreach (var person in people)
{
    Console.WriteLine($"姓名: {person.DisplayName}, 邮箱: {person.EmailAddresses.FirstOrDefault()?.Address}");
}

// 创建组织级外部联系人(Beta版API)
var externalContact = new ExternalContact
{
    DisplayName = "John Doe",
    EmailAddresses = new List<EmailAddress>
    {
        new EmailAddress
        {
            Address = "john.doe@external.com",
            Name = "John Doe"
        }
    },
    PhoneNumbers = new List<PhoneNumber>
    {
        new PhoneNumber
        {
            Type = PhoneType.Mobile,
            Number = "+1 555-123-4567"
        }
    }
};

var createdContact = await graphClient.ExternalContacts
    .Request()
    .AddAsync(externalContact);

注意事项

  • 读取操作需确保账号有权限访问GAL数据
  • 创建外部联系人依赖Graph Beta版API,正式环境使用前需确认稳定性

内容的提问来源于stack exchange,提问作者Jeff McKay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 21:30:52