none
Save contacts in specific folder RRS feed

  • Question

  • Hi

    I have couple of contact folder and I want to save contact in specific folder. I am using EWS Managed API to save contacts.

    Based on documentation, it has option to save data in specific folder but it is not working. 


    Satyam MCITP, MCPD

    Friday, May 20, 2016 6:13 PM

Answers

  • OK Satyam,

    you were almost there ;-) You need to bind to a folder AND specify its ID as argument to Save() in order to make this work. My code is PowerShell but I am sure you can easily translate it back to C#:

    $targetFld = 'CustomContacts' ## this is expected to be a subfolder of Contacts; otherwise, bind to Root and use DeepTraversal
    $mail = 'my@mail.de'
    
    
    $ex_ews_path = “C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll”
    Add-Type -Path $ex_ews_path
    $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013
    $Service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
    $Service.AutodiscoverUrl($mail,{$true})
    $Service.Url.OriginalString
    
    
    $RootID = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Contacts,$mail) 
    $RootFld = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$RootID)  
    
    $FolderView = new-object Microsoft.Exchange.WebServices.Data.FolderView(1) 
    $SearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,$targetFld) 
    $findFolderResults = $service.FindFolders($RootID,$SearchFilter,$FolderView) 
    if ($findFolderResults.TotalCount -gt 0){ 
        foreach($folder in $findFolderResults.Folders){ 
            $tfTargetFolder = $folder                
            $folder
        } 
    } else { 
        "Error Folder Not Found"  
        $tfTargetFolder = $null  
    }       
    if ($tfTargetFolder) {
        $tFld = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$tfTargetFolder.Id)
        $iContact = New-Object Microsoft.Exchange.WebServices.Data.Contact($Service)
        # personal
        $iContact.GivenName = 'Maximilian'
        $iContact.Surname = 'von Bayern'
        $iContact.NickName = 'Mad Max'
        
        # business
        if ($c_company) {
            $iContact.CompanyName = 'Bayrischer Koenigshof'
        }
        $iContact.JobTitle = 'Koenig'
        
        # communication
        $iContact.EmailAddresses[[Microsoft.Exchange.WebServices.Data.EmailAddressKey]::EmailAddress1] = 'madmax@bayern.by'
        
        $iContact.Save($tfTargetFolder.Id)
    }


    Evgenij Smirnov

    msg services ag, Berlin -> http://www.msg-services.de
    my personal blog (mostly German) -> http://it-pro-berlin.de
    Windows Server User Group, Berlin -> http://www.winsvr-berlin.de
    Mark Minasi Technical Forum, reloaded -> http://newforum.minasi.com

    In theory, there is no difference between theory and practice. In practice, there is.

    • Marked as answer by satyam kumar Monday, May 23, 2016 3:40 PM
    Saturday, May 21, 2016 8:27 PM

