New-MgSubscription fails when using a PowerShell based function app.
When you send a request to create a subscription to get change notifications through webhooks, the subscription service checks if the notificationUrl property in your subscription request is valid. My notificationUrl is a PowerShell based function app function.
The endpoint must respond with the following characteristics within 10 seconds of step 1:
- A status code of
HTTP 200 OK
. - A content type of
text/plain
. - A body that includes the URL decoded plain text validation token.
My function facilitates this using the following code.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
ContentType = "text/plain"
Body = $Request.Query.ValidationToken
})
Running the PowerShell command or using Postman fails. It's trying to validate the endpoint.
PowerShell:
$params = @{
changeType = "updated"
notificationUrl = "https://{function url}?validationToken=something"
resource = "sites/{site id}/lists/{list id}"
expirationDateTime = "2024-08-01T09:00:00Z"
clientState = "{something}"
latestSupportedTlsVersion = "v1_2"
}
New-MgSubscription -BodyParameter $params
Error Code in Postman:
{
"error": {
"code": "ValidationError",
"message": "something,Validation: Testing client application reachability for subscription Request-Id: {redacted}",
"innerError": {
"date": "2024-07-09T07:51:51",
"request-id": "{redacted}",
"client-request-id": "{redacted}"
}
}
}
It uses an app registration for authentication. The function app has other functions that perform work on SharePoint and works just find. It has at Sites.Read.All required permission.
I'm unable to extract more information on why this is failing.