How to use _spPageContextInfo in sharepoint Applic

2019-07-05 02:59发布

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

Error: Uncaught ReferenceError: _spPageContextInfo is not defined

here is code i used

<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>

...

2条回答
Root(大扎)
2楼-- · 2019-07-05 03:19

you can try this code with Angular JS and SharePoint Javascript Object Model:

   <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>

You can refer to this blog for step by step instruction in developing Angular JS with SharePoint Javascript Object Model.

Please click here for the blog link.

查看更多
干净又极端
3楼-- · 2019-07-05 03:23

You can probably use as below.

To get current user logged in ::

<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));
}
查看更多
登录 后发表回答