【问题标题】:Error trying to access Sharepoint Online host web list data from provider hosted add-in part using JSOM CDL尝试使用 JSOM CDL 从提供商托管的加载项部分访问 Sharepoint Online 主机 Web 列表数据时出错
【发布时间】:2017-05-02 03:20:01
【问题描述】:

我已经在整个互联网上寻找答案,但仍然无法弄清楚。有很多例子与我正在尝试做的事情很接近,但实际上没有一个是准确的。这是我正在尝试做的事情:

我有一个托管 Sharepoint 加载项的提供商,它有一个带有 C# 代码隐藏的 aspx 页面。我在提供商托管端(Web 端,而不是应用程序 Web)也有一个 javascript 文件,我试图用它来访问主机 Web 中的一些列表数据。

这是我正在使用的代码:

var customWP_NewsAndInfo_allArticles;
var hostweburl;
var addinweburl;

    // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
    $(document).ready(function () {
        //Get the URI decoded URLs.
        hostweburl =
            decodeURIComponent(
                getQueryStringParameter("SPHostUrl")
        );
        addinweburl =
            decodeURIComponent(
                getQueryStringParameter("SPAppWebUrl")
        );

        // resources are in URLs in the form:
        // web_url/_layouts/15/resource
        var scriptbase = hostweburl + "/_layouts/15/";

        // Load the js files and continue to the successHandler
        $.getScript(scriptbase + "SP.Runtime.js",
            function () {
                $.getScript(scriptbase + "SP.js",
                    function () { $.getScript(scriptbase + "SP.RequestExecutor.js", customWP_NewsAndInfo_retrieveListItems); }
                    );
            }
            );
    });


function customWP_NewsAndInfo_retrieveListItems() {
        // get parameters from the query string for add-in part properties
        var sourceList = 'News and Information';


        // context: The ClientContext object provides access to
        //      the web and lists objects.
        // factory: Initialize the factory object with the
        //      app web URL.
        var clientContext = new SP.ClientContext(addinweburl);
        var factory = new SP.ProxyWebRequestExecutorFactory(addinweburl);
        clientContext.set_webRequestExecutorFactory(factory);
        var appContextSite = new SP.AppContextSite(clientContext, hostweburl);

        this.web = appContextSite.get_web();
        clientContext.load(this.web);

        var web = clientContext.get_web();

        var oList = web.get_lists().getByTitle(sourceList);

        var items = oList.getItems(SP.CamlQuery.createAllItemsQuery());
        var includeExpr = 'Include(Title)';
        clientContext.load(items, includeExpr);

        clientContext.executeQueryAsync(
            Function.createDelegate(this, this.customWP_NewsAndInfo_onQuerySucceeded),
            Function.createDelegate(this, this.customWP_NewsAndInfo_onQueryFailed)
        );
}

我不需要展示成功和失败的功能,因为它从来没有达到那些。当它运行 executeQueryAsync 时,它总是返回此错误和堆栈跟踪:

MicrosoftAjax.js:5 Uncaught TypeError: Cannot read property 'apply' of undefined
    at Array.<anonymous> (MicrosoftAjax.js:5)
    at MicrosoftAjax.js:5
    at SP.ClientRequest.$3I_0 (SP.Runtime.js?_=1493695027549:2)
    at Array.<anonymous> (MicrosoftAjax.js:5)
    at MicrosoftAjax.js:5
    at Sys.Net.WebRequest.completed (MicrosoftAjax.js:5)
    at SP.ProxyWebRequestExecutor.$1W_1 (SP.RequestExecutor.js?_=1493695027551:2)
    at Function.SP.ProxyWebRequestExecutorInternal.processSuccessCallback (SP.RequestExecutor.js?_=1493695027551:2)
    at SP.RequestInfo.success (SP.RequestExecutor.js?_=1493695027551:2)
    at SP.RequestExecutor.internalOnMessage (SP.RequestExecutor.js?_=1493695027551:2)

