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? Sep 10, 2019 at 13:03
  • I am using Modern Script Editor on Sharepoint Online. If this aint working i am open to use REST as well
    – Y_Lakdime
    Sep 10, 2019 at 13:08

1 Answer 1


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?

  • should I replace webAbsoluteUrl with our tenant site?
    – Y_Lakdime
    Sep 10, 2019 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. Sep 10, 2019 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
    – Y_Lakdime
    Sep 10, 2019 at 13:56
  • Check updated answer. you can use reference link on how to redirect to another site using javascript. Sep 10, 2019 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
    – Y_Lakdime
    Sep 10, 2019 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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