Overcoming 502 Bad Gateway error when updating properties on a major version using the validateUpdateListItem() API call

Share the love

I covered how to update properties of a file or list item in SharePoint without affecting the version history in a previous post, however, I received a 502 Bad Gateway error when the file was in a major version and I was trying to update the properties. If the file was in a minor version, the API call had no issues updating the properties.

I found that by adding the modified date to the list of properties I wanted to update, the call would go through. Just for good measure, I also added the Modified By property (Editor) so that it would display the user who modified the item, instead of the flow user.

[
  {
    "FieldName": "NextReview",
    "FieldValue": "addDays(utcNow(),365,'M/d/yyyy')"
  },
  {
    "FieldName": "Modified",
    "FieldValue": "formatDateTime(triggerOutputs()?['body/Modified'],'g')"
  },
  {
    "FieldName": "Editor",
    "FieldValue": "concat('[{''Key'':''',triggerOutputs()?['body/Editor/Claims'],'''}]')"
  }
]

This worked great! However, it seemed to put my file into a minor version. For example, if I had just published my file and the version number was 4.0, my code would run and update the properties but the version number would be updated to 4.1, even though I had set “bNewDocumentUpdate”:true.

I had to think of another way to update the properties but keep the file in a major version.

I decided to use Content Approval. In the Versioning Settings on the library, you can turn on Content Approval. This will require your files to be approved before they are published.

Instead of a user clicking Publish in the library, they would now select Submit for approval which would change the Approval Status of the item to Pending (the Approval Status column is automatically added when you allow Content Approval).

As the trigger for the flow is ‘when an item is modified’, I would look for an item that is Pending when my flow ran. In my condition, if the item is Pending, I would make my property changes. As the file is in a minor version (this will always be the case as the Approval Status changes to Pending which creates a version change), I will be able to make the changes without affecting the version with using the validateUpdateListItem() API call. Using the JSON in the Compose action I posted above, I add that to the body of the SharePoint HTTP request.

_api/web/lists/getbytitle('<List Title>')/items(<ID>)/validateUpdateListItem()
{
   "formValues": outputs('Compose_-_Form_values_for_a_major_version'),
   "bNewDocumentUpdate":true
}

Now that we have updated the properties, it’s time to publish the document. There is an out-of-the-box flow action called Set content approval status, but I like to programmatically update the Approval Status. To do this, add another Compose action and set the Approval Status to 0 (the internal name for Approval Status is _ModerationStatus and 0 is equivalent to approved; see the values Microsoft provide for Approval Status in this article).

[
  {
    "FieldName": "_ModerationStatus",
    "FieldValue": "0"
  }
]

Finally, we’ll create another HTTP request to SharePoint. We cannot update properties and update the Approval Status at the same time, hence the reason we have to create two HTTP SharePoint requests.

_api/web/lists/getbytitle('<List Title>')/items(<ID>)/validateUpdateListItem()
{
   "formValues": outputs('Compose_-_Form_value_for_Moderation_Status'),
   "bNewDocumentUpdate":true
}

After the second SharePoint HTTP request runs, your file will be approved. Since Content Approval is turned on, when an item is approved, it will also change to a major version.


Share the love

5 Replies to “Overcoming 502 Bad Gateway error when updating properties on a major version using the validateUpdateListItem() API call”

  1. I thought that this was going to be unsolvable. I was trying to Update ModerationStatus and Editor in one call. You’ve saved me!

Leave a Reply to RobA Cancel reply

Your email address will not be published. Required fields are marked *