Why SharePoint remote event receiver calls Azure Function six times?

132 Views Asked by At

I have a list in SharePoint that has a synchronous event receiver attached to it that responds to the change event of the list item (ItemUpdating). There is also an Azure Function that the event receiver calls. The function validates the fields. The question is that if the field is validated, then the function is run once, but if the validation is not passed, then the event receiver runs the function six times.

using namespace System.Net;

param($Request, $TriggerMetadata);
Write-Output "Function started.";

$xmlDocument=[xml]$Request.Body;
$ListItemTitle = $xmlDocument.Envelope.Body.ProcessEvent.properties.ItemEventProperties.AfterProperties.KeyValueOfstringanyType[1].Value.InnerText;
$listName = $xmlDocument.Envelope.Body.ProcessEvent.properties.ItemEventProperties.ListTitle;

Write-Output $ListItemTitle;
Write-Output $listName;

$responseBody = @'
<s:Envelope
    xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <ProcessEventResponse
            xmlns="http://schemas.microsoft.com/sharepoint/remoteapp/">
            <ProcessEventResult
                xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                <ChangedItemProperties
                    xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
                <ErrorMessage>Validation Error!</ErrorMessage>
                <RedirectUrl i:nil="true"/>
                <Status>CancelWithError</Status>
            </ProcessEventResult>
        </ProcessEventResponse>
    </s:Body>
</s:Envelope>
'@;

if($ListItemTitle -eq "BadTitle"){
    Write-Output "Validation error!";
    Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
        Body = $responseBody;
        ContentType = "text/xml";
        StatusCode = [HttpStatusCode]::OK;
    });
}
else{
    Write-Output "Validation passed.";
    Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
        StatusCode = [HttpStatusCode]::OK;
    });
}

How can I make the function run once when validation fails?

0

There are 0 best solutions below