Sharepoint - How to change 'Welcome Page' on the basis of logged in user or group?
It's not possible OOB, but you can do the following:
- Create blank welcome page and put script editor web part on that page
- Put below script into the script editor:
function AutoRedirect() {
var clientContext = new SP.ClientContext.get_current();
var currentUser = clientContext.get_web().get_currentUser();
clientContext.load(currentUser);
var userGroups = currentUser.get_groups();
clientContext.load(userGroups);
clientContext.executeQueryAsync(function(){
var groupsEnumerator = userGroups.getEnumerator();
while (groupsEnumerator.moveNext()) {
var group= groupsEnumerator.get_current();
if(group.get_title() == "Group 1") {
window.location.href = _spPageContextInfo.webAbsoluteUrl + "/Pages/page1.aspx"
break;
}
//etc...
}
}, function(err){
alert(err);
});
}
SP.SOD.executeOrDelayUntilScriptLoaded(AutoRedirect,'sp.js');
The code above checks if user belongs to a SharePoint group and automatically redirects to preconfigured page. This script is more efficient, becasue it doesn't iterate over all groups and users, that's why working faster.
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;
}
}
Usage:
//Usage
function DoesCurrentUserBelongToGroup()
{
IsCurrentUserMemberOfGroup("Group A", function (isCurrentUserInGroup) {
if(isCurrentUserInGroup)
{
location.href = "https://siteurl/pages/Page1.aspx";
}
});
IsCurrentUserMemberOfGroup("Group B", function (isCurrentUserInGroup) {
if(isCurrentUserInGroup)
{
location.href = "https://siteurl/pages/Page2.aspx";
}
});
}
ExecuteOrDelayUntilScriptLoaded(DoesCurrentUserBelongToGroup, 'SP.js');
There is no OOTB way to do that, what you can do is add a script editor webpart/content editor webpart on the default home page of portal and use above code to redirect users based on which group they belong to.
Reference - Check if user belongs to group
This is not only possible but I've done exactly as you describe completely out of the box, without a shred of JavaScript needed.
Here's the basic approach:
- Create a custom list to use for the redirect.
- A single line of text field houses the landing URL (Title is fine).
- Create a calculated column to where the output generates a simple HTML redirect using the destination the user placed in the Title field.
- Make the calculated column type as number or SP wil will display the code instead of rendering it.
- Change permission on list items for each group who has a separate landing page.
- Place the custom list on the welcome page, showing only the calculated column with an item limit of one.
- This allows your Group A/B users to redirect to the secondary page because they can only see the one list item redirecting them to their custom page.