Sharepoint 2013:我无法访问主机站点

时间:2015-07-01 16:58:06

标签: javascript sharepoint

Sharepoint 2013:我想遍历mu sharepoint在线网站上的所有列表。但是,当我加载主机端并调用executeQueryAsync函数时,它似乎总是调用faliure函数,即我得到一个警告“失败”作为输出

var context = SP.ClientContext.get_current();
var user = context.get_web().get_currentUser(); 
var hostUrl
var hostContext
var site
(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 () {

        $('#Add_List').click(function(event)
        {
        //  alert("in Add_List ")
   hostUrl=decodeURIComponent(getQueryStringParameter("SPHostUrl"));
        alert("host URL:"+hostUrl);

        hostContext=new SP.AppContextSite(context,hostUrl)
        site=hostContext.get_web();
        context.load(site)
        context.executeQueryAsync(success,failure)

    })
  });


})();

function getQueryStringParameter(param)
{
 var params=document.URL.split("?")[1].split("&")
//alert(""+params);
for (var i = 0; i < params.length; i = i + 1) {
     var singleParam = params[i].split("=");
     alert(""+singleParam[i])
     if (singleParam[0] == param) {
         alert("if")
         return singleParam[1];

     }
 }
}
function success()
{
 alert("in success")
} 
function failure()
 {
alert("in failure")
}

1 个答案:

答案 0 :(得分:0)

您需要跨站点库 ProxyWebRequestExecutorFactory ,并且应用清单必须请求主机网络的读取权限。

对于代码,您可以这样使用库:

context = new SP.ClientContext(appweburl);
factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
context.set_webRequestExecutorFactory(factory);
appContextSite = new SP.AppContextSite(context, hostweburl);

this.web = appContextSite.get_web();
context.load(this.web);

对于请求,您必须在清单

中包含此权限
<AppPermissionRequests>
  <AppPermissionRequest 
    Scope="http://sharepoint/" 
    Right="Read" />
</AppPermissionRequests>

您可以在MSDN site

上找到更多信息(及以上示例)