我什至尝试通过 Sharepoint Hosted 加载项进行故障排除,但我得到了一个非常相似的错误:

Uncaught TypeError: Cannot read property 'apply' of undefined
    at Array.<anonymous> (ScriptResource.axd?d=NmWpgseF5Lm2ObZqsFB7X8Sc_FI92_ZNS_ucNmpXp96dB8TO5HMUZeqx3R5ocUw8b6hyk9AKxItxXJ…:5)
    at ScriptResource.axd?d=NmWpgseF5Lm2ObZqsFB7X8Sc_FI92_ZNS_ucNmpXp96dB8TO5HMUZeqx3R5ocUw8b6hyk9AKxItxXJ…:5
    at SP.ClientRequest.$x_0 (SP.Runtime.js?_=1493693899820:2)
    at SP.ClientRequest.$3I_0 (SP.Runtime.js?_=1493693899820:2)
    at Array.<anonymous> (ScriptResource.axd?d=NmWpgseF5Lm2ObZqsFB7X8Sc_FI92_ZNS_ucNmpXp96dB8TO5HMUZeqx3R5ocUw8b6hyk9AKxItxXJ…:5)
    at ScriptResource.axd?d=NmWpgseF5Lm2ObZqsFB7X8Sc_FI92_ZNS_ucNmpXp96dB8TO5HMUZeqx3R5ocUw8b6hyk9AKxItxXJ…:5
    at Sys.Net.WebRequest.completed (ScriptResource.axd?d=NmWpgseF5Lm2ObZqsFB7X8Sc_FI92_ZNS_ucNmpXp96dB8TO5HMUZeqx3R5ocUw8b6hyk9AKxItxXJ…:5)
    at SP.ProxyWebRequestExecutor.$1W_1 (SP.RequestExecutor.js?_=1493693899822:2)
    at Function.SP.ProxyWebRequestExecutorInternal.processSuccessCallback (SP.RequestExecutor.js?_=1493693899822:2)
    at SP.RequestInfo.success (SP.RequestExecutor.js?_=1493693899822:2)

我尝试创建一个名为“testing”的自定义列表,并且只使用了'Include(Title)',但我收到了同样的错误。我尝试插入 debugger; 并使用 Chrome 开发者工具单步执行代码,但很难阅读混淆的 Microsoft 代码。任何有关解决此问题的帮助将不胜感激!提前非常感谢!

保罗

【问题讨论】:

    标签: javascript sharepoint sharepoint-online


    【解决方案1】:

    我终于能够通过使用this code 解决这个问题并从中恢复,因为它在我运行它时工作。我终于发现问题出在电话上:

    clientContext.executeQueryAsync(
            Function.createDelegate(this, this.onQuerySucceeded),
            Function.createDelegate(this, this.onQueryFailed)
        );
    

    当我与 github 示例进行比较时,他们的调用是这样的:

    clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);
    

    当我取出 Function.createDelegate 部分并使用他们使用的语法时,错误消失了,我现在可以加载列表项了!

    至于为什么会这样,也许有人可以为我们提供比我更多的信息?我所知道的是,无论我尝试什么,甚至在 Sharepoint Hosted 加载项而不是 Provider Hosted 上进行测试,在我更改此代码之前它都无法正常工作。现在,我可以在 Web 端的 Provider Hosted 加载项上使用跨域库来使用 Sharepoint JSOM。这对我来说非常有价值,尤其是在使用 Publishing Images 时,因为 REST 在这方面的限制。我知道在我的情况下我可以使用 REST,但不幸的是,REST 调用不适用于我的情况,因为我还获得了一个发布图像列 (Type="Image") 并且使用 REST 你必须制作一个单独调用每个列表项。

    我真的希望这可以帮助我所在位置的其他人,并节省我花费在追踪此问题上的时间!

    干杯!

    保罗

    【讨论】:

      猜你喜欢
      • 2015-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-09
      • 1970-01-01
      相关资源
      最近更新 更多