sharepoint office 365托管插件中的Contextinfo错误,其中包含Web部件获取列表标题 [英] Contextinfo error in sharepoint office 365 hosted addin with web part getting list titles

查看:97
本文介绍了sharepoint office 365托管插件中的Contextinfo错误,其中包含Web部件获取列表标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在Visual Studio 2013中编写的SharePoint Hosted Add-in,它有一个部署到SharePoint Office 365站点的Web部件。网页部分在页面上显示正常,但当我尝试从网站获取列表标题时,我收到有关contextinfo的错误。



我设置了AppManifest。 xml有以下权限



范围:网页

许可:管理





这是web部分.aspx文件中的脚本代码



 $( document )。ready( function ()
{
hostweburl = decodeURIComponent(getQueryStringParameter(< span class =code-string>' SPHostUrl'));
appweburl = decodeURIComponent(getQueryStringParameter(' SPAppWebUrl'));

loadDependentScripts();
});


// 检索查询字符串值的函数。
// 出于生产目的,您可能希望使用
// 用于处理查询字符串的库。
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 loadDependentScripts()
{
var scriptbase = hostweburl + / _ layouts / 15 /;

// 加载js文件并继续使用successHandler
$ .getScript(scriptbase + SP.Runtime.js
function ()
{
$ .getScript(scriptbase + SP.js
function (){$ .getScript(scriptbase + SP.RequestExecutor.js,getHostWebListsUsingREST);}
);
}
);
}


function getHostWebListsUsingREST()
{
var 执行者;

// 虽然我们从主机网络获取数据,但SP.RequestExecutor已初始化使用应用程序Web URL ..
executor = new SP.RequestExecutor(appweburl);
executor.executeAsync(
{
url:appweburl + / _ api / SP .AppContextSite(@target)/ web / lists /?@ target =' + hostweburl + '
方法: 获取
标题: { 接受 application / json; odata = verbose},
成功:onGetHostWebListsUsingRESTSuccess,
错误:onGetHostWebListsUsingRESTFail
}
);
}

function onGetHostWebListsUsingRESTSuccess(data)
{
var jsonObject = JSON .parse(data.body);
var lists = jsonObject.d.results;
var listsHtml = $ .each(lists, function (index,list)
{
$(' #list')。append(list.Title + + list.ItemCount + < span class =code-string>)< br />);
});
}

function onGetHostWebListsUsingRESTFail(data,errorCode,errorMessage)
{
alert(' 无法获取主机站点。错误:' + errorMessage);
}





HTML没什么好看的



 <   body  >  
< div >
< p id = message < span class =code-keyword>>
<! - 运行应用时,以下内容将替换为用户名 - 请参阅App.js - >
初始化...
< / p >
< / div >
< div >
< p id = results >
< / p >
< / div >
< / body >





我得到的错误如下'sp.runtime.js'



第2行未处理的异常,https列60952 ://myhost.sharepoint.com/_layouts/15/sp.runtime.js



0x800a138f - JavaScript运行时错误:无法设置属性'https:// myhost未定义或空引用的.sharepoint.com / sites / Developer / Ven / TestSharePointAppPart / Pages / TestSharePointAppPart / _api / contextinfo'



来自脚本的警告说



'无法获得主机站点:错误:禁止'



我尝试过:



我在AppManifest.xml中尝试了不同的权限,我在登录时获得了在网站上创建任何内容的权限。

解决方案

document )。ready( function ( )
{
hostweburl = decodeURIComponent(getQueryStringParameter(' SPHostUrl'));
appweburl = decodeURIComponent(getQueryStringParameter(' SPAppWebUrl'));

loadDependentScripts();
});


// 检索查询字符串值的函数。
// 出于生产目的,您可能希望使用
// 用于处理查询字符串的库。
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 loadDependentScripts()
{
var scriptbase = hostweburl + / _ layouts / 15 /;

// 加载js文件并继续到successHandler

.getScript(scriptbase + SP.Runtime.js
function ()
{


.getScript(scriptbase + SP.js
function (){

I've got an SharePoint Hosted Add-in written in Visual Studio 2013 that has a web part that is deployed to a SharePoint Office 365 site. The web part surfaces fine on the page but when I try to get list titles from the site I get an error about the contextinfo.

I've set the AppManifest.xml to have the following Permissions

Scope: Web
Permission: Manage


Here is the script code in the web part .aspx file

$(document).ready(function ()
{
    hostweburl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
    appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));

    loadDependentScripts();
});


// Function to retrieve a query string value.
// For production purposes you may want to use
// a library to handle the query string.
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 loadDependentScripts()
{
    var scriptbase = hostweburl + "/_layouts/15/";

    // Load the js files and continue to the successHandler
    $.getScript(scriptbase + "SP.Runtime.js",
        function ()
        {
            $.getScript(scriptbase + "SP.js",
                function () { $.getScript(scriptbase + "SP.RequestExecutor.js", getHostWebListsUsingREST); }
                );
        }
    );
}


function getHostWebListsUsingREST()
{
    var executor;

    // although we're fetching data from the host web, SP.RequestExecutor gets initialized with the app web URL..
    executor = new SP.RequestExecutor(appweburl);
    executor.executeAsync(
        {
            url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists/?@target='" + hostweburl + "'",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: onGetHostWebListsUsingRESTSuccess,
            error: onGetHostWebListsUsingRESTFail
        }
    );
}

function onGetHostWebListsUsingRESTSuccess(data)
{
    var jsonObject = JSON.parse(data.body);
    var lists = jsonObject.d.results;
    var listsHtml = $.each(lists, function (index, list)
    {
        $('#lists').append(list.Title + "(" + list.ItemCount + ")<br />");
    });
}

function onGetHostWebListsUsingRESTFail(data, errorCode, errorMessage)
{
    alert('Failed to get host site. Error:' + errorMessage);
}



HTML nothing fancy

<body>
    <div>
        <p id="message">
        <!-- The following content will be replaced with the user name when you run the app - see App.js -->
        initializing...
        </p>
    </div>
    <div>
        <p id="results">
        </p>
    </div>
</body>



The errors I'm getting are as follows in the 'sp.runtime.js'

Unhandled exception at line 2, column 60952 in https://myhost.sharepoint.com/_layouts/15/sp.runtime.js

0x800a138f - JavaScript runtime error: Unable to set property 'https://myhost.sharepoint.com/sites/Developer/Ven/TestSharePointAppPart/Pages/TestSharePointAppPart/_api/contextinfo' of undefined or null reference

And the alert from the script says

'Failed to get host site: Error:Forbidden'

What I have tried:

I've tried different permissions in the AppManifest.xml and I've got permissions on my login to create anything in the site.

解决方案

(document).ready(function () { hostweburl = decodeURIComponent(getQueryStringParameter('SPHostUrl')); appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl')); loadDependentScripts(); }); // Function to retrieve a query string value. // For production purposes you may want to use // a library to handle the query string. 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 loadDependentScripts() { var scriptbase = hostweburl + "/_layouts/15/"; // Load the js files and continue to the successHandler


.getScript(scriptbase + "SP.Runtime.js", function () {


.getScript(scriptbase + "SP.js", function () {


这篇关于sharepoint office 365托管插件中的Contextinfo错误,其中包含Web部件获取列表标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