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>
KGlasier
  • 1,745
  • 1
  • 10
  • 22
Y_Lakdime
  • 113
  • 7
  • Which version of SharePoint are you using? – harshal gite Sep 05 '19 at 14:04
  • @hashal gite Sharepoint Online – Y_Lakdime Sep 05 '19 at 14:06
  • javascript is not supported on Modern Pages. How did you add this? – harshal gite Sep 05 '19 at 14:11
  • by adding Modern Script Editor (https://medium.com/niftit-sharepoint-blog/add-the-script-editor-webpart-back-to-sharepoint-modern-experience-688a7b7208e4) – Y_Lakdime Sep 05 '19 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 05 '19 at 14:19
  • Try using same code in Content Editor/Script Editor web part on Classic SharePoint Page to check if this something related to modern/classic experience. – Ganesh Sanap - MVP Sep 05 '19 at 14:22
  • @KGlasier this didnt resolve it mate – Y_Lakdime Sep 05 '19 at 14:34
  • Try changing SP.SOD.executeFunc('sp.js', CheckCurrentUerGroup); to SP.SOD.executeFunc('sp.js', SP.ClientContext, CheckCurrentUerGroup);. The ClientContext may not be present yet when you're trying to run your own function. – KGlasier Sep 05 '19 at 14:44
  • @KGlasier the error message is gone but the method CheckCurrentUerGroup doesnt seem to be executed :\ – Y_Lakdime Sep 05 '19 at 14:53
  • Looks like I can get it to run with quotations. Please try my solution and get back to me. If it runs without error and you get an alert stating checkcurrentuergroup then you've got a different issue with your code. – KGlasier Sep 05 '19 at 15:07

1 Answers1

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);
KGlasier
  • 1,745
  • 1
  • 10
  • 22