10

I want to set respective page as welcome page to the logged in user or group, If user go to the site Url.

e.g.

https://siteurl/pages/Page1.aspx - for group A

https://siteurl/pages/Page2.aspx - for group B

When User enter only url till https://siteurl/

Can we acheive this using OOTB or do we need to add some jquery for it.(can not use server side code - using O365)

11

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.

  • 1
    Perfect! Just want to one more thing here, that 'sp.js' does not load for the users who have read only permission. To solve the issue I have used below script in end of the js code: SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady); //Empty function jsut to ensure "sp.js" file loaded function sharePointReady() {} Ref: c-sharpcorner.com/UploadFile/prasham/… – Rishi Jagati Dec 6 '16 at 10:29
  • Very good point! – Sergei Sergeev Dec 6 '16 at 11:25
  • The last answer below uses more standard OOTB SP technologies to allow for any number of groups and not requiring serious scripting – Danny '365CSI' Engelman Feb 10 '17 at 9:03
4
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

3

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:

  1. Create a custom list to use for the redirect.
  2. A single line of text field houses the landing URL (Title is fine).
  3. Create a calculated column to where the output generates a simple HTML redirect using the destination the user placed in the Title field.
  4. Make the calculated column type as number or SP wil will display the code instead of rendering it.
  5. Change permission on list items for each group who has a separate landing page.
  6. Place the custom list on the welcome page, showing only the calculated column with an item limit of one.
  7. 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.
  • +1 because it is the most flexible solution not requiring any serious scripting and maintenance is done using standard OOTB lists – Danny '365CSI' Engelman Feb 10 '17 at 9:02
  • Nice trick indeed! – Sergei Sergeev Feb 10 '17 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.