none
Exchange ews managed api create item in public folder RRS feed

  • Question

  • I have written the following VB function to create a contact using the Exchange ews managed API.  It creates a contact in teh default contacts folder.  However I need to know how to modify it to create a contact in a public folder.  The documentation seems to be poor on this topic.  Feel free to reply if you have a solution in VB or C#.  Many thanks.

    Function create_contact()
            ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateCertificate)
            Dim service As New ExchangeService(requestedServerVersion:=ExchangeVersion.Exchange2007_SP1)
            'Add a valid EWS service end point here or user Autodiscover
    
            service.Url = New Uri("https://server/ews/exchange.asmx")
    
            'Add a valid user credentials
    
            service.Credentials = New WebCredentials("username", "password", "domain")
    
            'To address the SSL challenge
    
            ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateCertificate)
    
            Try
    
    
                Dim contact As Contact = New Contact(service)
    
                contact.GivenName = "Brian"
                contact.MiddleName = "David"
                contact.Surname = "Johnson"
                contact.FileAsMapping = FileAsMapping.SurnameCommaGivenName
                contact.Save()
    
    
    
                MsgBox("Contact created!!!")
    
            Catch ex As Exception
    
                MsgBox(ex.Message)
    
            End Try
    
    
    
        End Function



    Saturday, June 15, 2013 12:45 PM

Answers

All replies

  • To use a Public folder you first need to know the EWS FolderId so you will need to use something like this to search for it http://stackoverflow.com/questions/14561124/searching-of-folders-in-public-folders-by-giving-its-path-name.

    Once you have the Folder object you can use use the Save Overload that allows you to specifiy the FolderId eg

    contact.Save(Folder.Id)

    The only thing to be careful of is that the account your using to create the Contact and find the Folder has client permissions to the folder in question.

    Cheers
    Glen

    Monday, June 17, 2013 7:02 AM
  • Thanks Glen,

    For anyone who is interested in teh VB code.  I created the following function to retrieve the folder object.

        Private Function GetTopLevelFolder(ByVal service As ExchangeService, ByVal folderName As [String]) As Folder
            Dim folderView As New FolderView(Integer.MaxValue)
            Dim findFolderResults As FindFoldersResults = service.FindFolders(WellKnownFolderName.PublicFoldersRoot, folderView)
    
            Try
    
                For Each folder As Folder In findFolderResults
    
                    If folderName.Equals(folder.DisplayName, StringComparison.InvariantCultureIgnoreCase) Then
    
                        Return folder
    
                    End If
                Next
    
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Function

    Call it with the following code:

            Dim foldername As String
            foldername = "Insert folder name here"
            Dim folder As Folder
            folder = GetTopLevelFolder(service, foldername)

    As Glen pointed out:  Save the contact with:

    contact.Save(Folder.Id)


    Sunday, June 30, 2013 4:26 AM