10

I'm trying to alter a SharePoint Online file's 'Author' and 'Editor' through the REST API. I have first made sure to set the list fields 'Created By' and 'Modified By' property ReadOnlyField = false. I'm trying any REST API I can get my hands on:


Sharepoint 2013 REST API

url: http://site/_api/web/Lists(guid'...')/items(3)
method: POST
headers:
    Authorization: "Bearer " + accessToken
    "IF-MATCH": "*"
    "X-HTTP-Method":"MERGE",
    content-type: "application/json;odata=verbose"
body: {"__metadata": {"type": "SP.Data.DocumentsItem"}, "EditorId": 21}

Error

status code: 409
body:
{
"odata.error" : {
    "code" : "-2130575305, Microsoft.SharePoint.SPException",
    "message" : {
        "lang" : "en-US",
        "value" : "The file <filename> has been modified by <username> on <timestamp>."
    }
}

This error basically says "I can't set the editor to the value you give me because the editor is this user", where this user points to the previous editor


Sharepoint 2010 REST API

url: http://site/_vti_bin/listdata.svc/Documents(3)
method: POST
headers:
    Authorization: "Bearer " + accessToken
    "IF-MATCH": "*"
    "X-HTTP-Method":"MERGE",
    content-type: "application/json;odata=verbose"
body: {"ModifiedById": 21}

Error

status code: 500
body:
{
    "error" : {
        "code" : "",
        "message" : {
            "lang" : "en-US",
            "value" : "An error occurred while processing this request." 
        }
    }
}

Office 365 Files API

url: http://site/_api/v1.0/me/files/<file-id>
method: PATCH
headers:
    Authorization: "Bearer " + accessToken
    content-type: "application/json;odata=verbose"
body: {'lastModifiedBy': {'user': {'id': '<user uuid>'}}}

Error

status code: 400
body:
{
    "error" : {
        "code" : "-1, Microsoft.OData.Core.ODataException",
        "message" : "Parsing JSON Light feeds or entries in requests without entity set is not supported. Pass in the entity set as a parameter to ODataMessageReader.CreateODataEntryReader or ODataMessageReader.CreateODataFeedReader method."
    }
}

None of the responses make sense to me. Anyone had any luck with this?

If modifying these fields for already existing files is not possible, I would also be interested in being able to set these fields for new files being uploaded.

8
  • 2
    These fields are being updated by Authorization Token. Does not matter what you put in request body. Commented Nov 15, 2016 at 11:47
  • This cannot be done via client object model
    – wjervis
    Commented Nov 15, 2016 at 12:02
  • An answer like this sharepoint.stackexchange.com/questions/134043/… hints that this may be possible through the client model
    – dtheodor
    Commented Nov 15, 2016 at 13:09
  • 1
    I can update other fields fine, fields like name, modification date. When setting 'EditorId' either alone or with other fields I get this error
    – dtheodor
    Commented Nov 16, 2016 at 11:36
  • 1
    You can achieve similar functionality Using REST API. I have tested in for Sharepoint Online. You can find code snippet at Update Editor and Author Filed
    – anomepani
    Commented Feb 2, 2020 at 13:05

3 Answers 3

1

Yes , This is possible with the help of SharePoint CSOM , that is using Microsoft.SharePoint.Client.dll ,

here i have done this while migrating the SharePoint 2010 list items into the SharePoint 2013 list where i had to maintain the old values in the fields #CreatedBy , #ModifiedBy , #Created and #Modified.

Here while copying the item we can assign our custom values to these fields like below:

item["Author"] = "Custom User Object";
item["Editor"] = "Custom User Object";
item["Created"] = "Custom Date value";
item["Modified"] = "Custom Date value";

This worked for me ,

if needed this can be developed as a console application and can be run to update the custom values to these fields.

2
  • What is the contents of "Custom User Object"? Is it a C# object, is it a string, how do you get it and what is its value?
    – dtheodor
    Commented Nov 17, 2016 at 8:34
  • Yes its a User Object , in CSOM FieldUserValue object we need to specify
    – Esaki
    Commented Nov 17, 2016 at 8:47
0

I could not change author field but another people field i can change like below. So maybe you can set this field and show this field as Created By column. If you find axact answer of your question please let us know. Also be sure your "X-RequestDigest" value is where your library value.

var item = {
    "type": "SP.Data.TestListItem",
    "UserFieldId": 532
};

var headers = {
    "Accept": "application/json;odata=verbose",
    "X-RequestDigest": $("#__REQUESTDIGEST").val(),
    "X-HTTP-Method": "MERGE",
    "If-Match": "*"
};

$.ajax({
    url: "/_api/web/lists/getbytitle('Test')/items(2)",
    method: "POST",
    data: JSON.stringify(item),
    contentType: "application/json;odata=verbose",
    headers: headers,
    async: false
}).done(function (data) {
    console.log(data);
});
0
0
+100

It's not possible or it's not allowed in client side. If it is allowed, then it will be security issue. Let say, I am updating some metadata of a document and finally setting another using in Modified By column. So

How it will be tracked who has updated the file?

I believe Created By and Modified By are automatically being added/updated based Current User/Authorization Token/X-RequestDigest.

If you need to replace some users after migration, then you have to use PowerShell. Did not find any client side solution yet.

Move-SPUser [-Identity] <SPUserPipeBind> -NewAlias <String> [-AssignmentCollection <SPAssignmentCollection>] [-Confirm [<SwitchParameter>]] [-IgnoreSID <SwitchParameter>] [-WhatIf [<SwitchParameter>]]

Above command will update the all references of old user by new user.

17
  • I see your point. However it would still make sense to allow modifying these fields from an administrator account (which is what I am trying)
    – dtheodor
    Commented Nov 16, 2016 at 9:30
  • Me tried too but no luck. Let me know if you find any way out. when administrator is updating, Modified By should be administrator. Isn't it expected? Commented Nov 16, 2016 at 9:41
  • Well, no if the administrator has a specific purpose for setting the Modified by to a different value. For example after running a script to move user data (as part of a migration or something like that), after the move is complete the previous Modified By values will be all overwritten by the admin account which is not desired, the original Modified By values should be retained
    – dtheodor
    Commented Nov 16, 2016 at 9:45
  • In that case, you have to replace old user with new user. Not updating using different user's Authorization Token. I did this things using powershell after migration. technet.microsoft.com/en-us/library/ff607729.aspx Commented Nov 16, 2016 at 9:52
  • Actually it is possible via REST or CSOM, the only condition is to set ReadOnlyField = false. Commented Nov 16, 2016 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.