2016-05-13 42 views
1

私が行う必要があるのは、すべての連絡先をMicrosoft Exchangeから取得することだけです。私はいくつかの研究を行いました。私にはEWS Managed APIを使うべきです。私はVisual StudioとC#のプログラミング言語を使用しています。EWS Managed APIを使用してMicrosoft Exchangeからすべての連絡先を取得する方法?

私はプログラム内のメッセージを特定のメールに送信できるので、Office365アカウントに接続できると思います。しかし、私は連絡先を取得することができません。

連絡先をソースコードで直接作成すると、連絡先はOffice365-> Peopleアプリケーションで作成されます。しかし、私は理由を知らない!私は、Exchangeアプリケーションを使って作業していると思っていました。

要約: Office365->管理 - > Exchange->受諾者 - >連絡先からすべての連絡先を取得する方法はありますか?すでにFindItems()メソッドをやって良い仕事をしているよう

class Program 
{ 
    static void Main(string[] args) 
    { 
     // connecting to my Exchange account 
     ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack; 
     ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); 
     service.Credentials = new WebCredentials("[email protected]", "password123"); 

     /* 
     // debugging 
     service.TraceEnabled = true; 
     service.TraceFlags = TraceFlags.All; 
     */ 

     service.AutodiscoverUrl("[email protected]", RedirectionUrlValidationCallback); 

     // send a message works good 
     EmailMessage email = new EmailMessage(service); 
     email.ToRecipients.Add("[email protected]"); 
     email.Subject = "HelloWorld"; 
     email.Body = new MessageBody("Toto je testovaci mail"); 
     email.Send(); 

     // Create the contact creates a contact in Office365 -> People application..Don't know why there and not in Office365 -> Exchange 
     /*Contact contact = new Contact(service); 

     // Specify the name and how the contact should be filed. 
     contact.GivenName = "Brian"; 
     contact.MiddleName = "David"; 
     contact.Surname = "Johnson"; 
     contact.FileAsMapping = FileAsMapping.SurnameCommaGivenName; 

     // Specify the company name. 
     contact.CompanyName = "Contoso"; 

     // Specify the business, home, and car phone numbers. 
     contact.PhoneNumbers[PhoneNumberKey.BusinessPhone] = "425-555-0110"; 
     contact.PhoneNumbers[PhoneNumberKey.HomePhone] = "425-555-0120"; 
     contact.PhoneNumbers[PhoneNumberKey.CarPhone] = "425-555-0130"; 

     // Specify two email addresses. 
     contact.EmailAddresses[EmailAddressKey.EmailAddress1] = new EmailAddress("[email protected]"); 
     contact.EmailAddresses[EmailAddressKey.EmailAddress2] = new EmailAddress("[email protected]"); 

     // Specify two IM addresses. 
     contact.ImAddresses[ImAddressKey.ImAddress1] = "[email protected]"; 
     contact.ImAddresses[ImAddressKey.ImAddress2] = " [email protected]"; 

     // Specify the home address. 
     PhysicalAddressEntry paEntry1 = new PhysicalAddressEntry(); 
     paEntry1.Street = "123 Main Street"; 
     paEntry1.City = "Seattle"; 
     paEntry1.State = "WA"; 
     paEntry1.PostalCode = "11111"; 
     paEntry1.CountryOrRegion = "United States"; 
     contact.PhysicalAddresses[PhysicalAddressKey.Home] = paEntry1; 

     // Specify the business address. 
     PhysicalAddressEntry paEntry2 = new PhysicalAddressEntry(); 
     paEntry2.Street = "456 Corp Avenue"; 
     paEntry2.City = "Seattle"; 
     paEntry2.State = "WA"; 
     paEntry2.PostalCode = "11111"; 
     paEntry2.CountryOrRegion = "United States"; 
     contact.PhysicalAddresses[PhysicalAddressKey.Business] = paEntry2; 

     // Save the contact. 
     contact.Save(); 
     */ 


     // msdn.microsoft.com/en-us/library/office/jj220498(v=exchg.80).aspx 
     // Get the number of items in the Contacts folder. 
     ContactsFolder contactsfolder = ContactsFolder.Bind(service, WellKnownFolderName.Contacts); 
     Console.WriteLine(contactsfolder.TotalCount); 

     // Set the number of items to the number of items in the Contacts folder or 50, whichever is smaller. 
     int numItems = contactsfolder.TotalCount < 50 ? contactsfolder.TotalCount : 50; 

     // Instantiate the item view with the number of items to retrieve from the Contacts folder. 
     ItemView view = new ItemView(numItems); 

