REST API to create item with custom Created by and Modified by values in Sharepoint online

Gokulnath Palani [Teams] 21 Reputation points
2021-01-06T16:43:56.35+00:00

Hi all,

I am using the below REST API to create item in Sharepoint . but i could not able to map the EDitor and Author field. Is there anything I need to change in REST API ?

URI: _api/web/lists/getbytitle('test')/items REST API BODY:

{
"__metadata": { "type": "SP.Data.TestListItem" },
"Title" : "Submited",
"AuthorId" : 9
}

Please advise.

Any snippets.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,765 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,576 questions
0 comments No comments
{count} votes

Accepted answer
  1. MichaelHan-MSFT 18,016 Reputation points
    2021-01-07T05:01:13.34+00:00

    Hi @Gokulnath Palani [Teams] ,

    That's not in the same rest api all. You need to make two rest api call to achieve this.

    First, set the ReadOnlyField to false for created by field.

    URI: _api/web/lists/getbytitle('test')/fields/getbytitle('Created By')
    Body:

     {  
     "__metadata": { "type": "SP.FieldUser"},  
     "ReadOnlyField": false  
     }  
    

    54234-image.png

    Then make another rest api call to create item in Sharepoint

    URI: _api/web/lists/getbytitle('test')/items
    BODY:

    {  
    "__metadata": { "type": "SP.Data.TestListItem" },  
    "Title" : "Submited",  
    "AuthorId" : 9  
    }  
    
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. MichaelHan-MSFT 18,016 Reputation points
    2021-01-07T03:00:15.597+00:00

    Hi @Gokulnath Palani [Teams] ,

    By default, the ReadOnlyField attribute is true for fields Created By and Modified By in SharePoint. You could check it with the endpoint: /_api/web/lists/getbytitle('Mylist')/fields/getbytitle('Created By')

    54241-image.png

    You need to set the ReadOnlyField to false to use custom Created by and Modified by values with the rest api:
    Uri: /_api/web/lists/getbytitle('Mylist')/fields/getbytitle('Created By')

    Body:  
        {  
        "__metadata": { "type": "SP.FieldUser"},  
        "ReadOnlyField": false  
        }  
    

    Thus you could create item with custom Created by and Modified by values.


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Gokulnath Palani [Teams] 21 Reputation points
    2021-01-07T04:24:55.867+00:00

    Hi @MichaelHan-MSFT

    Thanks for your reply. I getting "The parameter __metadata does not exist in method GetByTitle." error .

    Is there anything i need to change?

    Thanks

    Body:
    {
    "__metadata": { "type": "SP.Data.TestListItem" },
    "Title" : "Submited",
    "__metadata": { "type": "SP.FieldUser"},
    "ReadOnlyField": false,
    "AuthorId" : 9

     }
    
    0 comments No comments