![](/img/trans.png)
[英]SharePoint Add-IN JSOM Create List Item Error. “this” is undefined
[英]Error trying to access Sharepoint Online host web list data from provider hosted add-in part using JSOM CDL
我已經在互聯網上四處尋找答案,但仍然無法解決。 有許多示例與我要執行的操作很接近,但實際上並沒有確切的示例。 這是我想做的事情:
我有提供者托管的Sharepoint加載項,該加載項具有一個帶有C#代碼隱藏的aspx頁面。 我還在提供程序托管端(網絡端,而不是應用程序網)中有一個javascript文件,我正嘗試使用該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.$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 Developer工具逐步執行代碼,但是很難閱讀模糊的Microsoft代碼。 任何幫助弄清楚這一點將不勝感激! 提前非常感謝!
保羅
我終於可以通過使用此代碼並從此方法解決問題,因為它在我運行時就起作用了。 我終於找到問題所在了:
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調用不適用於我的情況,因為我還獲得了Publishing Image列(Type =“ Image”),並且要使用REST,您必須每個列表項的單獨調用。
我真的希望這可以幫助其他人找到我的位置,並節省您花費在跟蹤問題上的時間!
干杯!
保羅
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.