0

I am facing this error message, referring to this line:

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

I am quite new to this stuff but what is causing this? I want to use a basic redirecting mechanism (from Get user from sharepoint group and redirect) using this code:

<script src="/_layouts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/_layouts/sp.core.js" type="text/javascript"></script>
<script src="/_layouts/sp.runtime.js" type="text/javascript"></script>
<script src="/_layouts/sp.js" type="text/javascript"></script>
<script type="text/javascript">
   $(document).ready(function() {
      alert("test");
   });
<script type="text/javascript">
   alert('2nd');

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

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

      currentContext.load(currentUser);

      var allGroups = currentWeb.get_siteGroups();
      currentContext.load(allGroups);

      var group = allGroups.getByName(groupName);
      currentContext.load(group);

      var groupUsers = group.get_users();
      currentContext.load(groupUsers);

      currentContext.executeQueryAsync(OnSuccess, OnFailure);

      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;
               break;
            }
         }
         OnComplete(userInGroup);
      }

      function OnFailure(sender, args) {
         OnComplete(false);
      }
   }

   function CheckCurrentUerGroup() {
      alert('checkcurrentuergroup');
      IsCurrentUserMemberOfGroup("IT Department", function(isCurrentUserInGroup) {
         if (isCurrentUserInGroup) {
            //Redirect to a certain page
            window.location = "httpd://www.youtube.com";
         }
      });

   }
   //$(document).ready(function() {
   //CheckCurrentUerGroup();
   //
   //});

   SP.SOD.executeFunc('sp.js', CheckCurrentUerGroup);
</script>
10
  • Which version of SharePoint are you using? Sep 5, 2019 at 14:04
  • @hashal gite Sharepoint Online
    – Y_Lakdime
    Sep 5, 2019 at 14:06
  • javascript is not supported on Modern Pages. How did you add this? Sep 5, 2019 at 14:11
  • by adding Modern Script Editor (medium.com/niftit-sharepoint-blog/…)
    – Y_Lakdime
    Sep 5, 2019 at 14:12
  • One issue you may be facing is that you have an extra <script> tag after your `alert('test'); on line 9 of the supplied script. Try removing that and try again.
    – KGlasier
    Sep 5, 2019 at 14:19

1 Answer 1

0

After a bit of testing, I was able to get your script to run on my environment by chancing the execution function from the first line to the second line below.

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

I tried a few options, and looks like you need the quotations in some situations, around the SP.ClientContext in this handle. Please let me know if you have more errors that pop up.

This'll turn your script into this:

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

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

      currentContext.load(currentUser);

      var allGroups = currentWeb.get_siteGroups();
      currentContext.load(allGroups);

      var group = allGroups.getByName(groupName);
      currentContext.load(group);

      var groupUsers = group.get_users();
      currentContext.load(groupUsers);

      currentContext.executeQueryAsync(OnSuccess, OnFailure);

      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;
               break;
            }
         }
         OnComplete(userInGroup);
      }

      function OnFailure(sender, args) {
         OnComplete(false);
      }
   }

   function CheckCurrentUerGroup() {
      alert('checkcurrentuergroup');
      IsCurrentUserMemberOfGroup("IT Department", function(isCurrentUserInGroup) {
         if (isCurrentUserInGroup) {
            //Redirect to a certain page
            window.location = "httpd://www.youtube.com";
         }
      });

   }

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

Your Answer

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

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