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

时间:2021-01-18 15:43:07

I've been trying to figure out this API for the past 1½ day, but now im struggling hard with getting a newly created contact added to a already existing group.

我一直试图在过去的一天半里找出这个API,但是现在我很难努力将新创建的联系人添加到已经存在的组中。

I have created a group the first time I ran the program with the following line of code:

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

    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;
    }

Then i've added a contact to the group with the following piece of code:

然后我使用以下代码向该组添加了联系人:

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

Now I want to create a new contact with the following code:

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

    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;
    }

Now I would like to add this new contact to the exisiting contactGroup. The problem is, that I cant get hold of "myContactGroup". Im guessing I have to find the ID of the ContactGroup, or maybe something totally different. I honestly dont know how to figure this out. Any help would be appriciated!

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

3 个解决方案

#1


3  

What you need to do is Find the ContactGroup you want to add the contact to eg use a SearchFilter to do this and then just use the Add Member method to add the contact to the group eg

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

        }

Cheers Glen

干杯格伦

#2


1  

Thanks Ultroman the Tacoman. I went with Glen's answer, but I managed to work a bit with your post too, and made a solution that would work too (just to get out the ContactGroup id, which I needed to add a new contact to a specific group. Code will look like this, if I went "your way":

感谢Ultroman the Tacoman。我接受了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;
    }

Only problem I see, is that this solution would use a bit more memory then Glen's answer, since it has to go through every contact.

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

#3


0  

You can use the code from this other * answer, from the comment "//You can get the ItemID with the following code." and downwards. https://*.com/a/19663185/1289974

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

#1


3  

What you need to do is Find the ContactGroup you want to add the contact to eg use a SearchFilter to do this and then just use the Add Member method to add the contact to the group eg

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

        }

Cheers Glen

干杯格伦

#2


1  

Thanks Ultroman the Tacoman. I went with Glen's answer, but I managed to work a bit with your post too, and made a solution that would work too (just to get out the ContactGroup id, which I needed to add a new contact to a specific group. Code will look like this, if I went "your way":

感谢Ultroman the Tacoman。我接受了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;
    }

Only problem I see, is that this solution would use a bit more memory then Glen's answer, since it has to go through every contact.

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

#3


0  

You can use the code from this other * answer, from the comment "//You can get the ItemID with the following code." and downwards. https://*.com/a/19663185/1289974

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