Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a custom page like this :

These are my links (refresh once a day. Last refresh time : **XXXXX** )

link1
link2
link3

And I have a custom list.

I want to add "last modified time" or "modified X mins ago" instead of "XXXXX" of the custom list in my page. Is that possible ? Might be easy for advanced dev, but I can't find what to googled.

I found this and this. So I tried to add JS via embedded code in my page :

<script language='javascript' type='text/javascript'>
var siteUrl = "http://mysite/mysubsite";
var context = new SP.ClientContext(siteUrl);
var web = context.get_web();
var list = web.get_lists().getByTitle('MyListName');
var value = SP.List.get_lastItemModifiedDate();
write(value);
</script>

But this doesn't return anything. As I'm not a developer, I really don't understand what's going on, Any ideas?

I have to say that I also tried this (I've customized variable as needed) without success :

function ReadListItem() {
    siteUrl = "my site Url";
    var listTitle = "NewsList";
    //Get the current client context
    var context = new SP.ClientContext(siteUrl);
    var newsList = context.get_web().get_lists().getByTitle(listTitle);
    var camlQuery = new SP.CamlQuery();         
    camlQuery.set_viewXml('<View><Query><OrderBy><FieldRef Name=\'Modified\' Ascending="FALSE"/></OrderBy><RowLimit>1</RowLimit></Query></View>";')
    this.listItems = newsList.getItems(camlQuery);
    context.load(listItems);
    context.executeQueryAsync(ReadListItemSucceeded, ReadListItemFailed);
}

function ReadListItemSucceeded(sender, args) {
    var itemsString = '';
    var enumerator = listItems.getEnumerator();
    while (enumerator.moveNext()) {
        var listItem = enumerator.get_current();
        itemsString += 'Modified On: ' + listItem.get_item('Modified');
    }
    $("#P1").append(itemsString);
}
share|improve this question

3 Answers 3

up vote 1 down vote accepted

Change your Javascript to

<script language='javascript' type='text/javascript'>
SP.SOD.executeOrDelayUntilScriptLoaded(function(){
var siteUrl = "http://mysite/mysubsite";
    var context = new SP.ClientContext(siteUrl);
    var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var lists = web.get_lists();
var list = lists.getByTitle("MyListTitle");
ctx.load(list, "LastItemModifiedDate");
ctx.executeQueryAsync(
    function() {
        alert(list.get_lastItemModifiedDate());
    },
    function() {}
);
}, 'sp.js');

</script>

You need to load what you want in the context object and then execute the query

share|improve this answer
    
Thanks for you answer. Trying this right now. And come back for feedback –  Nico May 16 at 12:57
    
Does not return anything. We agree that I click on 'modify page' -> 'Insert' -> 'Embedded code' -> Then paste this in the popup, then save and finally reload the page ? –  Nico May 16 at 13:00
    
You mean the webpart ? Process I explained do actually the same, and creates webpart. But I'll give a try. When I save and reload page (I'm using Google chrome) and use "inspect element", and can see an error : "Uncaught TypeError: undefined is not a function" on line var context = new SP.ClientContext(siteUrl);. Does this help you ? –  Nico May 16 at 13:04
1  
New code again, from my good friend @AnatolyMironov, chuvash.eu/2012/09/24/jsom-last-modified-date-of-a-list –  Robert Lindgren May 16 at 13:22
1  
You should use something like jQuery('#anElementId').text(ist.get_lastItemModifiedDate()); –  Robert Lindgren May 16 at 14:01

You can do this with JavaScript and the JavaScript Object Model, called JSOM in SharePoint 2013.

Go read this: http://msdn.microsoft.com/en-us/jj163201(v=office.15)

Try this: http://code.msdn.microsoft.com/SharePoint-2013-Execute-a41c49ff

function retrieveListItems(siteUrl) { 
    var clientContext = new SP.ClientContext(siteUrl); 
    var oList = clientContext.get_web().get_lists().getByTitle('Announcements'); 

    var camlQuery = new SP.CamlQuery(); 
    camlQuery.set_viewXml( 
        '<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' +  
        '<Value Type=\'Number\'>1</Value></Geq></Where></Query>' +  
        '<RowLimit>10</RowLimit></View>' 
    ); 
    this.collListItem = oList.getItems(camlQuery); 

    clientContext.load(collListItem); 
    clientContext.executeQueryAsync( 
        Function.createDelegate(this, this.onQuerySucceeded),  
        Function.createDelegate(this, this.onQueryFailed) 
    );  
} 

function onQuerySucceeded(sender, args) { 
    var listItemInfo = ''; 
    var listItemEnumerator = collListItem.getEnumerator(); 

    while (listItemEnumerator.moveNext()) { 
        var oListItem = listItemEnumerator.get_current(); 
        listItemInfo += '\nID: ' + oListItem.get_id() +  
            '\nTitle: ' + oListItem.get_item('Title') +  
            '\nBody: ' + oListItem.get_item('Body'); 
    } 

    alert(listItemInfo.toString()); 
} 

function onQueryFailed(sender, args) { 
    alert('Request failed. ' + args.get_message() +  
        '\n' + args.get_stackTrace()); 
}
share|improve this answer
1  
Thanks both of you for your comments. I didn't precise that I'm not a developer at all. I can understand a little bit JavaScript, but I don't know how to use it at all. I will have a look at your links, and try understand. I thought I could do that my modifying the .aspx page, and add something like <SharePoint:SPDataSource runat="server" DataSourceMode="List" ..., no ? According to this. –  Nico May 15 at 15:12
    
Well this is the best moment to get some development skills :) It's an easy example / exercise to start through. Read the articles, they will help you and have a look at the examples provided in the second link. –  hupseb May 15 at 15:17
1  
I learned for 4 years Networks and Systems. I used a few of VB, and I'm friendly with scripting (PS, Bash). But never used JS at all ! I'll read your articles (and get headache) and try doing this, thanks. So there is no way to do this by adding something in the .aspx page ? –  Nico May 15 at 15:20
    
Don't worry it's not that hard. You will not get a headache :) You can put the JavaScript into an .aspx page as well. It will be executed as soon as the page is loaded. Just like every other JavaScript. –  hupseb May 15 at 15:24
1  
Look at this: blog.cloudshare.com/2012/10/29/… Adding JavaScript is easy ;) –  hupseb May 15 at 15:35

Well you can use CAML Query and following query should bring you last modified item:

SPQuery query = new SPQuery();
query.Query = string.Concat(
      @"<OrderBy> 
           <FieldRef Name='Modified' Ascending='False' /> 
        </OrderBy>");
query.RowLimit = 1;

var myList = web.Lists.TryGetList("ListTitle");

if (myList != null)
{
   var itemCollection = myList.GetItems(query);

   var result = string.Empty;

   if (itemCollection.Count > 0)
   {
       SPListItem item = itemCollection[0];
       var lastModifiedDate = DateTime.Parse(item["Modified"].ToString());
       TimeSpan span = DateTime.Now - lastModifiedDate;
       result = "modified " + span.TotalMinutes + " mins ago";
   }

   // use result
}

This should help

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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