Sample code for 30+ languages & platforms
DataFlex

OneDrive -- Search for Files

See more OneDrive Examples

Search for files within a hierarchy of items for items matching a query. You can search within a folder hierarhcy or a whole drive.

Note: This example requires Chilkat v9.5.0.97 or greater.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoJson
    Handle hoHttp
    Variant vSbResults
    Handle hoSbResults
    Handle hoLastMod
    Handle hoPhotoTaken
    Integer i
    Integer iNumItems
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

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

    // Use your client ID, client secret, and tenant ID in the following lines
    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComUpdateString Of hoJson "client_id" "2871da2c-8176-4b7f-869b-2311aa82e743" To iSuccess
    Get ComUpdateString Of hoJson "client_secret" "2hu9Q~r5QuryUcEkNbg1btLtnfU1VUXzhSCG6brH" To iSuccess
    Get ComUpdateString Of hoJson "scope" "https://graph.microsoft.com/.default" To iSuccess
    Get ComUpdateString Of hoJson "token_endpoint" "https://login.microsoftonline.com/114d7ed6-71bf-4dbe-a866-748364121bf2/oauth2/v2.0/token" To iSuccess

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End
    Get ComEmit Of hoJson To sTemp1
    Set ComAuthToken Of hoHttp To sTemp1

    // Sends the following GET request:

    // Search the entire drive like this:
    // If not using "me", then the path should be /v1.0/users/{id | userPrincipalName}/...
    // GET /v1.0/users/{user-id}/drive/root/search(q='{search-text}')

    // Or search under a particular folder like this:
    // GET /v1.0/users/{user-id}/drive/root:/{item-path}:/search(q='{search-text}')

    // This example will search for files in or under the "/Misc" directory, having "penguin" in the file path.
    Get ComSetUrlVar Of hoHttp "item_path" "/Misc" To iSuccess
    Get ComSetUrlVar Of hoHttp "search_text" "penguins" To iSuccess
    Get ComSetUrlVar Of hoHttp "user_id" "4fe732c3-322e-4a6b-b729-2fd1eb5c6104" To iSuccess

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResults
    If (Not(IsComObjectCreated(hoSbResults))) Begin
        Send CreateComObject of hoSbResults
    End
    Get pvComObject of hoSbResults to vSbResults
    Get ComQuickGetSb Of hoHttp "https://graph.microsoft.com/v1.0/users/{$user_id}/drive/root:/{$item_path}:/search(q='{$search_text}')" vSbResults To iSuccess
    Get ComLastMethodSuccess Of hoHttp To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // The response should be JSON.
    Set ComEmitCompact Of hoJson To False
    Get pvComObject of hoSbResults to vSbResults
    Get ComLoadSb Of hoJson vSbResults To iSuccess

    // A successful response should return a status code of 200.
    Get ComLastStatus Of hoHttp To iTemp1
    If (iTemp1 <> 200) Begin
        Get ComEmit Of hoJson To sTemp1
        Showln sTemp1
        Get ComLastStatus Of hoHttp To iTemp1
        Showln "Response status = " iTemp1
        Procedure_Return
    End

    Get ComEmit Of hoJson To sTemp1
    Showln sTemp1
    Showln "--"

    // Iterate over the results in the JSON response:
    Get Create (RefClass(cComCkDateTime)) To hoLastMod
    If (Not(IsComObjectCreated(hoLastMod))) Begin
        Send CreateComObject of hoLastMod
    End
    Get Create (RefClass(cComCkDateTime)) To hoPhotoTaken
    If (Not(IsComObjectCreated(hoPhotoTaken))) Begin
        Send CreateComObject of hoPhotoTaken
    End
    Move 0 To i
    Get ComSizeOfArray Of hoJson "value" To iNumItems
    While (i < iNumItems)
        Set ComI Of hoJson To i
        Showln "-- DriveItem " (i + 1)
        Get ComStringOf Of hoJson "value[i].id" To sTemp1
        Showln "id: " sTemp1
        Get ComStringOf Of hoJson "value[i].name" To sTemp1
        Showln "name: " sTemp1
        Get ComIntOf Of hoJson "value[i].size" To iTemp1
        Showln "size: " iTemp1

        // Get the lastModifiedDateTime
        Get ComStringOf Of hoJson "value[i].fileSystemInfo.lastModifiedDateTime" To sTemp1
        Get ComSetFromTimestamp Of hoLastMod sTemp1 To iSuccess

        // Is this a folder?
        Get ComHasMember Of hoJson "value[i].folder" To bTemp1
        If (bTemp1 = True) Begin
            Get ComIntOf Of hoJson "value[i].folder.childCount" To iTemp1
            Showln "This is a folder with " iTemp1 " children"
        End

        Get ComHasMember Of hoJson "value[i].file" To bTemp1
        If (bTemp1 = True) Begin
            Showln "This is a file."
            Get ComStringOf Of hoJson "value[i].file.hashes.sha1Hash" To sTemp1
            Showln "SHA1 hash: " sTemp1
            Get ComStringOf Of hoJson "value[i].mimeType" To sTemp1
            Showln "mimeType: " sTemp1
        End

        Get ComHasMember Of hoJson "value[i].image" To bTemp1
        If (bTemp1 = True) Begin
            Showln "This is an image."
            Get ComIntOf Of hoJson "value[i].image.height" To iTemp1
            Showln "height: " iTemp1
            Get ComIntOf Of hoJson "value[i].image.width" To iTemp1
            Showln "width: " iTemp1
        End

        Get ComHasMember Of hoJson "value[i].photo" To bTemp1
        If (bTemp1 = True) Begin
            Showln "This is a photo."
            Get ComStringOf Of hoJson "value[i].photo.takenDateTime" To sTemp1
            Get ComSetFromTimestamp Of hoPhotoTaken sTemp1 To iSuccess
            Get ComGetAsRfc822 Of hoPhotoTaken True To sTemp1
            Showln "photo taken on " sTemp1
        End

        Get ComHasMember Of hoJson "value[i].audio" To bTemp1
        If (bTemp1 = True) Begin
            Showln "This is an audio file."
            Get ComIntOf Of hoJson "value[i].audio.duration" To iTemp1
            Showln "duration: " iTemp1
        End

        Move (i + 1) To i
    Loop

    // Sample JSON OneDrive search response:

    // 	{
    // 	  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#Collection(driveItem)",
    // 	  "value": [
    // 	    {
    // 	      "@odata.type": "#microsoft.graph.driveItem",
    // 	      "@microsoft.graph.downloadUrl": "https://public.dm2301.livefilestore.com/y4m01N6dLIV7ditztcRwWDx2Jzf1qiwJNASFiHfajfZUuJnEMgHmIZTASy3lNzwUFHQgET0I11hzqCyb0A6I5jhI9FgFAJlBJHFrToT7QXj2xvCV46LOZVJASk79LEWRRM97aOhdOHhKeIl_HxoIWSl08sX3kGyC50RP72rDE7ERSVdZAa3CmMNkp6tRtfKI5wfJNqSx3NeqLQ6LxwBBY4KgniPQOgEna0G_MKTGDt10T4",
    // 	      "createdBy": {
    // 	        "user": {
    // 	          "displayName": "Matt Smith",
    // 	          "id": "3a33fceb9b74cc15"
    // 	        }
    // 	      },
    // 	      "createdDateTime": "2017-06-04T20:40:22.48Z",
    // 	      "cTag": "aYzozQTMzRkNFQjlCNzRDQzE1ITQ4NzIuMjU3",
    // 	      "eTag": "aM0EzM0ZDRUI5Qjc0Q0MxNSE0ODcyLjI",
    // 	      "id": "3A33FCEB9B74CC15!4872",
    // 	      "lastModifiedBy": {
    // 	        "user": {
    // 	          "displayName": "Matt Smith",
    // 	          "id": "3a33fceb9b74cc15"
    // 	        }
    // 	      },
    // 	      "lastModifiedDateTime": "2017-06-04T20:40:23.463Z",
    // 	      "name": "penguins.jpg",
    // 	      "parentReference": {
    // 	        "driveId": "3a33fceb9b74cc15",
    // 	        "id": "3A33FCEB9B74CC15!4871",
    // 	        "name": "wildlife",
    // 	        "path": "/drive/root:/Misc/wildlife"
    // 	      },
    // 	      "size": 777835,
    // 	      "webUrl": "https://1drv.ms/i/s!ABXMdJvr_DM6pgg",
    // 	      "file": {
    // 	        "hashes": {
    // 	          "sha1Hash": "DF7BE9DC4F467187783ACA68C7CE98E4DF2172D0"
    // 	        },
    // 	        "mimeType": "image/jpeg"
    // 	      },
    // 	      "fileSystemInfo": {
    // 	        "createdDateTime": "2017-06-04T20:40:22.48Z",
    // 	        "lastModifiedDateTime": "2009-07-14T05:32:31.674Z"
    // 	      },
    // 	      "image": {
    // 	        "height": 768,
    // 	        "width": 1024
    // 	      },
    // 	      "photo": {
    // 	        "takenDateTime": "2008-02-18T05:07:31Z"
    // 	      },
    // 	      "searchResult": {
    // 	        "onClickTelemetryUrl": "https://www.bing.com/personalsearchclick?IG=D10F15177AE54D49A97F361270D53290&CID=3A33FCEB9B74CC150000000000000000&ID=DevEx%2c5005&q=penguins&resid=3A33FCEB9B74CC15%214872"
    // 	      }
    // 	    }
    // 	  ]
    // 	}


End_Procedure