尝试使用 JSOM CDL 从提供商托管的加载项部分访问 Sharepoint Online 主机 Web 列表数据时出错
Error trying to access Sharepoint Online host web list data from provider hosted add-in part using JSOM CDL
我在网上找遍了这个问题的答案,但还是想不通。有很多例子接近我正在尝试做的事情,但 none 实际上是准确的。这是我正在尝试做的事情:
我有一个提供商托管的 Sharepoint 加载项,它有一个带有 C# 代码隐藏的 aspx 页面。我在提供商托管端(网页端,而不是应用程序网页)也有一个 javascript 文件,我试图用它从主机网页访问一些列表数据。
这是我使用的代码:
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.I_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.W_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 托管加载项进行故障排除,但我遇到了非常相似的错误:
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.I_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.W_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 代码。解决这个问题的任何帮助将不胜感激!提前致谢!
保罗
我终于能够通过使用 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 端的提供商托管加载项上使用跨域库来使用 Sharepoint JSOM。这对我来说非常有价值,尤其是在使用发布图像时,因为 REST 在这方面的局限性。我知道我可以在我的情况下使用 REST,但不幸的是,REST 调用不适合我的情况,因为我还得到一个发布图像列(类型="Image")并且使用 REST 你必须为每个列表项单独调用。
我真的希望这对处于我位置的其他人有所帮助,并为您节省我花在追踪上的时间!
干杯!
保罗
我在网上找遍了这个问题的答案,但还是想不通。有很多例子接近我正在尝试做的事情,但 none 实际上是准确的。这是我正在尝试做的事情:
我有一个提供商托管的 Sharepoint 加载项,它有一个带有 C# 代码隐藏的 aspx 页面。我在提供商托管端(网页端,而不是应用程序网页)也有一个 javascript 文件,我试图用它从主机网页访问一些列表数据。
这是我使用的代码:
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.I_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.W_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 托管加载项进行故障排除,但我遇到了非常相似的错误:
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.I_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.W_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 代码。解决这个问题的任何帮助将不胜感激!提前致谢!
保罗
我终于能够通过使用 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 端的提供商托管加载项上使用跨域库来使用 Sharepoint JSOM。这对我来说非常有价值,尤其是在使用发布图像时,因为 REST 在这方面的局限性。我知道我可以在我的情况下使用 REST,但不幸的是,REST 调用不适合我的情况,因为我还得到一个发布图像列(类型="Image")并且使用 REST 你必须为每个列表项单独调用。
我真的希望这对处于我位置的其他人有所帮助,并为您节省我花在追踪上的时间!
干杯!
保罗