SharePoint托管的应用程序无法获取我的列表
-
10-12-2019 - |
题
我正在为SharePoint 2013开发一个SharePoint托管的应用程序(在Office365中),但我是一个问题访问SharePoint托管的应用程序中的列表。
我的代码很好地打印了列表,但它只向我展示“组成的外观”列表和“母版页库”列表,我无法访问我在SharePoint站点中创建的列表。
以及当我尝试访问'联系人的列表(在我部署应用程序的SharePoint站点中存在)时,它会出现此异常:
列表“联系人”在网站上不存在带URL ' https://mysharepinointsite-03ea186502297f.sharepoint.com/sphostedapp12
因此,我的应用程序从另一个站点运行而不是我的主站点,这就是为什么我无法从我的主网站访问现有列表...
有人知道如何在主站点中解决此问题并访问现有列表吗?
非常感谢!
这是我的app.js代码:
'use strict';
var gobbe = window.gobbe || {};
gobbe.Contacts;
gobbe.ContactList = function () {
// private members
var createItem = function (lname, fname, bphone) {
var ctx = new SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle('Contacts');
ctx.load(list);
var listItemCreationInfo = new SP.ListItemCreationInformation();
var newContact = list.addItem(listItemCreationInfo);
newContact.set_item('Last Name', lname);
newContact.set_item('First Name', fname);
newContact.set_item('Business Phone', bphone);
newContact.update();
ctx.executeQueryAsync(success, error);
},
readAll = function () {
// Not implemented
},
readAllSuccess = function () {
// Not implemented
},
updateItem = function () {
// Not implemented
},
updateItem = function (id, lname, fname, bphone) {
// Not implemented
},
removeItem = function (id) {
// Not implemented
},
success = function () {
readAll();
},
error = function (sender, args) {
alert(args.get_message());
}
// public interface
return {
createContact: createItem,
updateContact: updateItem,
deleteContact: removeItem
}
}();
gobbe.Collections = function () {
// private members
var site,
listCollection,
getListCollection = function () {
var ctx = new SP.ClientContext.get_current();
site = ctx.get_web();
ctx.load(site);
listCollection = site.get_lists();
ctx.load(listCollection, 'Include(Title,Id,Fields.Include(Title,Description))');
ctx.executeQueryAsync(success, failure);
},
success = function () {
var html = [];
// List Information
html.push('<ul>');
var listEnumerator = listCollection.getEnumerator();
while (listEnumerator.moveNext()) {
// List Title
html.push('<li>');
html.push(listEnumerator.get_current().get_title());
html.push('<ul>');
// Fields Names
var fieldEnumerator = listEnumerator.get_current().get_fields().getEnumerator();
while (fieldEnumerator.moveNext()) {
html.push('<li>');
html.push(fieldEnumerator.get_current().get_title());
html.push('</li>');
}
html.push('</ul></li>')
}
html.push('</ul>');
// Show results
$('#displayDiv').html(html.join(''));
},
failure = function (sender, args) {
alert(args.get_message());
}
// public interface
return {
execute: getListCollection
}
}();
$().ready(function () {
// Show lists
gobbe.Collections.execute();
// Try to add a contact
gobbe.ContactList.createContact('Cox', 'Brian', '555-555-5555');
alert('Contact Created!');
// Update it
gobbe.ContactList.updateContact(1, 'Cox', 'Brian', '111-111-1111');
alert('Contact Updated!');
// Delete it
gobbe.ContactList.deleteContact(1);
alert('Contact Deleted!');
});
. 解决方案
与主机Web相比,应用程序在不同的域中工作。因此,当您尝试访问App Web中的某些列表时,它会访问其跨域调用。 通常浏览器不允许为安全原因进行这些跨域呼叫。但是,在应用程序中,要与主机Web进行交互,还有一个名为sp.requestexecutor.js的库,这是一个跨域,您可以使用它来制作这些呼叫。 请查看以下链接,这些链接将帮助您制作这些跨域调用以访问应用程序Web内的主机Web数据。
http://msdn.microsoft.com/en-in/library/ fp179927.aspx
http://www.mavention.com/博客/ SharePoint-app-ready-data-from-host-web
希望有帮助。
其他提示
听起来您忘记提供应用程序权限:列表“联系人”在网站上不存在带URL。
当您安装应用程序时,将要求您要求访问特定库或列表的权限。
你将不得不添加这样的东西...... 在App Manifest中标记的样子示例:
<AppPermissionRequests>
<AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web/list" Right="Write"/></AppPermissionRequests>
//Get the URI decoded URLs.
hostweburl =
decodeURIComponent(
getQueryStringParameter("SPHostUrl")
);
appweburl =
decodeURIComponent(
getQueryStringParameter("SPAppWebUrl")
);
.
然后你可以使用这样的东西:
// Initialize the RequestExecutor with the app web URL.
executor = new SP.RequestExecutor(appweburl);
// Issue the call against the host web.
// To get the title using REST we can hit the endpoint:
// app_web_url/_api/SP.AppContextSite(@target)/web/title?@target='siteUrl'
// The response formats the data in the JSON format.
// The functions successHandler and errorHandler attend the
// success and error events respectively.
executor.executeAsync(
{
url:
appweburl +
"/_api/SP.AppContextSite(@target)/web/title?@target='" +
hostweburl + "'",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: successHandler,
error: errorHandler
}
);
.