How to show specific views depending on the current user group in a SharePoint list

There is a way to show and redirect users to specific views depending on their group, by modifying the "AllItems.aspx" page.
Firstly, create  library within your site called JqueryLibraries and put there the jquery-1.9.0.js and the jquery.SPServices-0.7.2.js libraries.

You can download the jquery.SPServices-0.7.2.js from here:
http://spservices.codeplex.com/releases/view/81401 (unzip the file and use the .js version)

You can download the jquery-1.9.0.js from here:
http://code.google.com/p/google-caja/source/browse/trunk/third_party/precajole/jquery/1.9.0/?r=5248

Create the views where you want the user to be redirected (i.e: Your1View.aspx and Your2View.aspx).
Make a security copy of the "AllItems.aspx" page, delete everything in the original page and paste the following:

<%@ Page Language="C#" %>
<html dir="ltr">

<script language="javascript" src="/SiteName/JqueryLibraries/jquery-1.9.0.js" type="text/javascript"></script>
<script language="javascript" src="/SiteName/JqueryLibraries/jquery.SPServices-0.7.2.js" type="text/javascript"></script>
<script type="text/javascript">

$(document).ready(function() {

    //Get current username
    userName = $().SPServices.SPGetCurrentUser({
        fieldName: "Name",
        debug: false
    });

    //get user's group
    $().SPServices({
        operation: "GetGroupCollectionFromUser",
        userLoginName: userName,
        async: false,
        completefunc: function(xData, status){
var found=false;
            $(xData.responseXML).find("Group").each(function(){
                if(status == "success"){
                    var GroupName = $(this).attr('Name');
                    if(GroupName=="GroupNameToSeeYour2View"){
          found=true;
                    }
                 }else{
                    alert("error");
                }
                                 if (found ==false)
            window.location.replace("/SiteName/Lists/ListName/Your1View.aspx");
            else 
            window.location.replace("/SiteName/Lists/ListName/Your2View.aspx");

            });
            
        }
       
    });
    });
</script>
</html>

If you want to restrict the views to specific groups go to your view (i.e: Your2View.aspx), look for <IsIncludedFilter />  and replace with:

<IsIncludedFilter>;;;;GroupNameToSeeTheView</IsIncludedFilter>

Users not part of the "GroupNameToSeeTheView" will not see the content web part.

Change the fields in red according to your Site/Group/List/View names.
In this case we only need to redirect two user groups, so we use the found variable to check if the current user belong to that specific group or belong to other (any), depending if it is true or false the user will be redirected to one view or other.

If you have more than 2 groups to compare consider using a count variable instead of  the found variable and use a switch case to redirect the view instead of the if statement.

Comments

Popular posts from this blog

How to convert fields in lower case/upper case within Info path forms

How to auto save a browser enabled info path form every X minutes in Sharepoint 2007, 2010 and 2013

Auto-populate the person/Group picker from the current user on InfoPath 2007/2010/2013