使用JSOM CDL

时间:2017-05-02 03:20:01

标签: javascript sharepoint sharepoint-online

我已经在互联网上寻找答案,但仍然无法理解。关于我试图做的事情有许多例子,但实际上没有一个是准确的。这就是我要做的事情:

我有一个托管的Sharepoint加载项,它有一个带有C#codebehind的aspx页面。我还在提供商托管方(Web端,而不是app-web)中有一个javascript文件,我试图用来从主机网访问一些List数据。

以下是我使用的代码:

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)

我尝试创建一个名为&#34;的测试&#34;并且只使用了'Include(Title)'但我收到了同样的错误。我尝试插入debugger;并使用Chrome开发者工具逐步执行代码,但很难阅读混淆的Microsoft代码。任何有关解决这个问题的帮助都将非常感激!非常感谢你!

1 个答案:

答案 0 :(得分:0)

我终于能够通过使用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加载项上使用Cross Domain Library来使用Sharepoint JSOM。这对我来说非常有价值,特别是在使用发布图像时,因为REST领域存在局限性。我知道我可以在我的情况下使用REST,但不幸的是,REST调用不适用于我的情况,因为我还得到一个发布图像列(Type =“Image”)并使用REST你必须制作一个单独调用每个列表项。

我真的希望这可以帮助我的位置上的其他人,并节省我花在跟踪它的时间!

干杯!