SharePoint Author/Created By

SharePoint Author/Created By

bigjohn1982bigjohn1982 Posts: 3Questions: 1Answers: 0

Hi, I've been trying to integrate a SharePoint list with Datatables which is working fine. Data is retrieved and displayed as expected, but whenever I try to use the Created By/Author column in SharePoint I get a "Requested unknown parameter" error.

I know in SharePoint the display name is "Created By" and the internal name is "Author". But neither seem to work.

Anyone any ideas?

Thanks...

John

Answers

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin

    We;d need to see the code you are using and the JSON data or a link to the page.

    Allan

  • bigjohn1982bigjohn1982 Posts: 3Questions: 1Answers: 0

    Apologies, I just realised that the REST service only returns the AuthorID. I should be able to figure it out from here.

    Sorry for wasting your time!

  • bigjohn1982bigjohn1982 Posts: 3Questions: 1Answers: 0

    Hi again Allan,

    I've managed to get the AuthorId from the SharePoint REST query, and I've managed to get the the User's Name using SPServices.

    I would like to replace the mData: AuthorId with the value I get from getUserInfo. Any ideas how I might be able to do that.

    Thanks in advance.

    Here is my code


    <script src="../Style%20Library/jquery.SPServices-2014.02.min.js"></script> <script type="text/javascript"> $(document).ready(function() { var thisUserAccount = $().SPServices.SPGetCurrentUser({ fieldName: "ID", debug: false }); getUserInfo(thisUserAccount); function getUserInfo(userId) { $().SPServices({ operation: "GetListItems", listName: "User Information List", CAMLQuery: "<Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + userId + "</Value></Eq></Where></Query>", CAMLRowLimit: 1, completefunc: function (xData, Status) { var userName; var row = $(xData.responseXML).SPFilterNode("z:row").get(0); userName = $(row).attr("ows_Name"); alert(userName); } }); } //alert(thisUserAccount); var call = $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/getByTitle('MyList')/items?$select=Title,AuthorId, Description&$filter=Author/Id eq '" + thisUserAccount + "'", //url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/getByTitle('MyList')/items", type: "GET", dataType: "json", headers: { Accept: "application/json;odata=verbose" } }); call.done(function (data,textStatus, jqXHR){ $('#tblMyRequests').dataTable({ "bPaginate": false, "bDestroy": true, "bProcessing": true, "aaData": data.d.results, "aoColumns": [ { "mData": "Title" }, { "mData": "AuthorId" }, { "mData": "Description" } ] }); }); call.fail(function (jqXHR,textStatus,errorThrown){ alert("Error retrieving Tasks: " + jqXHR.responseText); }); });
  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin

    You could use columns.data as a function rather than a string. However, if getUserInfo makes an ajax call that isn't going to work, even even if it did it would kill performance.

    Allan

  • EFH52EFH52 Posts: 8Questions: 1Answers: 0
    edited August 2017

    To do it wholly with REST and not fall back to SPServices you need to have 'Author/Name' in your $select= clause and 'Author' in your $expand= clause.

    https://www.yourserver.com/path/to/your/site/_api/web/list/getByTitle(%27YourList%27)/items?$select=Author/Name&$expand=Author
    

    Here is another method to get the current users ID without and AJAX call SPServices or not:

    function getUserId() {
      return _spPageContextInfo.userId;
    }
    
  • EFH52EFH52 Posts: 8Questions: 1Answers: 0

    Your line 30:
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/getByTitle('MyList')/items?$select=Title,AuthorId, Description&$filter=Author/Id eq '" + thisUserAccount + "'",
    to:
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/getByTitle('MyList')/items?$select=Title,AuthorId,Author/Name,Description&$expand=Author&$filter=Author/Id eq '" + thisUserAccount + "'",

    Your line 46:
    { "mData": "AuthorId" },
    with:
    { "mData": "author.id" },

    This should get you close. I have used it in much of my code base and for retrieving file names and making links.
    If you $select for 'File/ServerRelativeUrl,File/Name', then $expand for 'File'; you can build links to your documetns like this:

    columns: [
        {data: function(source, type, val) {
                        return "<a target='_blank' href='"+_spPageContextInfo.siteAbsoluteUrl+source.File.ServerRelativeUrl+"'>"+source.File.Name+"</a>"
        }},
    
This discussion has been closed.