Basically, you can do one of two things: have separate form sets for different groups of users or simply hide/show parts of the web page based on the current user’s group. If the difference between the two user-specific views is minor you can go for the latter choice, which is probably your case, so you can skip to step 3 below. You won’t need fd.openForm in that case, you’ll need to read the blogpost at the bottom to see how to show and hide different UI elements.
Otherwise if you want to implement completely different forms for different users, start from step 1.
1. Create an additional form set in Forms Designer by clicking the plus sign (this form set will be used as a redirect destination for a SharePoint group):
We'll implement this functionality for the edit form. Implementing it for Display and New forms would be exactly the same (although filenames of forms would be different).
2. Take a note of the filename at the bottom left corner. You will be redirecting your group members to it.
3. Insert the following code into the JavaScript editor of the form:
Code: Select all
fd.showLoading();
ExecuteOrDelayUntilScriptLoaded(IsCurrentUserWithContributePerms, 'SP.js');
//function that implements the redirect logic
function IsCurrentUserWithContributePerms()
{
//specify the name of the group here instead of “Members”
IsCurrentUserMemberOfGroup("Members", function (isCurrentUserInGroup) {
//if the user is definitely in the group
if(isCurrentUserInGroup){
// then redirect the user somewhere. Replace this filename with the filename of your form to which a member is to be redirected (see the screenshot above)
fd.openForm('fd_Item_767bfb1a-7093-46f0-be37-1cf85c917a63_EditForm.aspx');
}
//here you will have as many if statements as you have user roles
else {
//here is the default choice for users that don’t belong to any groups:
//maybe redirect him/her someplace else - use fd.openForm(..) like above
//or remain on the current page (and show it):
fd.hideLoading();
}
});
}
//function to check if a user belongs to a group (no need to worry about this)
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;
}
}
4. Take a look at the code comments. You will want to specify the name of your group (instead of "Members"), the filename of the form (where it says fd.openForm(...)), implement additional logic for other user roles and possibly implement some default redirect logic or just show the current default form, which is what happens at the moment.
5. Repeat the steps 1-4 for Display and New forms.
If you simply want to hide/show a couple of fields, please read this article
http://spform.com/office-365/cond ... ynamically (you can use the code above to determine if the current user is a member of a group or not).