SharePoint 2013 Get current user group collection through Rest api

SharePoint 2013 Get current user group collection through Rest api

In this SharePoint 2013 article we will discuss the easy way to get current user group collection through REST API and JQuery in SharePoint 2013. 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”. Read about Script editor web part SharePoint 2016.



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:

<script src=”http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.1.min.js”></script>

<script type=”text/javascript”>

$(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 = “<ul>”;

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

InnrHtmlgrp += “<li>” + results[i].Title + “</li>”;

}

$(“#Group”).append(InnrHtmlgrp + “</ul>”);

}

});

}

</script>

<strong>security Group Name:</strong>

<div id=”Group”></div>

SharePoint 2013 Get current user group collection through Rest api
SharePoint 2013 Get current user group collection through Rest api


In the above you can see the group names on which the current user belongs to.

Check out Best Alternative to InfoPath -> Try Now

You May Also like the Following SharePoint Online Tutorials:

About Sagar Pardeshi

I am Developer working on Microsoft Technologies for the past 6+years. I am very much passionate about programming and my core skills are SharePoint, ASP.NET & C#,Jquery,Javascript,REST. I am running this blog to share my experience & learning with the community I am an MCP, MCTS .NET & Sharepoint 2010, MCPD Sharepoint 2010, and MCSD HTML 5,Sharepoint 2013 Core Solutions. I am currently working on Sharepoint 2010, MOSS 2007, Sharepoint 2013,Sharepoint 2013 App Dev, C#, ASP.NET, and SQL Server 2008.

View all posts by Sagar Pardeshi →