1

I am able to get items from a list using CSOM/JSOM using JavaScript and it seems to pull in the Created By (Author) value but it does not seem to pull the Modified By (Editor) value.

How can I get the Modified By (Editor) value also?

ExecuteOrDelayUntilScriptLoaded(function()
{
    var context = new SP.ClientContext.get_current()
    var list = context.get_web().get_lists().getByTitle("test");
    var view = list.get_views().getByTitle("All Items");
    context.load(view);

    context.executeQueryAsync(Function.createDelegate(this, function(sender, args)
    {
        var query = new SP.CamlQuery();
        query.set_viewXml("<View><Query>" + view.get_viewQuery() + "</Query></View>");

        var items = list.getItems(query);

        context.load(items);

        context.executeQueryAsync(Function.createDelegate(this, function(sender, args)
        {
            var listEnumerator = items.getEnumerator();
            while(listEnumerator.moveNext())
            {
                var listItem = listEnumerator.get_current();

                // this works
                var createdBy = listItem.get_item("Author");

                // THIS DOES NOT WORK
                // THIS DOES NOT WORK
                // THIS DOES NOT WORK
                // THIS DOES NOT WORK
                // THIS DOES NOT WORK
                var createdBy = listItem.get_item("Editor");
            }
        }), Function.createDelegate(this, function(){}));
    }), Function.createDelegate(this, function(){}));
}, "sp.js");
  • May be because you have created 'createdBy' variable two times ? – Ronak Patel Oct 19 '16 at 4:14
  • Note that createDelegate bloats your code and is old IE8 code: sharepoint.stackexchange.com/questions/164893/… – Danny '365CSI' Engelman Oct 19 '16 at 8:37
  • @RonakPatel: Naw, it should just replace the variable. – IMTheNachoMan Oct 19 '16 at 12:26
  • @Danny'365CSI'Engelman Ah. Thanks! Does it matter that at my company all SP sites default to IE8 mode even though we're on IE 11? – IMTheNachoMan Oct 19 '16 at 12:27
  • Yes, bind is available since IE9 and officially Microsoft does not support anything below IE9 anymore since january 12th of this year.. so if you put in a support call they will say.. "We don't support IE8" – Danny '365CSI' Engelman Oct 19 '16 at 13:08
1

You need to include the field "Editor". Try below code:

    ExecuteOrDelayUntilScriptLoaded(function()
{
    var context = new SP.ClientContext.get_current()
    var list = context.get_web().get_lists().getByTitle("test");
    var view = list.get_views().getByTitle("All Items");
    context.load(view);

    context.executeQueryAsync(Function.createDelegate(this, function(sender, args)
    {
        var query = new SP.CamlQuery();
        query.set_viewXml("<View><Query>" + view.get_viewQuery() + "</Query></View>");

        var items = list.getItems(query);

        context.load(items,'Include(Editor)');

        context.executeQueryAsync(Function.createDelegate(this, function(sender, args)
        {
            var listEnumerator = items.getEnumerator();
            while(listEnumerator.moveNext())
            {
                var listItem = listEnumerator.get_current();

                // this works
                var createdBy = listItem.get_item("Author");

                var modifiedBy = listItem.get_item("Editor");
            }
        }), Function.createDelegate(this, function(){}));
    }), Function.createDelegate(this, function(){}));
}, "sp.js");
  • Thank you. It turns out there is nothing wrong with my code but rather my list has too many fields and getItems isn't returning all of them. When I deleted one field from the list everything worked as expected. Odd. – IMTheNachoMan Oct 19 '16 at 12:25
0

It should work can you try below code,

<script language="ecmascript" type="text/ecmascript">

        var listItem;
        var list;
        var clientContext;

        function getFieldUserValue() {

            this.clientContext = SP.ClientContext.get_current();
            if (this.clientContext != undefined && clientContext != null) {
                var webSite = clientContext.get_web();
                this.list = webSite.get_lists().getByTitle("CustomList");
                this.listItem = list.getItemById(1);
                clientContext.load(this.listItem);
                this.clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess), Function.createDelegate(this, this.OnLoadFailed));
            }
        }

        function OnLoadSuccess(sender, args) {
            var fieldUserValueCreatedBy = this.listItem.get_item("Author");
            var fieldUserValueModifiedBy = this.listItem.get_item("Editor");
            alert("Created By: " + fieldUserValueModifiedBy.get_lookupValue() + "\n Modified By: " + fieldUserValueModifiedBy.get_lookupValue() + "\n");
        }


        function OnLoadFailed(sender, args) {
            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }</script>
  • Thank you. It turns out there is nothing wrong with my code but rather my list has too many fields and getItems isn't returning all of them. When I deleted one field from the list everything worked as expected. Odd. – IMTheNachoMan Oct 19 '16 at 12:25
0

So it turns out there is nothing wrong with my code but rather my list has too many fields and getItems isn't returning all of them. When I deleted one field from the list everything worked as expected.

I am not sure how to get all fields even for lists with a lot of fields but for this particular issue deleting one field did fix the problem.

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.