SharePoint 2013: Get current user group collection through Rest API

Posted: April 27, 2015 in SharePoint2013

Introduction

In this article, we will learn how to get current user group collection through REST API in JQuery.



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&#8221;></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.     function getCurrentUserGroupColl(UserID) {
  19.         $.ajax
  20.         ({
  21.             url: _spPageContextInfo.webAbsoluteUrl + “/_api/web/GetUserById(“   + UserID + “)/Groups”,
  22.             method: “GET”,
  23.             headers: { “Accept”“application/json; odata=verbose” },
  24.             success: function (data) {
  25.                 /* get all group’s title of current user. */
  26.                 var results = data.d.results; var InnrHtmlgrp = “<ul>”;
  27.                 for (var i = 0; i < results.length; i++) {
  28.                     InnrHtmlgrp += “<li>” + results[i].Title + “</li>”;
  29.                 }
  30.                 $(“#Group”).append(InnrHtmlgrp + “</ul>”);
  31.             }
  32.         });
  33.     }
  34. </script>
  35.   <strong>security Group Name:</strong>
  36.       <div id=“Group”></div>

Final Output Result:

Advertisements
Comments
  1. shaibya says:

    Its not working..even site feature “minimal download strategy ” is deactivated already. Even though it is not displaying current user’s share point group names.Please help.

    • Hi shaibya,

      my script is working fine. Can you please share me your script so that I can see through it and find the solution.

      $(document).ready(function () { getCurrentUser(); });
      function getCurrentUser() {
      getUserWebPermissionREST();
      $.ajax({
      url: _spPageContextInfo.webAbsoluteUrl + “/_api/web/CurrentUser”,
      method: “GET”,
      headers: { “Accept”: “application/json; odata=verbose” },
      success: function (data) {
      getCurrentUserGroupColl(data.d.Id);
      },
      error: function (data) {
      failure(data);
      }
      });
      }
      function getCurrentUserGroupColl(UserID) {
      $.ajax
      ({
      url: _spPageContextInfo.webAbsoluteUrl + “/_api/web/GetUserById(“ + UserID + “)/Groups”,
      method: “GET”,
      headers: { “Accept”: “application/json; odata=verbose” },
      success: function (data) {
      /* get all group’s title of current user. */
      var results = data.d.results; var InnrHtmlgrp = “

        ”;
        for (var i = 0; i < results.length; i++) {
        InnrHtmlgrp += “

      • ” + results[i].Title + “
      • ”;
        }
        $(“#Group”).append(InnrHtmlgrp + “

      ”);
      }
      });
      }

      security Group Name:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s