【问题标题】:add new contact to existing contactgroup ews api将新联系人添加到现有联系人组 ews api
【发布时间】:2014-10-05 14:58:55
【问题描述】:

在过去的 1½ 天里,我一直在尝试找出这个 API,但现在我正在努力将新创建的联系人添加到现有组中。

我在第一次使用以下代码行运行程序时创建了一个组:

    public static ContactGroup CreateGroup(ExchangeService service)
    {
        // Create a new contact group object.
        ContactGroup myContactGroup = new ContactGroup(service);

        // Give the group a name.
        myContactGroup.DisplayName = "Test Contact Group";

        // Save the group.
        myContactGroup.Save();

        return myContactGroup;
    }

然后我使用以下代码将联系人添加到组:

    public static void AddContactToGroup(ContactGroup myContactGroup, Contact contact)
    {
        myContactGroup.Members.AddContactEmailAddress(contact, EmailAddressKey.EmailAddress2);
        myContactGroup.Update(ConflictResolutionMode.AlwaysOverwrite);

现在我想使用以下代码创建一个新联系人:

    public static Contact tempCont(ExchangeService service)
    {
        Contact contact = new Contact(service);
        contact.GivenName = "Jonas";
        contact.Surname = "Jonassen";
        contact.FileAsMapping = FileAsMapping.SurnameCommaGivenName;
        contact.PhoneNumbers[PhoneNumberKey.HomePhone] = "12345678";
        contact.EmailAddresses[EmailAddressKey.EmailAddress1] = new EmailAddress("jonas@jonassen.dk");

        PhysicalAddressEntry paEntry1 = new PhysicalAddressEntry();
        paEntry1.Street = "123 Main Boulevard";
        paEntry1.City = "Kbh";
        paEntry1.State = "";
        paEntry1.PostalCode = "1200";
        paEntry1.CountryOrRegion = "Denmark";
        contact.PhysicalAddresses[PhysicalAddressKey.Home] = paEntry1;

        contact.Save();

        return contact;
    }

现在我想将此新联系人添加到现有的联系人组中。问题是,我无法掌握“myContactGroup”。我猜我必须找到 ContactGroup 的 ID,或者可能是完全不同的东西。老实说,我不知道如何解决这个问题。任何帮助都会得到帮助!

【问题讨论】:

    标签: c# outlook contact exchangewebservices


    【解决方案1】:

    您需要做的是找到您想要添加联系人的 ContactGroup,例如使用 SearchFilter 来执行此操作,然后只需使用 Add Member 方法将联系人添加到组中,例如

                ItemView ItemView = new ItemView(1);
            SearchFilter cntGroup = new SearchFilter.IsEqualTo(ItemSchema.ItemClass, "IPM.DistList");
            SearchFilter cntGroupName = new SearchFilter.IsEqualTo(ContactGroupSchema.DisplayName, "Test Contact Group");
            SearchFilter sfCol = new SearchFilter.SearchFilterCollection(LogicalOperator.And) { cntGroup, cntGroupName };
    
            FolderId ContactFolder = new FolderId(WellKnownFolderName.Contacts, "user@domain.com");
            FindItemsResults<Item> fiCntResults = service.FindItems(ContactFolder, sfCol, ItemView);
            if (fiCntResults.Items.Count == 1)
            {
                ContactGroup contactGroup = (ContactGroup)fiCntResults.Items[0];
                Contact Contact2 = new Contact(service);
                Contact2.EmailAddresses[EmailAddressKey.EmailAddress1] = new EmailAddress("blah@blah.dk");
                Contact2.Subject = "Blah";
                Contact2.Save();
                GroupMember gm = new GroupMember(Contact2,EmailAddressKey.EmailAddress1);             
    
                contactGroup.Members.Add(gm);
                contactGroup.Update(ConflictResolutionMode.AlwaysOverwrite);
    
            }
    

    干杯 格伦

    【讨论】:

    • 这比我的回答要好得多。非常流畅的代码,格伦!
    【解决方案2】:

    您可以使用来自其他 stackoverflow 答案的代码,来自注释“//您可以使用以下代码获取 ItemID。”和向下。 https://stackoverflow.com/a/19663185/1289974

    【讨论】:

    • 感谢塔科曼奥特曼。我接受了 Glen 的回答,但我也设法对您的帖子进行了一些处理,并制定了一个也可以使用的解决方案(只是为了获取 ContactGroup id,我需要将新联系人添加到特定组。代码将看起来像这样,如果我走“你的路”:
    【解决方案3】:

    感谢塔科曼奥特曼。我同意了 Glen 的回答,但我也设法对您的帖子进行了一些处理,并制定了一个也可以使用的解决方案(只是为了获取 ContactGroup id,我需要将新联系人添加到特定组。代码将看起来像这样,如果我走“你的路”:

        public static ContactGroup FindContactGroup(ExchangeService service, String groupName)
        {
            // Instantiate the item view with the number of items to retrieve from the Contacts folder.
            ItemView view = new ItemView(9999);
    
            // Request the items in the Contacts folder that have the properties that you selected.
            FindItemsResults<Item> contactItems = service.FindItems(WellKnownFolderName.Contacts, view);
    
            // Loop through all contacts 
            foreach (Item item in contactItems)
            {
                //Check to see if ContactGroup
                if (item is ContactGroup)
                {
                    //Get the contact group
                    ContactGroup contactGroup = item as ContactGroup;
                    if (contactGroup.DisplayName == groupName)
                    {                        
                        return contactGroup;
                    }
                }
            }
            return null;
        }
    

    我看到的唯一问题是,这个解决方案会比 Glen 的回答使用更多的内存,因为它必须通过每个联系人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多