5

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;
    }
}   
8

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; 
}
| improve this answer | |
  • Anders Aune Your solution works for me.. But it save item 2 times.. don't know but I will Figure it out. – Akshay Dattatray Nangare Aug 8 '16 at 13:14
  • Same issue for me item save 2 times how can you avoid ? – Admin Dec 26 '16 at 10:28
  • @Admin updated my answer – Anders Aune Dec 28 '16 at 11:07

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.