2012-01-03 42 views
0

任何人都可以告诉我如何使用JavaScript获取SharePoint列表项目?如何使用JavaScript从多个网站集合获取SharePoint列表项目

我有两个网站集:site1和site2。我在site1应用程序中工作,我想获取site2的列表项。我怎样才能做到这一点?请帮帮我。

这是我使用的JavaScript端的代码:

var ctx; 
var listItem; 
var title; 
var col1; 
var col2; 

function SetItemValue(listItemId, listId, siteUrl, webUrl) { 
    ctx = new SP.ClientContext.get_current(); 
    var web; 
    var site = ctx.get_site(siteUrl);//Here passing the second sitecollection url  
    if (webUrl != undefined && webUrl != '') 
     web = site.openWeb(webUrl); 
    else 
     web = site.openWeb(''); 
    var list = web.get_lists().getById(listId);//Here passing the valid guid of list id 
    listItem = list.getItemById(listItemId); 
    ctx.load(list); 
    ctx.load(listItem); 
    ctx.executeQueryAsync(OnListLoaded); 

    list.update(); 
    web.update(); 
    ctx.load(web); 
} 

function OnListLoaded() { 
    listItem.set_item(col1, 'Hi'); 
    listItem.set_item(col2, 'Test'); 
    listItem.update(); 

    ctx.load(listItem); 
    ctx.executeQueryAsync(OnListUpdated, OnError); 
} 

function OnListUpdated(args) { 
} 

function OnError(sender, args) { 
    alert(args.get_message()); 
} 

它显示像“列表不存在”的消息。我想它会检查第一个网站集的列表,这就是为什么这个消息会弹出。任何人都可以帮我解决这个问题吗?

感谢,

Rasu

回答

0

您可以使用 “构造” new ClientContext(serverAbsoluteUrl)创建一个特定的URL上下文:

function SetItemValue(listItemId, listId, siteUrl, webUrl) { 
    ctx = new SP.ClientContext(siteUrl); 
    var web; 
    var site = ctx.get_site(siteUrl);//Here passing the second sitecollection url  
    if (webUrl != undefined && webUrl != '') 
     web = site.openWeb(webUrl); 

    // ... 
+0

这难道不是一个跨域调用?我认为使用JSOM是不可能的,必须在这里使用SharePoint App模式。 http://blogs.msdn.com/b/officeapps/archive/2012/11/29/solving-cross-domain-problems-in-apps-for-sharepoint.aspx – Ali 2014-10-02 00:24:31

相关问题