I was looking for help last week to get a simple javascript code made for SharePoint 2007 working for SharePoint 2010 and didn't really get a clear answer that i could use where i work unfortunately so i decided to try to just make my own. It's suppossed to be an Auction List and have a countdown in the "Time Left" field till the item expires, but i can't figure out what's wrong. I am very unfamiliar with javascript and sharepoint but i am an experienced programmer. Can anyone help with this? Here's the code below:
<script type="text/javascript">
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var lists = web.get_lists();
var listId = SP.ListOperation.Selection.getSelectedList();
var list = lists.getById(listId);
var item = getItemByName("End Date");
var end = Date.parse(item.text())/1000;
var todayNow = new Date();
todayNow = Date.parse(today)/1000;
var result = (end-todayNow);
var item2 = getItemByName("Time Left");
item2.text(result);
</script>
The End Date is a field that will probably be hidden, but just used as a placeholder to find the difference from now till the item expires.
Thanks guys for any responses.
Edit: Ok thanks Robert, you've really helped a lot. I was just about to post this when i saw your last comment. I'm extremely close now since I've been googling and researching what you said in your first comment and I've gotten this far:
<
script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(getWebSiteData, "sp.js");
var context = null;
var web = null;
var lists = null;
var listId = null;
var list = null;
var item = null;
function getWebSiteData(){
context = new SP.ClientContext.get_current();
web = context.get_web();
lists = web.get_lists();
listId = SP.ListOperation.Selection.getSelectedList();
list = lists.getById(listId);
context.load(list, 'End Date');
context.executeQueryAsync(Function.createDelegate
(this, this.onSuccessMethod), Function.createDelegate
(this, this.onFailureMethod));
}
function onSuccessMethod(sender, args){
alert('web title:' + web.get_title() + '\n ID:' + web.get_id
());
}
function onFailureMethod(sender, args){
alert('request failed' + args.get_message
() + '\n' + args.get_stackTrace());
}
</script>
I think the only thing i have left to do is figure out how to get the current item so i can set the Time Left for that specific item. Do you know how to do that? Am i as close as i think? Thanks again for your help.
When you use javascript to access SharePoint, you're using the ECMA Client Object Model. I haven't worked with the Client Object Model for 07, but I have for '10, and right away can tell the code you posted won't work in sp10. At least in sp10 com, every time you grab a new instance of a SharePoint object (list, web, listItem, column, etc), you need to set that item into your local context, then load the context against the server via an asynchronous method. Only after that point, can you access the fields in the referenced object that you desire. Let me know if you cna't figure it out. Here's some sample code: http://pastebin.com/3amgaEhv
edit: As for updating list items, I just found this link, here: http://sprider.org/2011/12/13/sharepoint-ecmascript-to-adddeleteupdateget-list-items/