Conditionally hide/disable fields

Discussions about Forms Designer for SharePoint 2013 / 2016 and Office 365.
Locked
User avatar
Roo
Posts: 107
Joined: Tue Oct 24, 2017

02 Sep 2020

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.

User avatar
mnikitina
Posts: 264
Joined: Wed Jun 05, 2019

02 Sep 2020

Hello Roo,

You can try out the code from this thread:

viewtopic.php?p=3801#p3801

User avatar
Roo
Posts: 107
Joined: Tue Oct 24, 2017

03 Sep 2020

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

User avatar
mnikitina
Posts: 264
Joined: Wed Jun 05, 2019

03 Sep 2020

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

User avatar
Roo
Posts: 107
Joined: Tue Oct 24, 2017

10 Sep 2020

Thanks Margarita

That works well

User avatar
Roo
Posts: 107
Joined: Tue Oct 24, 2017

10 Sep 2020

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

User avatar
mnikitina
Posts: 264
Joined: Wed Jun 05, 2019

11 Sep 2020

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);

            }
        });
    }
});

User avatar
Roo
Posts: 107
Joined: Tue Oct 24, 2017

11 Sep 2020

Is it possible to use a case statement instead?

I've got as many as 7 separate groups

User avatar
mnikitina
Posts: 264
Joined: Wed Jun 05, 2019

14 Sep 2020

Roo,

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

Locked
  • Information
  • Who is online

    Users browsing this forum: No registered users and 3 guests