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)
                Dim contact As Contact = New Contact(service)
                contact.GivenName = "Brian"
                contact.MiddleName = "David"
                contact.Surname = "Johnson"
                contact.FileAsMapping = FileAsMapping.SurnameCommaGivenName
                MsgBox("Contact created!!!")
            Catch ex As Exception
            End Try
        End Function

    Saturday, June 15, 2013 12:45 PM


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

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


    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.


    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)
                For Each folder As Folder In findFolderResults
                    If folderName.Equals(folder.DisplayName, StringComparison.InvariantCultureIgnoreCase) Then
                        Return folder
                    End If
            Catch ex As Exception
            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:


    Sunday, June 30, 2013 4:26 AM