I want to redirect users from a landing page to another page in SharePoint 2010, based on their group membership. I need to do this using JavaScript (ECMAScript) and not server side code.
8 Answers
You can get the group collection of a site and enumerate over it to find a specific group. You can then enumerate the members of the group to find the current user.
function onGetSharePointGroup() {
var context = SP.ClientContext.get_current();
var groupsEnum = this.groupCollection.getEnumerator();
while (groupsEnum.moveNext()) {
var group = groupsEnum.get_current();
if (group.get_title() == "GROUP NAME TO FIND") {
this.members = group.get_users();
context.load(members);
}
}
if (members != null) {
context.executeQueryAsync(Function.createDelegate(this, this.FindCurrentUser), Function.createDelegate(this, this.onFailure));
}
}
In this approach it is determined whether User
belongs to specific Group
and if so, then he/she is getting redirected to another page:
function isUserMemberOfGroup(userId, groupId, success,error) {
var ctx = SP.ClientContext.get_current();
var allGroups = ctx.get_web().get_siteGroups();
var group = allGroups.getById(groupId);
ctx.load(group,'Users');
ctx.executeQueryAsync(
function(sender, args) {
var userInGroup = findUserById(group.get_users(),userId);
success(userInGroup);
},
error);
var findUserById = function(users,id){
var found = false;
var e = group.get_users().getEnumerator();
while (e.moveNext()) {
var user = e.get_current();
if (user.get_id() == id) {
found = true;
break;
}
}
return found;
};
}
Usage
(function(){
var currentUserId = _spPageContextInfo.userId;
var groupId = 4;
var defaultPageUrl = '/en/Pages/default.aspx';
isUserMemberOfGroup(currentUserId, groupId,
function (isCurrentUserInGroup) {
window.location.replace(defaultPageUrl); //redirect
},
function(sender,args){
//Error handling goes here..
});
})();
You've got a plenty of answer how to check permissions. If you want to check the group membership (it is of course not the same as permissions), then you have to check which groups the current user is member of and decide where to redirect.
Alternative 1 I have written a little example how to achieve this using jsom. Suppose you have a group called "Owners" and you want to redirect them to chuvash.eu (just for demo):
var desiredGroupTitle = "Owners";
var context = SP.ClientContext.get_current()
var web = context.get_web()
var user = web.get_currentUser()
var groups = user.get_groups()
context.load(groups, 'Include(Id, Title)');
context.executeQueryAsync(function() {
var groupCount = groups.get_count();
for(var i = 0; i < groups.get_count(); i++) {
var group = groups.itemAt(i);
var groupTitle = group.get_title();
if (groupTitle === desiredGroupTitle) {
window.location.assign("http://chuvash.eu");
}
}
}, function() {
console.error("failed to check group membership for current user");
});
Alternative 2 Use REST api to get the current user's group membership. Here is the endpoint:
/_api/web/CurrentUser/Groups?$select=Id,Title
Here is a rest call using SP.RequestExecutor.js:
var desiredGroupTitle = "Owners";
var webUrl = _spPageContextInfo.webAbsoluteUrl;
var url = webUrl + '/_api/web/CurrentUser/Groups?$select=Id,Title';
var sodKey = 'sp.requestexecutor.js';
if (!_v_dictSod[sodKey]) {
SP.SOD.registerSod(sodKey, '/_layouts/15/sp.requestexecutor.js');
}
SP.SOD.executeFunc(sodKey, "SP.RequestExecutor", function () {
var executor = new SP.RequestExecutor(webUrl);
executor.executeAsync(
{
url: url,
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function(response) {
var data = JSON.parse(response.body);
var groups = data.d.results;
for (var i = 0; i < groups.length; i++) {
if(groups[i].Title === desiredGroupTitle) {
window.location.assign("http://chuvash.eu");
}
}
},
error: function() {
console.error("failed to check group membership for current user");
}
}
);
});
You could wrap your redirect inside a SPSecurityTrimmedControl
<Sharepoint:SPSecurityTrimmedControl runat="server" PermissionsString="ViewListItems">
...Put your redirect in here...
</SharePoint:SPSecurityTrimmedControl>
Here are the values you can use for PermissionString.
If you want to find users that do NOT have a certain permission, this code would work better:
<script>
var allowUserOnPage=false;
</script>
<Sharepoint:SPSecurityTrimmedControl runat="server" PermissionsString="ViewListItems">
<script>
allowUserOnPage=true;
</script>
</SharePoint:SPSecurityTrimmedControl>
<script>
if (!allowUserOnPage) {
...Put your redirect in here...
}
</script>
-
I'm afraid this won't work for my scenario. I need it to be based on SharePoint group membership, not a permission string. Commented May 25, 2011 at 4:10
-
3Welcome to SharePoint! While this may theoretically answer the question, we prefer inclusion of the essential parts of the answer here, and to provide the link for reference. See How to Answer for general guidelines. Commented Sep 9, 2011 at 15:48
You can use a content editor web part for your script then use audience targeting on the webpart to determine what groups should see it.
You can get the groups in client side Silverlight app by calling the GetGroupCollectionFromUserAsync method of SharePoint's UserGroup.asmx web service. check out the link below for sample code.
http://www.innovawiz.com/get-the-sharepoint-groups-a-user-is-part-of-using-sharepoint-object-model
-
3Welcome to SharePoint.SE! Please read the comment I left on @spdailytips's answer above or see How to Answer for general guidelines. We recommend including the code sample in your answer in addition to providing the link. Commented Apr 16, 2012 at 17:34
You can actually do this using a generic HttpHandler and call from the client browser. Will share you some code sample later.