2012-01-03 39 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

相關問題