从SharePoint 2013提供程序托管的App中的列表中检索数据

时间:2014-03-18 09:47:46

标签: sharepoint-2013 csom

我在SharePoint 2013中开发了一个提供程序托管应用程序。正如您所知,Visual Studio创建了Web应用程序和SharePoint应用程序。 Web应用程序托管在IIS和SharePoint网站集中的SharePoint App中。我尝试使用CSOM从SharePoint托管的列表中获取数据。但我得到了不安全的内容错误。

" [已屏蔽]' https://localhost:44302/Pages/Default.aspx?SPHostUrl=http%3A%2F%2Fecontent&0319c41%2Eecontent%2Eelibrary%2Eapps%2Elocal%2FSharePointApp2%5Fsingeltest' 是通过HTTPS加载的,但运行的不安全内容来自' http://apps-892db5a0319c41.econtent.elibrary.apps.local/sharepointapp2_singeltest/_layouts/15/AppWebProxy.aspx':此内容也应通过HTTPS加载。"

这是我在Default.aspx中的代码

<script type="text/javascript" src="../Scripts/jquery-1.8.2.js"></script>
<script type="text/javascript" src="../Scripts/MicrosoftAjax.js"></script>
<script type="text/javascript" src="../Scripts/SP.Core.js"></script>
<script type="text/javascript" src="../Scripts/INIT.JS"></script>
<script type="text/javascript" src="../Scripts/SP.Runtime.js"></script>
<script type="text/javascript" src="../Scripts/SP.js"></script>
<script type="text/javascript" src="../Scripts/SP.RequestExecutor.js"></script>

<script type="text/javascript" src="../Scripts/App.js"></script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>

    <form id="form1" runat="server">
    <div>
        <input id="Button1" type="button" value="Get title via CSOM" onclick="execCSOMTitleRequest()" /> <br />
        <input id="Button2" type="button" value="Get Lists via CSOM" onclick="execCSOMListRequest()" />
    </div>
        <p ID="lblResultTitle"></p><br />
        <p ID="lblResultLists"></p>
    </form>
</body>
</html>

和App.js是:

var hostwebUrl;
var appwebUrl;

// Load the required SharePoint libraries
$(document).ready(function () {
    //Get the URI decoded URLs.
    hostwebUrl =
        decodeURIComponent(
            getQueryStringParameter("SPHostUrl")
    );
    appwebUrl =
        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 + "/MicrosoftAjax.js",
    //   function () {
    //       $.getScript(scriptbase + "SP.Core.js",
    //           function () {
    //               $.getScript(scriptbase + "INIT.JS",
    //                   function () {
    //                       $.getScript(scriptbase + "SP.Runtime.js",
    //                           function () {
    //                               $.getScript(scriptbase + "SP.js",
    //                                   function () { $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest); }
    //                                   );
    //                           }
    //                           );
    //                   });
    //           });
    //   });
});

function execCrossDomainRequest() {
    alert("scripts loaded");
}
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];
    }
}

function execCSOMTitleRequest() {
    var context;
    var factory;
    var appContextSite;
    var collList;
    //Get the client context of the AppWebUrl
    context = new SP.ClientContext(appwebUrl);
    //Get the ProxyWebRequestExecutorFactory
    factory = new SP.ProxyWebRequestExecutorFactory(appwebUrl);
    //Assign the factory to the client context.
    context.set_webRequestExecutorFactory(factory);
    //Get the app context of the Host Web using the client context of the Application.
    appContextSite = new SP.AppContextSite(context, hostwebUrl);
    //Get the Web
    this.web = context.get_web();
    //Load Web.
    context.load(this.web);
    context.executeQueryAsync(
        Function.createDelegate(this, successTitleHandlerCSOM),
        Function.createDelegate(this, errorTitleHandlerCSOM)
        );
    //success Title
    function successTitleHandlerCSOM(data) {
        $('#lblResultTitle').html("<b>Via CSOM the title is:</b> " + this.web.get_title());
    }
    //Error Title
    function errorTitleHandlerCSOM(data, errorCode, errorMessage) {
        $('#lblResultLists').html("Could not complete CSOM call: " + errorMessage);
    }
}

function execCSOMListRequest() {
    var context;
    var factory;
    var appContextSite;
    var collList;
    //Get the client context of the AppWebUrl
    context = new SP.ClientContext(appwebUrl);
    //Get the ProxyWebRequestExecutorFactory
    factory = new SP.ProxyWebRequestExecutorFactory(appwebUrl);
    //Assign the factory to the client context.
    context.set_webRequestExecutorFactory(factory);
    //Get the app context of the Host Web using the client context of the Application.
    appContextSite = new SP.AppContextSite(context, hostwebUrl);
    //Get the Web
    this.web = context.get_web();
    // Get the Web lists.
    collList = this.web.get_lists();
    //Load Lists.
    context.load(collList);
    context.executeQueryAsync(
        Function.createDelegate(this, successListHandlerCSOM),
        Function.createDelegate(this, errorListHandlerCSOM)
        );
    //Success Lists
    function successListHandlerCSOM() {
        var listEnumerator = collList.getEnumerator();
        $('#lblResultLists').html("<b>Via CSOM the lists are:</b><br/>");
        while (listEnumerator.moveNext()) {
            var oList = listEnumerator.get_current();
            $('#lblResultLists').append(oList.get_title() + " (" + oList.get_itemCount() + ")<br/>");
        }
    }
    //Error Lists
    function errorListHandlerCSOM(data, errorCode, errorMessage) {
        $('#lblResultLists').html("Could not complete CSOM Call: " + errorMessage);
    }
};

感谢任何解决方案。

1 个答案:

答案 0 :(得分:0)

首先,我可以告诉你你的decodeURIComponent逻辑不起作用。

此致:https://localhost:44302/Pages/Default.aspx?SPHostUrl=http%3A%2F%2Fecontent&0319c41%2Eecontent%2Eelibrary%2Eapps%2Elocal%2FSharePointApp2%5Fsingeltest

我的:https://localhost:44302/Pages/Default.aspx?SPHostUrl=http://econtent&0319c41.econtent.elibrary.apps.local/SharePointApp2_singeltest

其次,HostURL内容应该是不安全的吗?如果是这样,那么您可能想要更改浏览器设置。如果没有,那么你需要了解为什么你的HostURL在匿名区域,但你的AppURL在安全区。

在任何一种情况下,请确认每个人至少具有您尝试从中提取的位置的读取权限。

最后一件事是,如果您有权访问管理中心,则设置可信主机位置。

以下是我以前测试的代码段:

    $(document).ready(function () {
    $.getScript(qsHostUrl + "/_layouts/15/SP.RequestExecutor.js", getHostInfo);

    function getHostInfo() {

        var ctxApp = new SP.ClientContext(qsAppUrl);
        var factory = new SP.ProxyWebRequestExecutorFactory(qsAppUrl);
        ctxApp.set_webRequestExecutorFactory(factory);
        var ctxHost = new SP.AppContextSite(ctxApp, qsHostUrl);
        var web = ctxHost.get_web();
        ctxApp.load(web);

        ctxApp.executeQueryAsync(
            Function.createDelegate(this, getHostInfoSuccess),
            Function.createDelegate(this, getHostInfoError)
        );

        function getHostInfoSuccess(sender, args) {
            lblData.html(
                'Title: ' + web.get_title() + '<br/>' +
                'Description: ' + web.get_description()
            );
        }
        function getHostInfoError(sender, args) {
            lblData.html(
                'Request Failed: ' + args.get_message() + '\n' +
                'Stacktrace: ' + args.get_stackTrace()
            );
        }
    }
}