Sample code for 30+ languages & platforms
Swift Requires Chilkat v11.0.0+

Outlook -- Create a Mail Folder

See more Outlook Examples

Creates a new mail folder as a child of an existing mail folder.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    let http = CkoHttp()!

    //  Use your previously obtained access token here:
    http.authToken = "MICROSOFT_GRAPH_ACCESS_TOKEN"

    //  This example will create a new mail folder as a child of /Inbox/abc

    //  Get the existing folder ID from the folder map created by this example 
    let htFolderMap = CkoHashtable()!
    let sbMap = CkoStringBuilder()!
    sbMap.loadFile(path: "qa_data/outlook/folderMap.xml", charset: "utf-8")
    htFolderMap.add(fromXmlSb: sbMap)

    var existingFolderId: String? = htFolderMap.lookupStr(key: "/Inbox/abc")
    if htFolderMap.lastMethodSuccess != true {
        print("Folder ID not found")
        return
    }

    //  Create a JSON request body with this content:
    //  
    //  	{
    //  	  "displayName": "displayName-value",
    //  	}

    //  This example will create /Inbox/abc/subFolderC
    let jsonRequestBody = CkoJsonObject()!
    jsonRequestBody.updateString(jsonPath: "displayName", value: "subFolderC")

    http.setUrlVar(name: "folder_id", value: existingFolderId)

    //  Create the folder "subFolderC" at the specified location.
    let resp = CkoHttpResponse()!
    success = http.httpJson(verb: "POST", url: "https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/childFolders", json: jsonRequestBody, contentType: "application/json", response: resp)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    //  A 201 response indicates success.
    if http.lastStatus.intValue == 201 {
        print("Folder created.")
    }
    else {
        print("Response status code = \(resp.statusCode.intValue)")
        print("Error: Folder not created.")
    }

    //  Show the response in both cases..
    let jsonResponse = CkoJsonObject()!
    jsonResponse.emitCompact = false
    jsonResponse.load(json: resp.bodyStr)
    print("\(jsonResponse.emit()!)")

    //  A sample successful JSON response looks like this:

    //  	{
    //  	  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('me')/mailFolders('AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huv8AAAA%3D')/childFolders/$entity",
    //  	  "id": "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAM6JqMIAAAA=",
    //  	  "displayName": "subFolderC",
    //  	  "parentFolderId": "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huv8AAAA=",
    //  	  "childFolderCount": 0,
    //  	  "unreadItemCount": 0,
    //  	  "totalItemCount": 0
    //  	}

}