none
How to create a Mail Enabled Contact in Active Directory and Exchange Server using EWS Proxy class (Code available) RRS feed

  • Question

  • Hi friends,

     

    I want to create a Mail enabled contact through EWS Proxy classes. The contact will have couple of email addresses, one them will be an internal email and other will be a SMTP (external email) address. Can someone paste the code to accomplish this.

     

     

     

         try
    
         {
    
          // Create an object of create item type.
    
          CreateItemType createItemType = new CreateItemType();
    
    
    
          // Because you are creating a contact, save the item in the Contacts folder.
    
          createItemType.SavedItemFolderId = new TargetFolderIdType();
    
          DistinguishedFolderIdType contactsFolder = new DistinguishedFolderIdType();
    
          contactsFolder.Id = DistinguishedFolderIdNameType.contacts;
    
    
    
          createItemType.SavedItemFolderId.Item = contactsFolder;
    
    
    
          createItemType.Items = new NonEmptyArrayOfAllItemsType();
    
          createItemType.Items.Items = new ItemType[1];
    
    
    
          // Create a contact item type.
    
          ContactItemType contactItem = new ContactItemType();
    
    
    
          // Set the relevant properties on the contact.
    
          contactItem.FileAs = "Room Test" + title;
    
          // Set the contact name and job information.
    
          contactItem.GivenName = title;
    
          contactItem.Surname = title;      
    
          contactItem.CompanyName = "abc";
    
    
    
          // Set a single e-mail address for the contact.
    
          contactItem.EmailAddresses = new EmailAddressDictionaryEntryType[1];
    
          EmailAddressDictionaryEntryType address = new EmailAddressDictionaryEntryType();
    
          address.Key = EmailAddressKeyType.EmailAddress1;
    
          address.Value = string.Format("{0}@serverUSA.se", mail);
    
          contactItem.EmailAddresses[0] = address;
    
    
    
          //EmailAddressDictionaryEntryType address2 = new EmailAddressDictionaryEntryType();
    
          //address2.Key = EmailAddressKeyType.EmailAddress2;
    
          //address2.Value = string.Format("{0}@server.se", mail);
    
          //contactItem.EmailAddresses[1] = address2;
    
    
    
          // Set the contact telephone number.
    
          contactItem.PhoneNumbers = new PhoneNumberDictionaryEntryType[1];
    
          PhoneNumberDictionaryEntryType phoneEntry = new PhoneNumberDictionaryEntryType();
    
          phoneEntry.Key = PhoneNumberKeyType.BusinessPhone;
    
          phoneEntry.Value = "4600000000";
    
    
    
          contactItem.PhoneNumbers[0] = phoneEntry;
    
    
    
          createItemType.Items.Items[0] = contactItem;
    
    
    
          // Send the request to create the contact item; receive the response.
    
          CreateItemResponseType createItemResponse = service.CreateItem(createItemType);
    
    
    
          // Check the results of the request.
    
          if (createItemResponse.ResponseMessages.Items.Length > 0 &&
    
           createItemResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Success)
    
          {
    
           ItemInfoResponseMessageType responseMessage = createItemResponse.ResponseMessages.Items[0] as ItemInfoResponseMessageType;
    
           ContactItemType contactResponse = responseMessage.Items.Items[0] as ContactItemType;
    
           ExchangeHelper.LogSuccess("Created Contact Item with Id {0} and ChangeKey {1} with ID #" + contactResponse.ItemId.Id + " & Response:" + contactResponse.ItemId.ChangeKey);
    
           ok = true;
    
          }
    
          
    
         }
    
         catch (Exception e)
    
         {
    
    
    
         
    
         }

     

    Wednesday, February 16, 2011 10:19 PM

Answers

  • If you want to create an Active Directory Contact then you need to use the Exchange Management Shell and the new-mailcontact cmdlet. EWS is an API for accessing MailStore contact so can only be used to create Mapi contacts in Mailboxes or Public Folders

    Cheers
    Glen

    • Marked as answer by Falak Mahmood Thursday, February 17, 2011 9:20 AM
    Thursday, February 17, 2011 1:22 AM