Redirect by Group in Custom Routing

@JoshMohr,

I'm sorry, my bad. I should've told you that the code needs to be slightly changed.

Please use the below code. It should list group members in the console. Please add your site URL and check that the group name is correct.

var Web = new Web("{your_site_URL}");
Web.siteGroups.getByName('Mitglieder von TestJosh').users.get().then(function(result) {
var usersInfo = "";
for (var i = 0; i < result.length; i++) {
usersInfo += "Title: " + result[i].Title + " ID:" + result[i].Id + "";
}
console.log(usersInfo);
}).catch(function(err) {
alert("Group not found: " + err);
});

Finally! This is it! Everything is working as expected now.

Thank you so much for investing that much time for helping me.

1 Like

I am so sorry to come back to this but I just checked the functionality back then and it worked. Now I wanted to implement it on a few forms but it always redirected me to the default form even if I returned another Form set Id. My finding was after going through the code execution step by step that after it enters following function:

Web.siteGroups.getByName('Mitglieder von TestJosh').users.get().then(function(result) {

this is logged:Custom function has returned incorrect result.

After this the code is executed like expected but just ignores the returned form set id. So it works but somewhere there is returned something and this throws the error and after that the actual result of the code is completely ignored.

You can try it yourself by using the sample code you send me. You will see after it enters the function the error is logged and regardeless what form set id you return, you get redirected to the default form set.

So again my fault or is this a bug? What can I do?

@JoshMohr,

Could you please share your complete code which you use in routing to test it.

And would be helpful if you could send the screenshot of the error you get in the console.

Thank you!

Of course! Here you go:

My Routing Code:

var Web = new Web("https://ravensburger.sharepoint.com/sites/TestJosh");
Web.siteGroups.getByName('Mitglieder von TestJosh').users.get().then(function(result) {
var usersInfo = "";
for (var i = 0; i < result.length; i++) {
usersInfo += "Title: " + result[i].Title + " ID:" + result[i].Id + "";
}
console.log(usersInfo);
}).catch(function(err) {
alert("Group not found: " + err);
});

And at this point I get the error message. Like you can see I get results and they're correct:

This is the error message:
error

After that the code is executed correctly but if I return a different form set or anything that gets ignored. Tried this on different Sites always same result.

Thank you :slight_smile:

@JoshMohr,

Please use the below code to check if the current user is the member of a group.

var web = new Web('{SiteURL}');
return web.siteGroups.getByName('{GroupName}').users.get().then(function(result) {
  return pnp.sp.web.currentUser.get().then(function(currentUser){  
    for (var i = 0; i < result.length; i++) {
      if(result[i].Email == currentUser.Email){
        return '{FormSetID}';
      }
    } 
  });
}).catch(function(err) {
  alert("Group not found: " + err);
});

This works. Now we have it. Thanks for your help!

1 Like

i am trying to make custom routing work for multiple group below is what i want. I am able to get all the groups and also check if user belong to group separately but i need to check inside the condition which i am not able to achieve so far

var Web = new Web('site URl');
Web.siteGroups.get().then(function(data) {
var groups = "";
for (var i = 0; i < data.length; i++) {
groups += data[i].Title + "\n";
alert(groups); // gives all groups one by one working

// now i need to check if user belong to any of these groups and route with Site Owners being first priority and then group A and Group B if user belong to multiple groups

 if(groups ==  'Site Owners')
 {alert("Admin");

//check if logged in user belong to this group
return '{FormSetID}';

}
else if(groups == 'Group A')
{alert("Group A");
//check if logged in user belong to this group and assignedto field
return '{FormSetID}';
}
else if(groups == 'Group B')
{alert("Group B");
//check if logged in user belong to this group and assignedto field
return '{FormSetID}';
}
}
});

Hello @ShantiBhattarai,

You can get all groups of Current User, check if a user is a member of a specific group and redirect to a corresponding form using the following code.

To redirect a user to the appropriate Form Set by priority, you need to create an array and check if the array stores the desired group name.

return web.currentUser.get()
	.then(function(user){
  		return web.siteUsers.getById(user.Id).groups.get()
			.then(function(groupsData){
				var groups = [];
                //Check if the user is a member of the desired groups and add them to the array
				for (var i = 0; i < groupsData.length; i++) {
					if(groupsData[i].Title == 'Site Owners') {
						groups.push('Site Owners');
					}
					
					if(groupsData[i].Title == 'Group A') {
						groups.push('Group A');
       	 			}
				}

                //Redirect to a Form Set if the user is a member of a group
				if (groups.indexOf('Site Owners') > 0) {
					return 'FormSetIdForSiteOwners';
				}
				else if (groups.indexOf('Group A') > 0) {
					return 'FormSetIdForGroupA';
				}
			});
	});

thank you i will check it

i tried the following code it gives all the group people belong to but always goes in else loop even the user belong to site owner group

return web.currentUser.get()
.then(function(user){
return web.siteUsers.getById(user.Id).groups.get()
.then(function(groupsData){
var groups = [];
//Check if the user is a member of the desired groups and add them to the array
for (var i = 0; i < groupsData.length; i++) {
if(groupsData[i].Title == 'Site Owners') {
groups.push('Site Owners');
}

				if(groupsData[i].Title == 'Group A') {
					groups.push('Group A');
   	 			}
			}

            //Redirect to a Form Set if the user is a member of a group
			if (groups.indexOf('Site Owners') > 0 && user.Id == item.AssignedToId) { // here i want to check if user belong to Assigned to Field too
                           alert("Site Owners");
				return 'FormSetIdForSiteOwners';
			}
			else if (groups.indexOf('Group A') > 0 && user.Id == item.AssignedToId) {
                              alert("Group A ");
				return 'FormSetIdForGroupA';
			}
		});
});

@ShantiBhattarai,

Please replace FormSetIdForGroupA and FormSetIdForSiteOwners in the code with the form set IDs.

image

Once the condition is met, the user will be redirected to the corresponding form, and the code execution will be stopped.

i have group id in FormSetID i just didnt posted that code with id here problem is it doesnt go to first loop at all it always goes to second loop no matter what i am not able to alert SIte Owners it always gives Group A when i am in both the groups

@ShantiBhattarai,

Please share the complete code that you are using.

return web.currentUser.get()
.then(function(user){
return web.siteUsers.getById(user.Id).groups.get()
.then(function(groupsData){
var groups = [];
//Check if the user is a member of the desired groups and add them to the array
for (var i = 0; i < groupsData.length; i++) {
if(groupsData[i].Title == 'Site Owners') {
groups.push('Site Owners');
}
if(groupsData[i].Title == 'Group A') {
groups.push('Group A');
}
}

        //Redirect to a Form Set if the user is a member of a group
		if (groups.indexOf('Site Owners') > 0 && user.Id == item.AssignedToId) { 
                       alert("Site Owners");
			return ' 73910c7b-dc27-4244-9995-e997d3e7dc22';
		}
		else if (groups.indexOf('Group A') > 0 && user.Id == item.AssignedToId) {
                          alert("Group A ");
			return ' d28c627d-35ab-41a1-823c-052db01d3a94';
		}
	});

});

@ShantiBhattarai,

The values form the form is not accessible in the code above. Please use the following code to check if the current user is selected in the 'AssignedTo' field.

Also, make sure that AssignedTo is the internal name of the field in the field.

var groups = [];

return web.currentUser.get()
	.then(function(user){
  		return web.siteUsers.getById(user.Id).groups.get()
			.then(function(groupsData){
				
				for (var i = 0; i < groupsData.length; i++) {
					if(groupsData[i].Title == 'Site Owners') {
						groups.push('Site Owners');
					}
					
					if(groupsData[i].Title =='Group A') {
						groups.push('Group A');
       	 			}
				}
			return item.get();
			})
                .then(function(item) {

					if (groups.indexOf('Site Owners') >= 0 && user.Id == item.AssignedToId)) {
						return '73910c7b-dc27-4244-9995-e997d3e7dc22';
					}
					else if (groups.indexOf('Group A') >= 0 && user.Id == item.AssignedToId)) {
						return 'd28c627d-35ab-41a1-823c-052db01d3a94';
					}
				});
	})

