Search code examples
powershellmicrosoft-graph-apiexchangewebservicesmicrosoft-graph-sdks

Convert EWS StoreId (FolderID) to Graph API restId using translateExchangeIds?


Is there any possible way to convert Get-MailboxFolderStatistics returned folderId (storeId) to Microsoft Graph API restId?

I've tried to use users/{userId}/translateExchangeIds endpoint but didn't get any useful results.

enter image description here

Does users/{userId}/translateExchangeIds endpoint support translating storeId to restId?

Is there any other way to translate this id's ?


Solution

  • You need to convert storeId to entryId by yourself and then use users/{userId}/translateExchangeIds to convert entryId to restId.

    For converting storeId to entryId convert folderId from base64 string to hex string. Then parse entryId hex string and encode entryId hex string to entryId.

    $folderId = "LgAAAADMaqNx7y87SYAkFzRBDsNkAQDBGxV/99wUQoEROcv7tgfjAAAWrAyWAAAB"
    
    # convert from base64 to bytes
    $folderIdBytes = [Convert]::FromBase64String($folderId)
    
    # convert byte array to string, remove '-' and ignore first byte
    $folderIdHexString = [System.BitConverter]::ToString($folderIdBytes).Replace('-','')
    $folderIdHexStringLength = $folderIdHexString.Length
    
    # get hex entry id string by removing first and last byte
    $entryIdHexString = $folderIdHexString.SubString(2,($folderIdHexStringLength-4))
    
    # convert to byte array - two chars represents one byte
    $entryIdBytes = [byte[]]::new($entryIdHexString.Length / 2)
    
    For($i=0; $i -lt $entryIdHexString.Length; $i+=2){
        $entryIdTwoChars = $entryIdHexString.Substring($i, 2)
        $entryIdBytes[$i/2] = [convert]::ToByte($entryIdTwoChars, 16)
    }
    
    # convert bytes to base64 string
    $entryIdBase64 = [Convert]::ToBase64String($entryIdBytes)
    
    # count how many '=' contains base64 entry id
    $equalCharCount = $entryIdBase64.Length - $entryIdBase64.Replace('=','').Length
    
    # trim '=', replace '/' with '-', replace '+' with '_' and add number of '=' at the end
    $entryId = $entryIdBase64.TrimEnd('=').Replace('/','_').Replace('+','-')+$equalCharCount
    

    folderId with value LgAAAADMaqNx7y87SYAkFzRBDsNkAQDBGxV/99wUQoEROcv7tgfjAAAWrAyWAAAB is converted to entryId AAAAAMxqo3HvLztJgCQXNEEOw2QBAMEbFX_33BRCgRE5y_u2B-MAABasDJYAAA2

    Then use the converted entryId in the call

    {
      "inputIds" : [
        "AAAAAMxqo3HvLztJgCQXNEEOw2QBAMEbFX_33BRCgRE5y_u2B-MAABasDJYAAA2"
      ],
      "sourceIdType": "entryId",
      "targetIdType": "restId"
    }
    

    Response

    {
        "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#Collection(microsoft.graph.convertIdResult)",
        "value": [
            {
                "sourceId": "AAAAAMxqo3HvLztJgCQXNEEOw2QBAMEbFX_33BRCgRE5y_u2B-MAABasDJYAAA2",
                "targetId": "AAMkAGRkZTFiMDQxLWYzNDgtNGQ3ZS05Y2U3LWU1NWJhMTM5YTgwMAAuAAAAAADMaqNx7y87SYAkFzRBDsNkAQDBGxV-99wUQoEROcv7tgfjAAAWrAyWAAA="
            }
        ]
    }
    

    Resources:

    URL-safe