如何在 sharepoint 应用程序中使用 _spPageContextInfo [英] How to use _spPageContextInfo in sharepoint Application

查看:47
本文介绍了如何在 sharepoint 应用程序中使用 _spPageContextInfo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 JavaScript 开发 SharePoint RESTful API.使用 _spPageContextInfo 时它不起作用.

I am working on SharePoint RESTful API using JavaScript. when using _spPageContextInfo it doesn't work.

错误:未捕获的引用错误:_spPageContextInfo 未定义

Error: Uncaught ReferenceError: _spPageContextInfo is not defined

这是我使用的代码

<html xmlns="http://www.w3.org/1999/xhtml">

<script src="../Scripts/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $.getJSON(_spPageContextInfo.webServerRelativeUrl + "/_api/web/currentuser",
            function (data) {
                $("#message").text('hello' + data.d.Title);
            });
    });
</script>

...

推荐答案

你大概可以如下使用.

获取当前用户登录::

<script type="text/javascript">
var userid;
try{
$(document).ready(function () {
//This will give the 'userId' by which we can get the user's info from the url as /_api/web/getuserbyid(userid)
userid = _spPageContextInfo.userId;
var siteurl = _spPageContextInfo.webAbsoluteUrl;
var urlUser = siteurl + "/_api/web/getuserbyid(" +userid+")";
$.ajax({
url : urlUser,
contentType : "application/json;odata=verbose",
headers : { "accept" : "application/json;odata=verbose" },
success : onsuccess,
error : onerror
});
});
}
catch(ex){alert(ex);}

function onsuccess(data, textStatus, jqXHR){
alert(data.d.Title);
alert(data.d.LoginName);
alert(data.d.Email);
alert(data.d.IsSiteAdmin);
}

function onerror(errorMessage){
alert(JSON.stringify(errorMessage));
}

这篇关于如何在 sharepoint 应用程序中使用 _spPageContextInfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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