All replies

  • Hi,

    it actually works quite well. You need to bind to the target folder, then all your saves will go in there.


    Evgenij Smirnov

    msg services ag, Berlin -> http://www.msg-services.de
    my personal blog (mostly German) -> http://it-pro-berlin.de
    Windows Server User Group, Berlin -> http://www.winsvr-berlin.de
    Mark Minasi Technical Forum, reloaded -> http://newforum.minasi.com

    In theory, there is no difference between theory and practice. In practice, there is.

    Friday, May 20, 2016 10:12 PM
  • do you have code snippets?

    Satyam MCITP, MCPD

    Friday, May 20, 2016 10:13 PM
  • Yes, but not right here, won't be getting on that site until Tuesday. But you can post your code and we can look over that.


    Evgenij Smirnov

    msg services ag, Berlin -> http://www.msg-services.de
    my personal blog (mostly German) -> http://it-pro-berlin.de
    Windows Server User Group, Berlin -> http://www.winsvr-berlin.de
    Mark Minasi Technical Forum, reloaded -> http://newforum.minasi.com

    In theory, there is no difference between theory and practice. In practice, there is.

    Friday, May 20, 2016 10:19 PM
  •  FolderId contactId = new FolderId("Dashboard Contacts");


    Contact newContact = new Contact(ex);
    newContact.GivenName = "John";
    newContact.Surname = "Smith";
    newContact.FileAsMapping = FileAsMapping.GivenNameSpaceSurname;
    newContact.CompanyName = "Smith & Smith Inc.";
    newContact.JobTitle = "CEO";
    newContact.AssistantName = "Pocahontas";
    newContact.Body = "Captain John Smith (c. January 1580 – 21 June 1631) Admiral ";

    newContact.Save(contactId );

    Also tried

    FolderView view = new FolderView(1);
     view.PropertySet = new PropertySet(BasePropertySet.IdOnly);
    view.PropertySet.Add(FolderSchema.DisplayName);
     view.PropertySet.Add(FolderSchema.Id);
    view.Traversal = FolderTraversal.Deep;
     FindFoldersResults findFolderResults = ex.FindFolders(WellKnownFolderName.Contacts, view);

    foreach (Microsoft.Exchange.WebServices.Data.Folder f in findFolderResults)
                    {
                     
                        newContact.Save(f.Id);
                        
                       
                    }


    Satyam MCITP, MCPD

    Friday, May 20, 2016 10:24 PM
  • OK Satyam,

    you were almost there ;-) You need to bind to a folder AND specify its ID as argument to Save() in order to make this work. My code is PowerShell but I am sure you can easily translate it back to C#:

    $targetFld = 'CustomContacts' ## this is expected to be a subfolder of Contacts; otherwise, bind to Root and use DeepTraversal
    $mail = 'my@mail.de'
    
    
    $ex_ews_path = “C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll”
    Add-Type -Path $ex_ews_path
    $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013
    $Service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
    $Service.AutodiscoverUrl($mail,{$true})
    $Service.Url.OriginalString
    
    
    $RootID = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Contacts,$mail) 
    $RootFld = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$RootID)  
    
    $FolderView = new-object Microsoft.Exchange.WebServices.Data.FolderView(1) 
    $SearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,$targetFld) 
    $findFolderResults = $service.FindFolders($RootID,$SearchFilter,$FolderView) 
    if ($findFolderResults.TotalCount -gt 0){ 
        foreach($folder in $findFolderResults.Folders){ 
            $tfTargetFolder = $folder                
            $folder
        } 
    } else { 
        "Error Folder Not Found"  
        $tfTargetFolder = $null  
    }       
    if ($tfTargetFolder) {
        $tFld = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$tfTargetFolder.Id)
        $iContact = New-Object Microsoft.Exchange.WebServices.Data.Contact($Service)
        # personal
        $iContact.GivenName = 'Maximilian'
        $iContact.Surname = 'von Bayern'
        $iContact.NickName = 'Mad Max'
        
        # business
        if ($c_company) {
            $iContact.CompanyName = 'Bayrischer Koenigshof'
        }
        $iContact.JobTitle = 'Koenig'
        
        # communication
        $iContact.EmailAddresses[[Microsoft.Exchange.WebServices.Data.EmailAddressKey]::EmailAddress1] = 'madmax@bayern.by'
        
        $iContact.Save($tfTargetFolder.Id)
    }


    Evgenij Smirnov

    msg services ag, Berlin -> http://www.msg-services.de
    my personal blog (mostly German) -> http://it-pro-berlin.de
    Windows Server User Group, Berlin -> http://www.winsvr-berlin.de
    Mark Minasi Technical Forum, reloaded -> http://newforum.minasi.com

    In theory, there is no difference between theory and practice. In practice, there is.

    • Marked as answer by satyam kumar Monday, May 23, 2016 3:40 PM
    Saturday, May 21, 2016 8:27 PM
  • Thanks Evgenij.

    I am working on this and let you know by EOD.


    Satyam MCITP, MCPD

    Monday, May 23, 2016 1:39 PM
  • Thanks. I am able to save contact in specific folder.

    Satyam MCITP, MCPD

    Monday, May 23, 2016 3:41 PM