添加新联系人到现有的contactgroup ews api

时间:2014-08-12 13:49:20

标签: c# outlook contact exchangewebservices

我一直试图在过去的一天半里找出这个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;
    }

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

3 个答案:

答案 0 :(得分:3)

您需要做的是找到要添加联系人的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);

        }

干杯 格伦

答案 1 :(得分:1)

感谢Ultroman Tacoman。我接受了Glen的回答,但我也设法对你的帖子进行了一些工作,并提出了一个也可以工作的解决方案(只是为了得到ContactGroup id,我需要在特定的地方添加一个新的联系人如果我去了#34;你的方式&#34;:

,代码将会是这样的
    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的回答,因为它必须通过每次联系。

答案 2 :(得分:0)

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