     // To keep the request smaller, request only the display name property. 
     view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ContactSchema.DisplayName); 

     // Retrieve the items in the Contacts folder that have the properties that you selected. 
     FindItemsResults<Item> contactItems = service.FindItems(WellKnownFolderName.Contacts, view); 

     // Display the list of contacts. 
     foreach (Item item in contactItems) 
     { 
      if (item is Contact) 
      { 
       Contact contact1 = item as Contact; 
       Console.WriteLine(contact1.DisplayName); 
      } 
     } 

     Console.ReadLine(); 
    } // end of Main() method 
    /*===========================================================================================================*/ 

    private static bool CertificateValidationCallBack(
    object sender, 
    System.Security.Cryptography.X509Certificates.X509Certificate certificate, 
    System.Security.Cryptography.X509Certificates.X509Chain chain, 
    System.Net.Security.SslPolicyErrors sslPolicyErrors) 
    { 
     // If the certificate is a valid, signed certificate, return true. 
     if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None) 
     { 
      return true; 
     } 

     // If there are errors in the certificate chain, look at each error to determine the cause. 
     if ((sslPolicyErrors & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) != 0) 
     { 
      if (chain != null && chain.ChainStatus != null) 
      { 
       foreach (System.Security.Cryptography.X509Certificates.X509ChainStatus status in chain.ChainStatus) 
       { 
        if ((certificate.Subject == certificate.Issuer) && 
         (status.Status == System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.UntrustedRoot)) 
        { 
         // Self-signed certificates with an untrusted root are valid. 
         continue; 
        } 
        else 
        { 
         if (status.Status != System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.NoError) 
         { 
          // If there are any other errors in the certificate chain, the certificate is invalid, 
          // so the method returns false. 
          return false; 
         } 
        } 
       } 
      } 

      // When processing reaches this line, the only errors in the certificate chain are 
      // untrusted root errors for self-signed certificates. These certificates are valid 
      // for default Exchange server installations, so return true. 
      return true; 
     } 
     else 
     { 
      // In all other cases, return false. 
      return false; 
     } 
    } 

    private static bool RedirectionUrlValidationCallback(string redirectionUrl) 
    { 
     // The default for the validation callback is to reject the URL. 
     bool result = false; 

     Uri redirectionUri = new Uri(redirectionUrl); 

     // Validate the contents of the redirection URL. In this simple validation 
     // callback, the redirection URL is considered valid if it is using HTTPS 
     // to encrypt the authentication credentials. 
     if (redirectionUri.Scheme == "https") 
     { 
      result = true; 
     } 
     return result; 
    } 
} 

答えて

0

は思わ:

は、ここに私のコードです。

すべての連絡先を取得するには、SearchFilterを追加する必要があります。この場合、Exists()フィルタを実行することをお勧めします。

あなたの質問は、Exchangeの連絡先を取得する方法であれば

Dim oFilter As New SearchFilter.Exists(ItemSchema.Id) 
    Dim oResults As FindItemsResults(Of Item) = Nothing 
    Dim oContact As Contact 

     Dim blnMoreAvailable As Boolean = True 
     Dim intSearchOffset As Integer = 0 
     Dim oView As New ItemView(conMaxChangesReturned, intSearchOffset, OffsetBasePoint.Beginning) 
     oView.PropertySet = BasePropertySet.IdOnly 

     Do While blnMoreAvailable 
      oResults = pService.FindItems(WellKnownFolderName.Contacts, oFilter, oView) 
      blnMoreAvailable = oResults.MoreAvailable 
      If Not IsNothing(oResults) AndAlso oResults.Items.Count > 0 Then 
       For Each oExchangeItem As Item In oResults.Items 
        Dim oExchangeContactId As ItemId = oExchangeItem.Id 
        //do something else 
       Next 
       If blnMoreAvailable Then oView.Offset = oView.Offset + conMaxChangesReturned 
      End If 
     Loop 
+1

ハンガリー表記が有効です!私はそれを十年以上見ていない。 –

0
How to retrieve all contacts from Microsoft Exchange using EWS Managed API? 

私のgithubのページgithub.com/rojoboを参照してください連絡先を扱う全Windowsアプリケーションの例が必要な場合は、以下の私の例を参照してください。 EWSを使用して、あなたはすでにそれで終わっています。

Office365->人々はあなたのExchangeの連絡先を操作するための正しいアプリです。ピープルアプリのURLをチェックアウトした場合、これはhttps://outlook.office365.com/owa/?realm=xxxxxx#exsvurl=1&ll-cc=1033&modurl=2と似ていますが、owaはOutlook Web Appの同義語です。

+0

こんにちはJackie、あなたの答えのためのthx。 https://outlook.office365.com/owa/?realm=xxxxxx&exsvurl=1&ll-cc=1033&modurl=2&path=/people –

+0

からすべての連絡先を具体的には** Office365-> People->アドレス帳** から取得する必要があります

+0

はい、あなたがすでに持っているコードは、それを行う方法です。 – Jackie

関連する問題