i further debug the code and
groups.indexOf('Site Owners') is returned -1 even i belong to that group and i tried to site Members group and it returned 0. what might be the reason?

@ShantiBhattarai,

indexOf returns the index of the string in the array. Sorry, I forgot that it could be 0. I've updated the conditions accordingly:

groups.indexOf('Site Owners') >= 0

Кegarding the Site Owners group. Maybe there is a typo in the group name or you need to define web as in this post.

To see all groups in which you are a member, please use the below code in Custom Routing. It will alert all groups in which you are a member on form load.

var userGroups = [];

return web.currentUser.get()
	.then(function(user){
  		return web.siteUsers.getById(user.Id).groups.get()
			.then(function(groupsData){
				
				for (var i = 0; i < groupsData.length; i++) {
                    userGroups.push(groupsData[i].Title);

				}
			    
                alert(userGroups);
			});
	});

// with this code i am able to route to forms depending on assigned to field

if (item) {
var user;
return web.currentUser.get()
.then(function(u) {
user = u;
return item.get();
})
.then(function(item) {
if (user.Id == item.AssignedToId) {
return 'form1';
}
else
{
return 'form2';
}

	});

}

// with this code i am able to get all the group i belong to
return web.currentUser.get()
.then(function(user){
return web.siteUsers.getById(user.Id).groups.get()
.then(function(groupsData){

			for (var i = 0; i < groupsData.length; i++) {
                userGroups.push(groupsData[i].Title);

			}
		    
            alert(userGroups);
            alert(user);
		   return item.get(); 
        
	});  
  
   
	});

but when i try to combine both to check if i belong to group and assigned to field it is not working

Finally Figured it out there was one extra bracket in this line of code after item.AssignedToId

working perfectly now and i am able to route to any form depending on group and priority. Thank you so much for all the support and guidance :slight_smile:

2 Likes