使用免费的office365帐户,创建了默认的sharepoint在线网站。在它上面我创建了一个名为“Product”的列表。当我尝试获取所有网站列表时,未列出产品列表。只有2个列表(1)Composed look& (2)主页面图库正在列出。下面是我的App.js代码;
'use strict';
var context = SP.ClientContext.get_current();
var user = context.get_web().get_currentUser();
var web = context.get_web();
var lists = context.get_web().get_lists();
var listItems;
(function () {
// 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 () {
getAllLists();
});
// This function prepares, loads, and then executes a SharePoint query to get
// the current users information
function getAllLists() {
context.load(lists);
context.executeQueryAsync(onGetListsSuccess, onGetListsFail);
}
// This function is executed if the above call is successful
// It replaces the contents of the 'message' element with the user name
function onGetListsSuccess() {
$('#message').text('Hello ' + lists.get_count().toString());
//$('#message').text('Hello ' + web.get_title().toString());
var listEnumerator = lists.getEnumerator();
var selectListBox = document.getElementById("ListItemListBox");
if (selectListBox.hasChildNodes()) {
while (selectListBox.childNodes.length >= 1) {
selectListBox.removeChild(selectListBox.firstChild);
}
}
// Traverse the elements of the collection, and load the name of
// each list into the dropdown list box.
while (listEnumerator.moveNext()) {
var selectOption = document.createElement("option");
selectOption.value = listEnumerator.get_current().get_title();
selectOption.innerText = listEnumerator.get_current().get_title();
selectListBox.appendChild(selectOption);
}
}
// This function is executed if the above call fails
function onGetListsFail(sender, args) {
alert('Failed to get user name. Error:' + args.get_message());
}
})();
答案 0 :(得分:0)
请先了解一下有关sharepoint app的一些概念:
App Web - 部署App的网站
您需要应用程序访问和使用SharePoint组件,例如列表,内容类型,工作流和页面。在本案例中,所有SharePoint组件都应部署在一个单独的SharePoint站点中,称为App Web。
主机网站 - 安装应用程序的网站
部署应用程序的实际站点称为主机Web。
从您的代码中,您通过clientcontext.get_current()方法访问App Web,该方法返回用户正在运行的当前上下文。因此,您只会在列表框中看到两个列表。如果您想访问信息在Host Web中,需要一种不同的方法。您可以使用名为AppSiteContext的对象,下面的代码供您参考:
'use strict';
var web;
var hostcontext;
var lists;
$(document).ready(function () {
var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
var currentcontext = new SP.ClientContext.get_current();
hostcontext = new SP.AppContextSite(currentcontext, hostUrl);
web = hostcontext.get_web();
lists = web.get_lists();
getAllLists();
});
function getAllLists() {
context.load(lists);
context.executeQueryAsync(onGetListsSuccess, onGetListsFail);
}
// This function is executed if the above call is successful
// It replaces the contents of the 'message' element with the user name
function onGetListsSuccess() {
$('#message').text('Hello ' + lists.get_count().toString());
var listEnumerator = lists.getEnumerator();
// Traverse the elements of the collection, and load the name of
// each list into the dropdown list box.
while (listEnumerator.moveNext()) {
alert(listEnumerator.get_current().get_title());
}
}
//this is just the sample function that's in all the MS samples
function getQueryStringParameter(paramToRetrieve) {
var params =
document.URL.split("?")[1].split("&");
var strParams = "";
for (var i = 0; i < params.length; i = i + 1) {
var singleParam = params[i].split("=");
if (singleParam[0] == paramToRetrieve)
return singleParam[1];
}
}
// This function is executed if the above call fails
function onGetListsFail(sender, args) {
alert('Failed to get user name. Error:' + args.get_message());
}