Conditionally hide/disable fields
Hello Roo,
Yes, but you can adapt the code from this example to enable/disable fields. For instance:
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);
}
});
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)
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);
});
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);
}
});
}
});
-
- Information
-
Who is online
Users browsing this forum: No registered users and 15 guests