I am using this code example to redirect a user based on group, but I am facing this error message:

TypeError: allGroups.getByName is not a function

my code:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"> 

 <script type="text/javascript" src="/_layouts/1033/init.js"></script>
 <script  type="text/javascript" src="/_layouts/15/MicrosoftAjax.js"> 
 <script  type="text/javascript" src="/_layouts/15/sp.core.js"></script>

    function IsCurrentUserMemberOfGroup(groupName, OnComplete) {
          console.log('CheckCurrentUerGroup called');
        var currentContext = new SP.ClientContext.get_current();
     console.log(currentContext );
        var currentWeb = currentContext.get_web();

           var currentUser = currentContext.get_web().get_currentUser();


        var allGroups = currentWeb.get_siteGroups();

        var group = allGroups.getByName(groupName);

        var groupUsers = group.get_users();


        function OnSuccess(sender, args) {
            var userInGroup = false;
            var groupUserEnumerator = groupUsers.getEnumerator();
            while (groupUserEnumerator.moveNext()) {
                var groupUser = groupUserEnumerator.get_current();
                if (groupUser.get_id() == currentUser.get_id()) {
                    userInGroup = true;

        function OnFailure(sender, args) {

function CheckCurrentUerGroup() 
console.log('CheckCurrentUerGroup called')
alert('CheckCurrentUerGroup called');
  IsCurrentUserMemberOfGroup("Students", function (isCurrentUserInGroup) {
        //Redirect to a certain page
        window.location = "https://www.youtube.com";


function testExecFunc() {

SP.SOD.executeFunc('sp.js', 'SP.ClientContext',  CheckCurrentUerGroup());


How can I resolve this? Have been looking around for a while but not been successful. If i need to provide more info, please note

  • Which version of SharePoint you are using? Do you want to use JSOM only or are you open to use REST api as well? – Ganesh Sanap Sep 10 '19 at 13:03
  • I am using Modern Script Editor on Sharepoint Online. If this aint working i am open to use REST as well – user3660293 Sep 10 '19 at 13:08

Try Using below SharePoint REST API Code:

function isCurrentUserMemberOfGroup(groupName) {
    var isUserInGroup = false;
        async: false,
        headers: { "accept": "application/json; odata=verbose" },
        method: "GET",
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/currentuser/groups",
        success: function (data) {
            data.d.results.forEach( function (value) {
                if (value.Title == groupName) {               
                     isUserInGroup = true;
        error: function (response) {
    return isUserInGroup;

How to Use:

var isAdmin = isCurrentUserMemberOfGroup("Site Owners");

if(isAdmin) {
    window.location = "http://www.yourSiteOwnerurl.com";    // If user is a Site Owner
} else {
    window.location = "http://www.yourSiteMemberurl.com";   // If user is not a Site Owner

How do I redirect with JavaScript?

| improve this answer | |
  • should I replace webAbsoluteUrl with our tenant site? – user3660293 Sep 10 '19 at 13:46
  • Not needed. It will automatically fetch the current site URL. If it is not working for you then you can use the current site URL where you are adding this code like https://tenant.sharepoint.com/sites/sitename. – Ganesh Sanap Sep 10 '19 at 13:49
  • can you please help with applying your code in my redirecting logic? so If current user is part of "Site Owners" , redirect to ***.com , otherwise ***.com – user3660293 Sep 10 '19 at 13:56
  • Check updated answer. you can use reference link on how to redirect to another site using javascript. – Ganesh Sanap Sep 10 '19 at 14:03
  • thanks. however i ran into a new problem, due do the redirecting It doesnt give me a chance to edit the code snipper on the page.. it immediately redirects the page.. how can I resolve this? I want to edit the code. thanks in advnace – user3660293 Sep 10 '19 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.