如何使用JavaScript从多个网站集中获取SharePoint列表项

时间:2012-01-03 11:29:40

标签: javascript sharepoint sharepoint-2010

有人能告诉我如何使用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());
}

显示“List not exists”之类的消息。我认为它会检查第一个网站集的列表,这就是弹出此消息的原因。有人可以帮我解决这个问题吗?

谢谢,

Rasu

1 个答案:

答案 0 :(得分:0)

您可以使用“构造函数”new ClientContext(serverAbsoluteUrl)为特定网址创建上下文:

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);

    // ...