SharePoint 2013: Get current user group collection through Rest API

Introduction

Initially we have to obtain the current user of SharePoint group they belong to, which can be achieved using SharePoint 2013 REST API.

Let's proceed

When we use /_api/web/CurrentUser and /_api/web/GetUserById("+UserID+")/Groups , we get the current user’s group collection . Right now I am using a work around that iterates through the (big) result set. To get the group for the current Login User follow below steps:

Step 1

Navigate to your SharePoint 2013 site.

Step 2

From this page select the Site Actions | Edit Page.

Edit the page, go to the "Insert" tab in the Ribbon and click the "Web Part" option. In the "Web Parts" dialogue, go to the "Media and Content" category, select the "Script Editor" Web Part and click the "Add button".

Step 3

Once the Web Part is inserted into the page, you will see an "EDIT SNIPPET" link; click it. You can insert the HTML and/or JavaScript as in the following.

  1. <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.1.min.js"></script>  
  2. <script type="text/javascript">  
  3.     $(document).ready(function () { getCurrentUser(); });  
  4.     function getCurrentUser() {  
  5.         getUserWebPermissionREST();  
  6.         $.ajax({  
  7.             url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/CurrentUser",  
  8.             method: "GET",  
  9.             headers: { "Accept""application/json; odata=verbose" },  
  10.             success: function (data) {  
  11.                 getCurrentUserGroupColl(data.d.Id);  
  12.             },  
  13.             error: function (data) {  
  14.                 failure(data);  
  15.             }  
  16.         });  
  17.   
  18.     }  
  19.     function getCurrentUserGroupColl(UserID) {  
  20.         $.ajax  
  21.         ({  
  22.             url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetUserById("   + UserID + ")/Groups",  
  23.             method: "GET",  
  24.             headers: { "Accept""application/json; odata=verbose" },  
  25.             success: function (data) {  
  26.                 /* get all group's title of current user. */  
  27.                 var results = data.d.results; var InnrHtmlgrp = "<ul>";  
  28.                 for (var i = 0; i < results.length; i++) {  
  29.                     InnrHtmlgrp += "<li>" + results[i].Title + "</li>";  
  30.                 }  
  31.                 $("#Group").append(InnrHtmlgrp + "</ul>");  
  32.             }  
  33.         });  
  34.     }  
  35. </script>  
  36.   <strong>security Group Name:</strong>  
  37.       <div id="Group"></div>  
Final Output Result: