SharePoint 2013 RESTful API : users and groups

Hello everyone I feel so warm that I can write my whole article in english ! Wow, still not impressed ? I will also write it in french (It shouldn’t be a big deal since it’s my native language) ! Now, let’s go straight ahead into the subject.

 Article en français

Well, recently I played around with the SharePoint 2013 API. Particulary with the users and groups methods. I had to get the current logged in user in SP and check if he belonged to a specific SP group (FYI the SP Admin group). I tried at first to use the classic approach, and to use the CSOM by declaring the ClientContext, then the Web and finally the Site. But, for my particular case, I wanted to do this with an asynchronous request. So I searched and found that the SP 2013 API could easily be called with an Ajax method.

To be able to use my examples, don’t forget to add jQuery to your page, and SP.UserGroup.js.

SP.SOD.executeOrDelayUntilScriptLoaded(functionToCall, 'SP.UserProfiles.js');

By doing this, you will load the SP.UserProfiles.js, thanks to jQuery. Then you’ll specify that your function « functionToCall() » will be executed after the script is loaded. If you are not willing to do this, as the $.getScript is asynchronous, it could happen that the script has not finished to be loaded when your function run. You can also use the method below :


It will add the execution of your function into the « TO-EXECUTE » stack of functions when loading the SharePoint site body.

Here is the examples of what I’ve done :

Get current logged in user :

        url : _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + _spPageContextInfo.userId + ")",
        contentType : "application/json;odata=verbose",
        headers : { "accept" : "application/json;odata=verbose" },
        success : userFound,
        error : userNotFound
    function userFound(data, request){
        loginName = data.d.Title;
        console.log("Welcome ",loginName);
    function userNotFound(error) {

 Check if user belong to a specific group :

function checkAdminRights() {
        var groupId = 7;  // admin group
	var userId = _spPageContextInfo.userId;  // get current user ID
	var requestHeaders = { "accept" : "application/json; odata=verbose" };
		url : _spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups(" + groupId + ")/users/getbyid(" + userId + ")",
		contentType : "application/json;odata=verbose",
		headers : requestHeaders,
		success : userAdmin,
		error : userNotAdmin
	function userAdmin(data, request) {
		var groupName = data.d.LoginName;
	function userNotAdmin(error) {
		console.log("user not admin");
		$(" li.static").each(function() {
			console.log("Found !");
			// if we find the word Administration in the menu title, we hide it. Hack to find what we want, in spite of the unamed (no id, no particular class) left menu nodes.
			if($(this).find("").text().indexOf("Administration") >= 0) {	

For sure I didn’t need the first piece of code as the second one perfectly fits to my needs, but I preferred to write down the steps I’ve followed, in order to keep a trace of the logic and of few pieces of codes. If you need to play around more or manipulate others object types, here is the API architecture picture from the MSDN site. It demonstrates the whole paths you should request to get what you want (personnaly I made my code working by the time I saw this, it’s pretty self-explicit) :



Pages: 1 2

Laisser un commentaire