by

Accessing a Person’s Shared Contacts Folders in EWS

Accessing a Person’s Shared Contacts Folders in EWS Some time again I posted a pattern of making a Shortcut to a Shared Calendar folder utilizing EWS right here . On this put up I am going to have a look at how you are able to do the inverse which is studying and accessing the folders related to these shortcuts. A customers Shared Contacts folder’s will appear as if the next in Outlook

These Shortcuts are particular objects which can be saved within the Widespread Views folder within the Non_IPM_Subtree of a mailbox. The information for the shortcut merchandise is saved in quite a few properties that are documented within the following Change protocol doc http://msdn.microsoft.com/en-us/library/ee157359(v=exchg.80).aspx

So to entry these shortcuts from EWS requires just a few totally different operations, the primary is it’s good to use a FindFolder operation on the Root of the Mailbox to situated the CommonViews Folder. Upon getting the CommonViews FolderId you then use the FindItems operation to search out any of the Objects the place the PidTagWlinkGroupName is about to Shared Contacts, which is able to successfully filter the objects returned simply to the shared contacts (word at this level if you’re utilizing a localized model of Outlook it’s good to use the localized textual content title for the node).

Upon getting entry to the ShortCut Objects the following step is to learn the PidTagWlinkStoreEntryId property from the ShortCut merchandise. To get the mailbox that this shortcut refers to you’ll be able to extract the X500 tackle from StoreId format which is documented right here . You possibly can resolve the X500 Tackle to an SMTP tackle utilizing the resolveName operation after which use the SMTP tackle bind to the shared Contacts folder usually in EWS.

I’ve posted a pattern powershell script to entry and question all of the Shared Contact folders in a Mailbox right here the code itself seems like

  1. ## Get the Mailbox to Entry from the 1st commandline argument  
  2.   
  3. $MailboxName = $args[0]  
  4.   
  5. ## Load Managed API dll    
  6. Add-Kind -Path “C:Program FilesMicrosoftExchangeWeb Services2.0Microsoft.Exchange.WebServices.dll”    
  7.     
  8. ## Set Change Model    
  9. $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Change2010_SP2    
  10.     
  11. ## Create Change Service Object    
  12. $service = New-Object Microsoft.Change.WebServices.Information.ExchangeService($ExchangeVersion)    
  13.     
  14. ## Set Credentials to use two choices are availible Option1 to use explict credentials or Possibility 2 use the Default (logged On) credentials    
  15.     
  16. #Credentials Possibility 1 utilizing UPN for the home windows Account    
  17. $psCred = Get-Credential    
  18. $creds = New-Object System.Internet.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())    
  19. $service.Credentials = $creds        
  20.     
  21. #Credentials Possibility 2    
  22. #service.UseDefaultCredentials = $true    
  23.     
  24. ## Select to ignore any SSL Warning points precipitated by Self Signed Certificates    
  25.     
  26. ## Code From http://poshcode.org/624  
  27. ## Create a compilation setting  
  28. $Supplier=New-Object Microsoft.CSharp.CSharpCodeProvider  
  29. $Compiler=$Supplier.CreateCompiler()  
  30. $Params=New-Object System.CodeDom.Compiler.CompilerParameters  
  31. $Params.GenerateExecutable=$False  
  32. $Params.GenerateInMemory=$True  
  33. $Params.IncludeDebugInformation=$False  
  34. $Params.ReferencedAssemblies.Add(“System.DLL”) | Out-Null  
  35.   
  36. $TASource=@ 
  37.   namespace Native.ToolkitExtensions.Internet.CertificatePolicy{ 
  38.     public class TrustAll : System.Internet.ICertificatePolicy { 
  39.       public TrustAll() {  
  40.       } 
  41.       public bool CheckValidationResult(System.Internet.ServicePoint sp, 
  42.         System.Safety.Cryptography.X509Certificates.X509Certificates cert,  
  43.         System.Internet.WebRequest req, int downside) { 
  44.         return true; 
  45.       } 
  46.     } 
  47.   } 
  48. @   
  49. $TAResults=$Supplier.CompileAssemblyFromSource($Params,$TASource)  
  50. $TAAssembly=$TAResults.CompiledAssembly  
  51.   
  52. ## We now create an occasion of the TrustAll and connect it to the ServicePointManager  
  53. $TrustAll=$TAAssembly.CreateInstance(“Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll”)  
  54. [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll  
  55.   
  56. ## finish code from http://poshcode.org/624  
  57.     
  58. ## Set the URL of the CAS (Consumer Entry Server) to use two choices are availbe to use Autodiscover to discover the CAS URL or Hardcode the CAS to use    
  59.     
  60. #CAS URL Possibility 1 Autodiscover    
  61. $service.AutodiscoverUrl($MailboxName,{$true})    
  62. “Using CAS Server : “ + $Service.url     
  63.      
  64. #CAS URL Possibility 2 Hardcoded    
  65.     
  66. #$uri=[system.URI] “https://casservername/ews/exchange.asmx”    
  67. #$service.Url = $uri      
  68.     
  69. ## Elective part for Change Impersonation    
  70.   
  71. #PropDefs   
  72. $pidTagStoreEntryId = new-object Microsoft.Change.WebServices.Information.ExtendedPropertyDefinition(4091, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)  
  73. $PidTagNormalizedSubject = new-object Microsoft.Change.WebServices.Information.ExtendedPropertyDefinition(0x0E1D,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String);   
  74. $PidTagWlinkType = new-object Microsoft.Change.WebServices.Information.ExtendedPropertyDefinition(0x6849, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)  
  75. $PidTagWlinkFlags = new-object Microsoft.Change.WebServices.Information.ExtendedPropertyDefinition(0x684A, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)  
  76. $PidTagWlinkOrdinal = new-object Microsoft.Change.WebServices.Information.ExtendedPropertyDefinition(0x684B, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)  
  77. $PidTagWlinkFolderType = new-object Microsoft.Change.WebServices.Information.ExtendedPropertyDefinition(0x684F, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)  
  78. $PidTagWlinkSection = new-object Microsoft.Change.WebServices.Information.ExtendedPropertyDefinition(0x6852, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)  
  79. $PidTagWlinkGroupHeaderID = new-object Microsoft.Change.WebServices.Information.ExtendedPropertyDefinition(0x6842, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)  
  80. $PidTagWlinkSaveStamp = new-object Microsoft.Change.WebServices.Information.ExtendedPropertyDefinition(0x6847, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)  
  81. $PidTagWlinkGroupName = new-object Microsoft.Change.WebServices.Information.ExtendedPropertyDefinition(0x6851, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String)  
  82. $PidTagWlinkStoreEntryId = new-object Microsoft.Change.WebServices.Information.ExtendedPropertyDefinition(0x684E, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)  
  83. $PidTagWlinkGroupClsid = new-object Microsoft.Change.WebServices.Information.ExtendedPropertyDefinition(0x6850, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)  
  84. $PidTagWlinkEntryId = new-object Microsoft.Change.WebServices.Information.ExtendedPropertyDefinition(0x684C, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)  
  85. $PidTagWlinkRecordKey = new-object Microsoft.Change.WebServices.Information.ExtendedPropertyDefinition(0x684D, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)  
  86. $PidTagWlinkCalendarColor = new-object Microsoft.Change.WebServices.Information.ExtendedPropertyDefinition(0x6853, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)  
  87. $PidTagWlinkAddressBookEID = new-object Microsoft.Change.WebServices.Information.ExtendedPropertyDefinition(0x6854,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)  
  88. $PidTagWlinkROGroupType = new-object Microsoft.Change.WebServices.Information.ExtendedPropertyDefinition(0x6892,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer)  
  89. $PidTagWlinkAddressBookStoreEID = new-object Microsoft.Change.WebServices.Information.ExtendedPropertyDefinition(0x6891,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)  
  90.   
  91.   
  92. $SharedFolders = @{}    
  93.     
  94. #$service.ImpersonatedUserId = new-object Microsoft.Change.WebServices.Information.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)   
  95. Write-Host (“Getting CommonVeiwFolder”)  
  96. #Get CommonViewFolder  
  97. $folderid = new-object Microsoft.Change.WebServices.Information.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root,$MailboxName)     
  98. $tfTargetFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)    
  99. $fvFolderView = new-object Microsoft.Change.WebServices.Information.FolderView(1)   
  100. $SfSearchFilter = new-object Microsoft.Change.WebServices.Information.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,“Common Views”)   
  101. $findFolderResults = $service.FindFolders($tfTargetFolder.Id,$SfSearchFilter,$fvFolderView)   
  102. if ($findFolderResults.TotalCount -gt 0){   
  103.     $ExistingShortCut = $false  
  104.     $cvCommonViewsFolder = $findFolderResults.Folders[0]  
  105.     #Outline ItemView to retrive simply 1000 Objects      
  106.     #Discover Objects that are unread  
  107.     $psPropset= new-object Microsoft.Change.WebServices.Information.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)    
  108.     $psPropset.add($PidTagWlinkStoreEntryId)  
  109.     $psPropset.add($PidTagWlinkFolderType)  
  110.     $cntSearch = New-Object Microsoft.Change.WebServices.Information.SearchFilter+IsEqualTo($PidTagWlinkGroupName“Shared Contacts”);  
  111.     $ivItemView =  New-Object Microsoft.Change.WebServices.Information.ItemView(1000)     
  112.     $ivItemView.Traversal = [Microsoft.Exchange.WebServices.Data.ItemTraversal]::Related  
  113.     $ivItemView.PropertySet = $psPropset  
  114.     $fiItems = $service.FindItems($cvCommonViewsFolder.Id,$cntSearch,$ivItemView)      
  115.     foreach($Merchandise in $fiItems.Objects){  
  116.         $idVal = $null  
  117.         if($Merchandise.TryGetProperty($PidTagWlinkStoreEntryId,[ref]$idVal)){  
  118.             Write-Host(“Processing “ + $Merchandise.Topic)  
  119.                 $ssStoreID = $idVal;  
  120.                 $leLegDnStart = 0;  
  121.                 $lnLegDN = “”;  
  122.                 for ($ssArraynum=($ssStoreID.Size – 2);$ssArraynum -ne 0; $ssArraynum–)  
  123.                         {  
  124.                             if ($ssStoreID[$ssArraynum] -eq 0)  
  125.                             {  
  126.                                 $leLegDnStart = $ssArraynum;  
  127.                                 $lnLegDN = [System.Text.ASCIIEncoding]::ASCII.GetString($ssStoreID$leLegDnStart + 1, ($ssStoreID.Size – ($leLegDnStart + 2)));  
  128.                                 $ssArraynum = 1;  
  129.                             }  
  130.                         }  
  131.                         Write-Host($lnLegDN)  
  132.                         $ncCol = $service.ResolveName($lnLegDN, [Microsoft.Exchange.WebServices.Data.ResolveNameSearchLocation]::DirectoryOnly, $true);  
  133.                         if ($ncCol.Rely -gt 0)  
  134.                         {  
  135.                             strive  
  136.                             {  
  137.                                 $SharedContactsId = new-object Microsoft.Change.WebServices.Information.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Contacts, $ncCol[0].Mailbox.Tackle);  
  138.                                 $SharedContactFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service$SharedContactsId);  
  139.                                 $SharedFolders.Add($ncCol[0].Mailbox.Tackle, $SharedContactFolder);  
  140.                             }  
  141.                             catch  {  
  142.                                 Write-Host “Error getting Shared Folder”  
  143.                             }  
  144.                         }  
  145.               
  146.         }                               
  147.     }  
  148. }  
  149. if($SharedFolders.Keys.Rely -ne 0){  
  150.     foreach($mbFolder in $SharedFolders.Keys){  
  151.         #Outline ItemView to retrive simply 1000 Objects      
  152.         $ivItemView =  New-Object Microsoft.Change.WebServices.Information.ItemView(1000)      
  153.         $fiItems = $null      
  154.         do{      
  155.             $fiItems = $service.FindItems($SharedFolders[$mbFolder].Id,$ivItemView)      
  156.             #[Void]$service.LoadPropertiesForItems($fiItems,$psPropset)    
  157.             foreach($Merchandise in $fiItems.Objects){        
  158.                 Write-Host (“Mailbox : “ + $mbFolder)  
  159.                 Write-Host (“Contact : “ + $Merchandise.Topic + ” : “ + $Merchandise.EmailAddresses[[Microsoft.Exchange.WebServices.Data.EmailAddressKey]::EmailAddress1])  
  160.             }      
  161.             $ivItemView.Offset += $fiItems.Objects.Rely      
  162.         }whereas($fiItems.MoreAvailable -eq $true)   
  163.     }  
  164. }