3

How can I get CreatedBy field using REST API SharePoint? Below is the code

var Ownurl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Listname')/items?$select=Author,ID&$filter=ID eq 1;  

        $.ajax({  
               url: Ownurl,  
               headers: { Accept: "application/json;odata=verbose" },  
               async:false,  
               success: function (data) {  
                        var items = data.d.results;  
                        if (items[0].Author!= "") {  
                                author = items[0].Author;
                            }  
                        },eror: function (data) {  
                            alert("An error occurred. Please try again.");  
                            }  
               }); 

But somehow I am not able to get Author. Is there anything wrong?

Any help would be appreciated

Thanks.

  • CreatedBy is type of person & group. So i think you have to make changes in query. Refer this link – SP Developer Feb 29 '16 at 5:48
6

Please try following REST URL, you must expand the Person and Group field.

var Ownurl = _spPageContextInfo.webAbsoluteUrl + "_api/web/lists/getbytitle('Listname')/items?$filter=Id eq 1&$expand=Author&$select=Author/Id"
  • 1
    You can try like this to get the ID of the Authour without expanding the field var Ownurl = _spPageContextInfo.webAbsoluteUrl + "_api/web/lists/getbytitle('Listname')/items?$select=AuthorId" – Vishnu PS Jul 30 '16 at 9:35
  • @VishnuPS I get Column 'AuthorId' does not exist doing that. – vapcguy Aug 28 at 13:54
  • @VishnuPS Could you please share your REST API Url.. – Rohit Waghela Aug 28 at 14:46
  • @VishnuPS Actually, I figured out my issue. I could not include a filter like I wanted to do. That line was fine. It just meant I would have to check for the length of my results and iterate each one, then check if the data.d.results[i].AuthorId == authorId; ... – vapcguy Aug 28 at 20:45
  • @RohitWaghela That actually IS the entire API URL... And I couldn't get your version to work, but his was fine... – vapcguy Aug 28 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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