Articles about European Sharepoint Hosting Service
SharePoint 2013 Hosting – HostForLIFE.eu :: How to Get Current Logged in User and Display Name using SharePoint 2013 REST API.
REST API is quite simple and straightforward, used User ID to get user Title, Email for SharePoint 2013 and apps for SharePoint. use /_api/web/getuserbyid(ID) to get the user field in the response data, we have an “AuthorId” will get us the user Title, Email etc. In this post, I’m gonna tell you how to get current logged in user and display name using SharePoint 2013 REST API.
Follow these steps to get current logged in user and display name using SharePoint 2013 REST API:
Step 1
Navigate to your SharePoint 2013 site and create a Wiki Page or a Web Parts page.
Step 2
Next, add your JavaScript code is quite straightforward:
Edit the page, go to the “Insert” tab in the Ribbon and click the “Web Part” option. In the “Web Parts” picker area, go to the “Media and Content” category, select the “Script Editor” Web Part and press the “Add button”.
Step 3
Once the Web Part is inserted into the page, you will see an “EDIT SNIPPET” link click. You can insert this HTML and/or JavaScript code into the dialog:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<script src="/Style Library/scripts/jquery-1.10.1.min.js"></script> <script type="text/javascript"> var userid= _spPageContextInfo.userId; var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")"; var requestHeaders = { "accept" : "application/json;odata=verbose" }; $.ajax({ url : requestUri, contentType : "application/json;odata=verbose", headers : requestHeaders, success : onSuccess, error : onError }); function onSuccess(data, request){ var Logg = data.d; //get login name var loginName = Logg.LoginName.split('|')[1]; alert(loginName); //get display name alert(Logg.Title); } function onError(error) { alert("error"); } </script> |
In the code above, you’ll get the author id from the list. By passing the author id in to GetUserBuId() method, it will return the user in raw format like “I:O#.f|xxxx|xx@xxxx.com”. Then, you can get the log in name by splitting the output.
SharePoint 2013 Hosting Recommendation
HostForLIFE.eu’s SharePoint 2013 Hosting solution offers a comprehensive feature set that is easy-to-use for new users, yet powerful enough for the most demanding web developer expert. Hosted SharePoint Foundation 2013 is the premiere web-based collaboration and productivity enhancement tool on the market today. With SharePoint 2013 Foundation, you can quickly access and manage documents and information anytime, anywhere though a Web browser in a secure and user friendly way. SharePoint hosting services start at only at €9.99/mo, allowing you to take advantage of the robust feature set for a small business price. HostForLIFE.eu offers a variety of hosted SharePoint Foundation 2013 plans as well as dedicated SharePoint 2013 Foundation options.
Print article | This entry was posted by Matthew Gilbert on April 23, 2015 at 2:46 am, and is filed under European SharePoint 2013 Hosting. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |