Page 1 of 1

Conditionally hide/disable fields

Posted: 02 Sep 2020
by Roo
Is it possible (on the online version) to conditionally hide or disable fields based on the sharepoint group the user is in?

I'm trying to get around the issues of multiple forms as I have up to 7 different user groups using the same edit form.

Re: Conditionally hide/disable fields

Posted: 02 Sep 2020
by mnikitina
Hello Roo,

You can try out the code from this thread:

viewtopic.php?p=3801#p3801

Re: Conditionally hide/disable fields

Posted: 03 Sep 2020
by Roo
Does this not refer back to the multiple forms though? I'm trying to make fields in one form only accessible if the user belongs to a certain group

Re: Conditionally hide/disable fields

Posted: 03 Sep 2020
by mnikitina
Hello Roo,

Yes, but you can adapt the code from this example to enable/disable fields. For instance:

Code: Select all

function IsCurrentUserMemberOfGroup(groupName, OnComplete) {

        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(groupName);
        context.load(group);
        
        var groupUsers = group.get_users();
        context.load(groupUsers);

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

                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;
        }
}
    
    IsCurrentUserMemberOfGroup('Main Owners', function (isCurrentUserInGroup) {
    //if the user is definitely in the group
    if(isCurrentUserInGroup){
        //disable field if user is a group member
        fd.field('Title').readonly(true);
    }
    else {
        //enable field if user is not a group member
        fd.field('Title').readonly(false);
    }
    });

Re: Conditionally hide/disable fields

Posted: 10 Sep 2020
by Roo
Thanks Margarita

That works well

Re: Conditionally hide/disable fields

Posted: 10 Sep 2020
by Roo
Is it possible to use more than one group?
If so is this the correct format? (Something isn't right as the form doesn't show correctly)

Code: Select all

function IsCurrentUserMemberOfGroup(groupName, OnComplete) {

        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(groupName);
        context.load(group);
        
        var groupUsers = group.get_users();
        context.load(groupUsers);

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

                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;
        }
}
    
    IsCurrentUserMemberOfGroup('Contract Coordinator', function (isCurrentUserInGroup) {
    //if the user is definitely in the group
    if(isCurrentUserInGroup){
        //disable field if user is a group member

		fd.field('Environment_x0020_Approved').readonly(true);
		fd.field('Safety_x0020_Approved').readonly(true);
		fd.field('Environment_x0020_Manager').readonly(true);
		fd.field('Environment_x0020_Sign_x0020_Off').readonly(true);
		fd.field('Safety_x0020_Officer').readonly(true);
		fd.field('Safety_x0020_Sign_x0020_Off_x002').readonly(true);
		fd.field('_x0043_oC1').readonly(true);		
    }
    else {


    }
    });
	
	IsCurrentUserMemberOfGroup('Environment Manager', function (isCurrentUserInGroup) {
    //if the user is definitely in the group
    if(isCurrentUserInGroup){

		fd.field('Safety_x0020_Approved').readonly(true);
		fd.field('Safety_x0020_Officer').readonly(true);
		fd.field('Safety_x0020_Sign_x0020_Off_x002').readonly(true);
		fd.field('Environment_x0020_Approved').readonly(false);
		fd.field('Safety_x0020_Approved').readonly(true);
		fd.field('Environment_x0020_Manager').readonly(false);
		fd.field('Environment_x0020_Sign_x0020_Off').readonly(false);
		

    else {

    }
    });



fd.updateDroppedDocuments($('.ProDoc'), function(listItem) {
    //get the values from the Form fields:
    var supplier = fd.field('Supplier_x0020_Name').value() ;
	var prefix = Date.now()/ 1000 | 0;
	var pextn = listItem.get_fieldValues().File_x0020_Type
	var newName = "ProDoc" + " " + prefix + supplier + "." + pextn;
	
	 //file name with the current directory:
    var newFileName = listItem.get_item('FileDirRef') + '/' + newName;
	//set the values on uploaded document:
    listItem.set_item('Contractor', supplier);
		listItem.update();
    listItem.update();
	listItem.get_file().moveTo(newFileName);
});

Re: Conditionally hide/disable fields

Posted: 11 Sep 2020
by mnikitina
There is a syntax error in the code, and I would suggest checking the membership of a user sequentially. Please see the updated part of a code below.

Code: Select all

IsCurrentUserMemberOfGroup('Contract Coordinator', function (isCurrentUserInGroup) {
    //if the user is definitely in the group
    if(isCurrentUserInGroup){
        //disable field if user is a group member

		fd.field('Environment_x0020_Approved').readonly(true);
		fd.field('Safety_x0020_Approved').readonly(true);
		fd.field('Environment_x0020_Manager').readonly(true);
		fd.field('Environment_x0020_Sign_x0020_Off').readonly(true);
		fd.field('Safety_x0020_Officer').readonly(true);
		fd.field('Safety_x0020_Sign_x0020_Off_x002').readonly(true);
		fd.field('_x0043_oC1').readonly(true);		
    }
    else {
        
        IsCurrentUserMemberOfGroup('Environment Manager', function (isCurrentUserInGroup) {
            //if the user is definitely in the group
            if(isCurrentUserInGroup){

                fd.field('Safety_x0020_Approved').readonly(true);
                fd.field('Safety_x0020_Officer').readonly(true);
                fd.field('Safety_x0020_Sign_x0020_Off_x002').readonly(true);
                fd.field('Environment_x0020_Approved').readonly(false);
                fd.field('Safety_x0020_Approved').readonly(true);
                fd.field('Environment_x0020_Manager').readonly(false);
                fd.field('Environment_x0020_Sign_x0020_Off').readonly(false);

            }
        });
    }
});

Re: Conditionally hide/disable fields

Posted: 11 Sep 2020
by Roo
Is it possible to use a case statement instead?

I've got as many as 7 separate groups

Re: Conditionally hide/disable fields

Posted: 14 Sep 2020
by mnikitina
Roo,

Yes, you can. But the logic would be the same as you need to call IsCurrentUserMemberOfGroup function for each specific group.