Power Automate - The Query String \"loginName\" Is Missing Or Invalid

In Power Automate, developers sometimes work on a requirement where they have to add SharePoint site users to SharePoint list items as unique user permissions. For this we have to get principal Id of site user with the help of HTTP POST – SharePoint RESP API request. The SharePoint REST API sample request looks like this:

https://contoso.sharepoint.com/sites/testsite/_api/web/siteusers(@v)?@v='i:0#.f|membership|[email protected]'

where @v is user claim and it has generally such format: “i:0#.f|membership|[email protected]

If user claim value is not encoded in querystring parameter, then we would get error like below while running Power Automate HTTP action.

{
    "odata.error": {
        "code": "-1, Microsoft.SharePoint.Client.InvalidClientQueryException",
        "message": {
            "lang": "en-US",
            "value": "The query string \"loginName\" is missing or invalid."
        }
    }
}

The simple solution to this problem is we need to use encodeURIComponent() function (in expressions) for user claim value and problem will be solved. EncodeURIComponent is being used to protect the URLs by encoding them.

Now REST API Url would like this,

https://contoso.sharepoint.com/sites/testsite/_api/web/siteusers(@v)?@v=’encodeURIComponent ('i:0#.f|membership|[email protected]')’

Next time when you open the Power Automate and check HTTP action, the encodeURIComponent() will not be visible in REST API URL but don’t worry, now the encoding is applied to user claim value.

Happy Learning, Anywhere! J