none
SharePoint REST API - Created List item via REST but cannot update the author/editor/created/modified metadata RRS feed

  • Question

  • I am creating an item using the REST api in SP2016 but then I am trying to update the authorId/editorId back to the original  item's data though I am having no luck.

    This is what I am trying in PowerShell:

     $updateHeaders_Post = @{
                        'X-RequestDigest' = $debugXRequestDigest
                        "Accept" = "application/json;odata=verbose"
                        "Content-Type" = "application/json;odata=verbose"
                        "X-HTTP-Method" = "MERGE"
                        "If-Match" = "*"
                        }
    
                        $updateBody_Post = @{
                                "__metadata" = @{
                                                type = "SP.Data.$($docTag)Item"
                                                }
                            "AuthorId" = 60
                            "EditorId" = 64 #$origFileInfo.EditorId
                            "Created"  = $origFileInfo.Created
                            "Modified" = $origFileInfo.Modified
                            "Title" = "Testing123"
                        }
    
                        Invoke-RestMethod -uri ($debugUri + '/_api/web/lists/GetByTitle(''{MyList}'')/items('+$newFileId+')') -Body ($updateBody_Post | ConvertTo-Json) -Headers $updateHeaders_Post -UseDefaultCredentials -Method Post #-ErrorAction SilentlyContinue
                        #Update Item End
    
                        
    					<# Debug Version End #>


    • Edited by DevMethods Friday, August 21, 2020 6:47 PM
    Friday, August 21, 2020 6:47 PM

All replies

  • You can check to see if this endpoint is available: /ValidateUpdateListItem.

    It functions similar to the SystemUpdate() method used in the SP server code for doing these types of updates.

    So in your example above, your code would be something like this:

    $updateHeaders_Post = @{
    	"X-RequestDigest" = $debugXRequestDigest
    	"Accept" = "application/json;odata=nometadata"
    	"Content-Type" = "application/json;odata=nometadata"
    }
    
    $updateBody_Post = @"
    {
    	`"formValues`" = [
    		{ `"FieldName`":`"Title`",`"FieldValue`":`"Testing123`" },
    		{ `"FieldName`":`"Author`",`"FieldValue`":`"[{'Key':'$($origFileInfo.Author.Claim)'}]`" },
    		{ `"FieldName`":`"Created`",`"FieldValue`":`"$($origFileInfo.Created)`" },
    		{ `"FieldName`":`"Editor`",`"FieldValue`":`"[{'Key':'$($origFileInfo.Author.Claim)'}]`" },
    		{ `"FieldName`":`"Modified`",`"FieldValue`":`"$($origFileInfo.Modified)`" },
    	],
    	`"bNewDocumentUpdate`":true
    }
    "@;
    
    Invoke-RestMethod -uri ($debugUri + '/_api/web/lists/GetByTitle(''{MyList}'')/items('+$newFileId+')/ValidateUpdateListItem') -Body $updateBody_Post -Headers $updateHeaders_Post -UseDefaultCredentials -Method Post #-ErrorAction SilentlyContinue

    **The update body is already a literal JSON string, as I had trouble coming up with the correct PS object for it.**

    One of the important things is to use the claim of the user, which will look like this:

    i:0#.w|some.user@somedomain.com

    Dates should be in ISO 8601 format:

    yyyy-MM-ddTHH:mm:ssZ

    Here are some good blogs about the ValidateUpdateListItem function:

    https://robertschouten.com/2018/04/30/update-file-metadata-with-rest-api-using-validateupdatelistitem-method/

    https://techcommunity.microsoft.com/t5/microsoft-sharepoint-blog/update-file-metadata-with-rest-api-using-validateupdatelistitem/ba-p/1365682

    Hope that helps. :-)




    • Edited by Kem-El Friday, August 28, 2020 9:19 PM Added comment about literal JSON string
    Friday, August 28, 2020 9:10 PM
  • Hi DevMethods,

    Non-SPMT(SharePoint Migration Tool) has transitioned to Microsoft Q&A for support, please visit the "Microsoft 365 on Q&A" site within Microsoft Q&A to post all non-SPMT questions regarding SharePoint.

    All SPMT(SharePoint Migration Tool) questions can continue to be asked in this TechNet forum.

    Best Regards,

    Michael Han


    "SharePoint" forums will be migrating to a new home on Microsoft Q&A !
    We invite you to post new questions in the "SharePoint" forums' new home on Microsoft Q&A !

    Friday, October 23, 2020 8:53 AM