Redirect by Group in Custom Routing

Oh sorry now its working!
These are the results:

Besitzer von TestJosh
Besucher von TestJosh
Mitglieder von TestJosh
Verteiler

They are also correct!

1 Like

@mnikitina

But what do I have to do to get this working in the custom routing? I mean the Group check in the Javascript editor doesn’t help me with the redirect and this is what I need, because I have to redirect based on Group membership AND field values.

Really big sorry for taking so much from your time!

@JoshMohr,
I’m sorry for the delayed reply.

Currently, there are some restrictions in Routing that doesn’t allow assigning the Web variable and getting the groups you require. We will fix this soon, the fix will be available by the end of July / beginning of August.

If the form is urgently needed, it should be possible to check group membership/field values on the form and hide/show certain fields. But it would be better to wait for a while until custom Routing is updated.

@mnikitina

No problem!

Sounds great :slight_smile: It is not that important right now so I can wait. I’m really looking forward to this because then the routing is really powerfull. Right now I actually would prefer the old one of the Forms Designer. The use case to route per group membership and values is necessary in many of our forms.

A really big “thank you” to you! Helped me a lot! Outstanding support as always :slight_smile:

Do you think its possible that you comment here when the fix is published?

@JoshMohr

We’ll for sure let you know once this will be fixed. :slight_smile:

We really appreciate your feedback. It helped us to see the existing issues. And once we solve them, it’ll make the routing function more convenient and useful.

Thank you! :slight_smile:

@mnikitina is there any update on this? Just checking :sweat_smile:

@JoshMohr,

Thanks for asking and waiting. :slight_smile:

We are still working on the new release. I will write you back once the update is published.

@JoshMohr,

Good news!

We've published the new release with the updates in Routing.

Please make sure that you have the latest App package (1.0.7) and Desktop Application (1.4.6) and try it setup rotating now.

Feel free to ask any questions!

@mnikitina

nice to hear that! I already tried it and can confirm that it works somewhat better. My Findings:
This following code works now in the routing section (did not work before) also with correct groups as result:

var Web = new Web("{your_site_URL}");
Web.siteGroups.get().then(function(data) {
 var groups = "";
 for (var i = 0; i < data.length; i++) {
     groups += data[i].Title + "\n";
 }
 console.log(groups);
});

But this does not solve my requirement as it only lists all groups on the page. This code which should list me all group members is still not working, with the same error:

pnp.sp.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);
});

@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';
			}
		});
});