简体   繁体   English

使Javascript在SharePoint 2010上运行时遇到问题

[英]Having problems getting Javascript to work on SharePoint 2010

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. 上周,我一直在寻求帮助,以获取为SharePoint 2010工作的SharePoint 2007编写的简单javascript代码,不幸的是我并没有得到明确的答案,我可以在工作的地方使用它,因此我决定尝试自己制作。 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. 我非常不熟悉javascript和sharepoint,但是我是一位经验丰富的程序员。 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. 使用javascript访问SharePoint时,将使用ECMA客户端对象模型。 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. 我没有使用07的客户端对象模型,但是我使用的是'10,并且可以立即告诉您发布的代码在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. 至少在sp10 com中,每次获取SharePoint对象的新实例(列表,网站,listItem,列等)时,都需要将该项目设置为本地上下文,然后通过异步将其加载到服务器上方法。 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 以下是一些示例代码: 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/ 编辑:至于更新列表项,我刚刚在这里找到此链接: http : //sprider.org/2011/12/13/sharepoint-ecmascript-to-adddeleteupdateget-list-items/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM