我正在使用JavaScript开发SharePoint RESTful API。使用 _spPageContextInfo 时,它不起作用。
错误:未捕获的ReferenceError:未定义_spPageContextInfo
这是我使用的代码
<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>
...
答案 0 :(得分:0)
你可以使用如下。
让当前用户登录::
<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));
}
答案 1 :(得分:0)
您可以使用Angular JS和SharePoint Javascript对象模型尝试此代码:
<script type="text/javascript">
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope, $location) {
var userid = _spPageContextInfo.userId;
var requestUriGetUserDetails = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
$scope.contentTypeTemplate = "application/json;odata=verbose";
$scope.requestHeadersTemplate = { "accept" : "application/json;odata=verbose" };
$.ajax({
url : requestUriGetUserDetails,
contentType : $scope.contentTypeTemplate,
headers : $scope.requestHeadersTemplate,
success : onSuccessGetUserDetails,
error : onErrorGetUserDetails
});
function onSuccessGetUserDetails(data, request){
var varFullName = data.d.Title;
var varEmail = data.d.Email;
var varLoginName = data.d.LoginName;
$scope.loginName = varLoginName;
$scope.email = varEmail;
$scope.requestorEmail = varEmail;
$scope.requestorFullName = varFullName;
var fullNameArray = varFullName.split(" ");
var firstNameValue = fullNameArray[0];
var lastNameValue = fullNameArray[1];
$scope.firstName = firstNameValue;
$scope.lastName = lastNameValue;
$scope.$apply();
}
function onErrorGetUserDetails(error) {
alert(error);
}
});
</script>
&#13;
您可以参考此博客,了解使用SharePoint Javascript对象模型开发Angular JS的分步说明。
请点击here获取博客链接。