【问题标题】:How to retrieve Sharepoint List in Sharepoint App?如何在 Sharepoint App 中检索 Sharepoint 列表?
【发布时间】:2015-04-01 20:34:43
【问题描述】:

我的网站内容中有我的应用和列表,如下所示:

我想从我的应用程序中读取、写入和操作来自 TestList 的数据。

TestList的内容是这样的:

我一直在尝试用这个来阅读它:

function setup() {

    var context = SP.ClientContext.get_current();

    var lists = context.get_web().get_lists();
    var list = lists.getByTitle('TestList');
    var listItem = list.getItemById("Title1"); // is Id the list title?

    context.load(listItem);
    context.executeQueryAsync(onLoadSuccess, onLoadFail);

    console.log(listItem);

}

function onLoadFail(sender, args) {
    alert(args.get_message());
    console.log(args.get_message());
};

以及herehere 概述的其他一些方法,但我经常遇到错误:

具有 URL 的站点中不存在列表“TestList” 'https://mysite.sharepoint.com/sites/developer/TestApp'。

我认为这可能与 TestList 是与 TestApp 位于同一目录级别的应用程序有关,而不是 TestApp 中的列表,这就是我包含图片的原因。但是,我不知道如何在 TestApp 中嵌入 TestList。

我对在应用程序中创建列表的另一个担忧是,每当我更新和重新部署 TestApp 时,它都会擦除对 TestList 的任何新更新。

有人可以看到我做错了什么或提供一些建议吗?提前致谢。

【问题讨论】:

    标签: javascript list sharepoint sharepoint-2013 sharepoint-list


    【解决方案1】:

    你需要使用正确的ClientContext。

    var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    currentcontext = new SP.ClientContext.get_current();
    hostcontext = new SP.AppContextSite(currentcontext, hostUrl);
    web = hostcontext.get_web(); // hostcontext instead of currentcontext
    
    var lists = web .get_lists(); 
    var list = lists.getByTitle('TestList');
    var listItem = list.getItemById(1); // Id is Id (number)
    
    context.load(listItem);
    context.executeQueryAsync(onLoadSuccess, onLoadFail);

    更多详情:http://blog.appliedis.com/2012/12/19/sharepoint-2013-apps-accessing-data-in-the-host-web-in-a-sharepoint-hosted-app/

    【讨论】:

    • 那么你将如何使用它来访问TestList
    • 更新答案 - 使用“hostcontext”作为主机上下文,使用“currentcontext”作为应用程序上下文
    • 很好的答案。非常感谢
    【解决方案2】:

    我认为你是在正确的轨道上。您似乎想使用主机 Web 上的列表,但您正在使用 appweb 端点。我可以想到两个选择:

    1. 在您的应用程序中创建一个列表实例(转到在 Visual Studio 中添加项目,您可以选择 SharePoint 项目并选择一个列表),然后当您部署应用程序时该列表将存在于 appweb 中。正如您所说,当您重新部署应用程序时,这将清除列表,因为它将卸载并重新安装应用程序。如果您在生产中,更新应用程序不会做同样的事情。
    2. 对主机 Web 进行跨域调用并直接使用主机 Web 列表。根据应用程序的性质,这可能是更好的解决方案。请记住,您必须确保为您的应用设置权限,使其具有网站集的权限才能使调用正常工作,然后您必须捕获主机上下文才能使用主机 SP.Web 对象。我认为this link 可能会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多