2

I have some code that I used to get my SP2010 task list last modified date using SP designer but doesn't seem to work and I tried to step through it with firebug but not really sure where my problem is and I am not getting an output it seems like. I also am not sure if I need a column just for Last modified date or not, I do not have that.

I tried this code here:

var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var lists = web.get_lists();
var list = lists.getByTitle("Task2BeCompleted");
ctx.load(list, "LastItemModifiedDate");
ctx.executeQueryAsync(
    function() {
        var lastmodified = list.get_lastItemModifiedDate();
    },
    function() {}
);

Tried to use firebug but not sure where to look for my response, used console.log and still not seeing my answer. I even tried using an alert to display my date but I think that the problem is with my function.

Don't have much experience with javascript or with developing for SP2010 so please any help would be really appreciated!

4 Answers 4

2

You should use caml query to retrieve 1 item with the latest modified date:

var _listItems;
ExecuteOrDelayUntilScriptLoaded(function () {
    var ctx = SP.ClientContext.get_current();
    var web = ctx.get_web();
    var lists = web.get_lists();
    var list = lists.getByTitle("Task2BeCompleted");
    ctx.load(list, "LastItemModifiedDate");

var query = new SP.CamlQuery();
  var rowlimit = "<RowLimit>" + itemAmount + "</RowLimit>";
  var orderByDesc = "<OrderBy><FieldRef Name='Modified'  Ascending='FALSE' /></OrderBy>";
  query.set_viewXml("<View><Query>" + orderByDesc + "</Query>" + rowlimit + "</View>");
        _listItems = list.getItems(query);
        ctx.load(_listItems);
  ctx.executeQueryAsync(
        function () {


       if(_listItems.get_count() > 0)
       {
         var latestItem = _listItems.get_item(0);
         var latestDate = latestItem.get_item("Modified");
        }
        },
        function () {
        }
    );
}, "sp.js");
2
  • Do I need to have a LastModified column in my sharepoint list though? I was looking at my list and I do not have that so I was wondering if the code was breaking because it didn't know where to get it.
    – Tomas E
    Commented Jan 17, 2014 at 3:57
  • Modified column is a default column of Sharepoint, it's included in every list of Sharepoint. You can use Sharepoint Manager to view this column.
    – MikeTran13
    Commented Jan 17, 2014 at 4:18
0

I guess "sp.js" might be not loaded. you can try this:

ExecuteOrDelayUntilScriptLoaded(function () {
    var ctx = SP.ClientContext.get_current();
    var web = ctx.get_web();
    var lists = web.get_lists();
    var list = lists.getByTitle("Task2BeCompleted");
    ctx.load(list, "LastItemModifiedDate");
    ctx.executeQueryAsync(
        function () {
            var lastmodified = list.get_lastItemModifiedDate();
        },
        function () {
        }
    );
}, "sp.js");

Hope it's helpful.

4
  • still no luck on this. I just get error on the page when I use an alert or try to display it on hover.
    – Tomas E
    Commented Jan 17, 2014 at 3:08
  • @TomasE Hello, what is the error message? And I also have 2 questions, first, if you set a break point at ctx.executeQueryAsync, will it stop at here? Second, can you set a break point in query failed function (you can give the function two parameters so you can track the request information from the parameters)? What's the error you get? Commented Jan 17, 2014 at 3:49
  • Not sure how to set a breakpoint? I am not creating a workflow.
    – Tomas E
    Commented Jan 17, 2014 at 4:00
  • @TomasE You can open firebug, click the "Script" tab, and choose your js file (or page that contains the script) in the dropdown list, then click at where you want to debug (on the line number). Then hover or refresh the page (it depends on how you execute the script) to trigger the breakpoint. Commented Jan 17, 2014 at 4:32
0

you place below code in content editor webpart and check

 <script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(Initializeall, 'sp.js');
 function Initializeall()
{
 alert("hi");
 var ctx = SP.ClientContext.get_current();
 var web = ctx.get_web();
 var lists = web.get_lists();
 var list = lists.getByTitle("Sample");
ctx.load(list, "LastItemModifiedDate");
ctx.executeQueryAsync(
function() {
alert("inside");
    var lastmodified = list.get_lastItemModifiedDate();
alert(lastmodified);
},
function() {}
);
}
     </script>
0

The code that you've provided for retrieving LastItemModifiedDate property is correct from functional perspective.

How to identify the error?

First option, print error details.

Please replace Failed callback in SP.ClientContext.executeQueryAsync method in order to display the actual error:

ctx.executeQueryAsync(
    function() {
        var lastmodified = list.get_lastItemModifiedDate();
    },
    function(sender,args) {
      alert('Request failed. ' + args.get_message() + 
        '\n' + args.get_stackTrace());
    }
);

Second option, debug CSOM (JavaScript) application (with IE).

Follow the article Using the F12 Developer Tools to Debug JavaScript Errors to find out how to debug (JavaScript) application and Debugging Script with the Developer Tools

IE Developer Tools

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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