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.

dtheodor
  • 101
  • 1
  • 6

3 Answers3

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.

Esaki
  • 1,262
  • 9
  • 18
  • 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 Nov 17 '16 at 08:34
  • Yes its a User Object , in CSOM FieldUserValue object we need to specify – Esaki Nov 17 '16 at 08: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);
});
Yavuz
  • 180
  • 10
0

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.

Atish Kumar Dipongkor
  • 13,371
  • 5
  • 32
  • 64
  • 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 Nov 16 '16 at 09: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? – Atish Kumar Dipongkor Nov 16 '16 at 09: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 Nov 16 '16 at 09: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. https://technet.microsoft.com/en-us/library/ff607729.aspx – Atish Kumar Dipongkor Nov 16 '16 at 09:52
  • Actually it is possible via REST or CSOM, the only condition is to set ReadOnlyField = false. – Sergei Sergeev Nov 16 '16 at 10:47
  • OP set ReadOnlyField = false and tried without luck. Can you post your answer? @Kai – Atish Kumar Dipongkor Nov 16 '16 at 10:54
  • I don't know why it doesn't work for @dtheodor. But I did it without problems many times – Sergei Sergeev Nov 16 '16 at 10:58
  • Take a look here This is definitely allowed through the client side. @dtheodor has some other issues while updating the item, I don't think Editor is the cause of this. – Sergei Sergeev Nov 16 '16 at 11:07
  • just curious what's in the ContextProvider.GetContext()? – Atish Kumar Dipongkor Nov 16 '16 at 11:13
  • @AtishDipongkor Just regular class for constructing ClientContext with help of SharePointOnlineCredentials, similar to this blog post – Sergei Sergeev Nov 16 '16 at 11:41
  • Tried the same thing but no luck in my environment too @Kai – Atish Kumar Dipongkor Nov 16 '16 at 11:49
  • @Kai, I'm learning a lot from you today. Thought this was not possible with CSOM. – wjervis Nov 16 '16 at 13:52
  • Is CSOM working for you @wjervis? I tried but not working – Atish Kumar Dipongkor Nov 16 '16 at 16:47
  • Getting error The URL 'MyLib/nexus 2.png' is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web – Atish Kumar Dipongkor Nov 16 '16 at 17:16
  • @AtishDipongkor, I have not yet had a chance to try. I have to update an app that uploads documents, so I'll try to see if I can make this change, but I probably won't have an answer for a few days. – wjervis Nov 17 '16 at 13:39
  • Sadly i didn't receive a definite answer to my problem. I don't want to let the bounty go to waste so unless a new answer apears I am going to award it to the answer of @AtishDipongkor, not because of correctness but because he showed the most involvement and participation in attempts to make this work and the discussion – dtheodor Nov 22 '16 at 14:45
  • Thanks for rewarding. I am assuring that if someone can answer this then I will add this bounty to that answer. @dtheodor – Atish Kumar Dipongkor Nov 23 '16 at 05:04