eca*_*eca 7 javascript sharepoint cross-domain
我有一个托管在服务器上的网页,比如说http://SVR1/path/index.html,我希望访问托管在另一台服务器上的本地SharePoint网站上的一些列表项,比如说http://SVR2/sites/mySite/.
我正在使用的SharePoint的当前安装(不在我的控制下)不允许部署SharePoint托管的应用程序,也不允许部署托管的托管应用程序,因此我尝试使用SharePoint跨域库从纯粹访问所需的列表项外部HTML5/JS/CSS3页面.作为用户,我对SharePoint网站中的列表具有完全访问权限,因此我认为阅读其项目应该没有问题.
按照此处的示例,我的页面如下:
<!doctype html>
<html>
<head>
<!-- Title -->
<title>Application Template</title>
<script language="javascript" type="text/javascript" src="js/jquery.js"></script>
<script language="javascript" type="text/javascript">
var hostweburl = "http://SVR2/sites/mySite";
var appweburl = "http://SVR1/path";
// Load the required SharePoint libraries
$(document).ready(function () {
$("#renderList").html("Requesting Lists...");
// 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.RequestExecutor.js", execCrossDomainRequest);
});
///////////////////////////////////////////////////////
// Function to prepare and issue the request to get
// SharePoint data
function execCrossDomainRequest() {
// executor: The RequestExecutor object
// Initialize the RequestExecutor with the app web URL.
var executor = new SP.RequestExecutor(appweburl);
// Issue the call against the host web.
// To get the title using REST we can hit the endpoint:
// hostweburl/_api/web/lists/getbytitle('listname')/items
// The response formats the data in the JSON format.
// The functions successHandler and errorHandler attend the
// sucess and error events respectively.
executor.executeAsync(
{
url: hostweburl + "/_api/web/lists",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: successHandler,
error: errorHandler
}
);
}
///////////////////////////////////////////////////////
// Function to handle the success event.
// Prints the data to the page.
function successHandler(data) {
var jsonObject = JSON.parse(data.body);
var listsHTML = "";
var results = jsonObject.d.results;
for (var i = 0; i < results.length; i++) {
listsHTML = listsHTML +
"<p><h1>" + results[i].Title +
"</h1>" + results[i].Body +
"</p><hr>";
}
document.getElementById("renderList").innerHTML =
listsHTML;
}
///////////////////////////////////////////////////////
// Function to handle the error event.
// Prints the error message to the page.
function errorHandler(data, errorCode, errorMessage) {
document.getElementById("renderList").innerText =
"Could not complete cross-domain call: " + errorMessage;
}
</script>
</head>
<body>
<h1 id="Root Page" style="text-align:center;">This is the home page</h1>
<div id="renderList">Placeholder</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
当我在浏览器中加载页面时,在javascript控制台中出现错误:"未捕获错误:无效字段或参数requestInfo.url.".
我的印象是问题出现在变量的内容中appweburl,在我发现的所有示例中,SharePoint都是由URL提供的,作为URL中查询部分的一部分.但这意味着提供商托管的应用程序已部署在SharePoint中 - 我无法做到 - 并且此应用程序正在调用其远程托管的对应方.
所以问题是:是否可以在完全在SharePoint外部的页面中使用SharePoint跨域库,如果是,我应该如何设置hostweburl,appweburl以及可能有其他东西可以访问SharePoint列表?
提前致谢.
你提到的两个URL,appwebUrl和hostwebUrl确实是为在应用程序中使用而设计的,所以我认为你不应该使用跨域库。
如果您只是使用非应用程序方式连接怎么办?例如,您可以调用自定义 Web 服务(在共享点之外),该服务可以通过 CSOM 连接到您的 SP 站点,或者您可以直接在 JavaScript 中执行此操作。
| 归档时间: |
|
| 查看次数: |
6469 次 |
| 最近记录: |