Question

as per my requirements

  • Check current user is present in manager group
  • If not present Check status columns
  • If status column consist value other than Requested then stop user to Save item

so for that I have used PreSaveAction() function. I have achieved my requirement here but not It stop save item in list even if Member is available in manager group

Here is screen shot of List Item enter image description here

Here is my code

 var j = jQuery.noConflict(); 
 function PreSaveAction() {

    var context = new SP.ClientContext.get_current();
    var currentWeb = context.get_web();

    var currentUser = context.get_web().get_currentUser();
    context.load(currentUser);

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

    var group = allGroups.getByName('Marketing');
    context.load(group);

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

    context.executeQueryAsync(
            function(sender, args) {
               var userInGroup = IsUserInGroup(currentUser,group);   

            if(userInGroup)
               {
                alert('avaliable in group');
                return true; 

               }
             else
               {
                var txtTitle = j(":input[title='Status']").val(); 

                if(txtTitle != 'Requested')
                { 
                  alert("Please Select other status ");
                  return false; 
                 }
                else
                { 
                 return true; 

                }

               alert('Not avaliable in group');

               }

            },
            function OnFailure(sender, args) {
               alert('Not Avaliable');
                var userInGroup = IsUserInGroup(currentUser,group);   
               alert("Not");
            });


    function IsUserInGroup(user,group)
    {
        var groupUsers = group.get_users();
        var userInGroup = false;
        var groupUserEnumerator = groupUsers.getEnumerator();
        while (groupUserEnumerator.moveNext()) {
            var groupUser = groupUserEnumerator.get_current();
            if (groupUser.get_id() == user.get_id()) {
                userInGroup = true;

                break;

            }
        }
        return userInGroup;
    }
}   
Was it helpful?

Solution

Its because you are doing it async, instead of returning return true, send in the form using SPClientForms.ClientFormManager.SubmitClientForm

Its already available on the form variable created by SP:

window.form.SubmitClientForm()

If you are using PreSaveAction, you'll need to return false on the initial save to avoid multiple "saves":

Example:

window.PreSaveAction = function() { 
    SP.SOD.executeFunc("sp.js", "SP.ClientContext", function() { 

        var context = SP.ClientContext.get_current();
        context.load(context.get_web(), "Title");
        context.executeQueryAsync(function() { 
                form.SubmitClientForm();

        }, function(s,args) { console.log(args.get_message()) })

    });


    return false; 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top