Wednesday 19 June 2013

How Do I Restrict Views of a Custom List by Group in Sharepoint 2007?

SPServices - http://spservices.codeplex.com/
In sharepoint, create a non-public library called "Jquery libraries" and upload your jquery and SPServices to it.
In sharepoint designer, create a copy of AllItems.aspx.
In sharepoint designer, edit the file AllItems.aspx. Clear all the code and insert this javascript code.
        <script language="javascript" type="text/javascript" src="PATH-TO-YOUR-JQUERY-FILE"></script>
<script language="javascript" type="text/javascript" src="PATH-TO-YOUR-SPSERVICES-FILE"></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){

            $(xData.responseXML).find("Group").each(function(){
                if(status == "success"){
                    var nomeGrupo = $(this).attr('Name');
                    //if user is in group 1 redirect to page 1. If user is in group 2 redirect to page 2 etc...
                    if(nomeGrupo=="Grupo sergio"){
                             window.location.replace("PATH-TO-PAGE-1/SomeItems.aspx");
                    }else{
                        window.location.replace("/PATH-TO-PAGE-2/ViewAll.aspx");
                    }

                }else{
                    alert("Falha na comunicação com o Sharepoint");
                }
            });
        }
    });
});
</script>
This code will redirect the user to other pages that will contain customised web-parts-view.
In sharepoint design, rename the AllItems.aspx to SomeItems.aspx (for example).
You can create multiples copys of this file and follow the next step to customise it.
Edit this file and delete the main webpart located at PlaceHolderMain.(Tip: u can click and delete if u are in split view(code an design) in sharepoint designer).
Then, you can insert a custom web part view for your list. In this custom view, you can filter data or not display some colums.

Download JQuery and SPServices libraries and place them in a read only document library or in your 14 Hive, whichever suits you. Then edit the NewForm.aspx of the list (with SP Designer) and add in references to the two files.
Add a script tag with the following:
$(document).ready(function() {
    Admin_Group = "My Group Name";
    if(IsGroupMember(Admin_Group))
    {
        $('input[title="Assigned To"]').parent().parent().css("display","none");
    }
});


function IsGroupMember(GroupName)
{
    var isGroupMember = false;
    $().SPServices({
            operation: "GetGroupCollectionFromUser",
            userLoginName: $().SPServices.SPGetCurrentUser(),
            async: false,
            completefunc: function(xData, Status) {
              if($(xData.responseXML).find("Group[Name='" + GroupName + "']").length == 1)                
              {
                  isGroupMember = true;
              }
            }
    });
    return isGroupMember;
}
You might need to check the input selector is correctly accessing the assignedto or what ever field you need to hide but I've used this approach successful in many situations. Make sure the field you hide isn't a required field. Also remember to hide this in the EditForm.aspx too if thats what you need.

Hi I managed to hide a field programmatically see below. The Approve field is now hidden in the edit screen of the list
SPField Appprove = bdcList.Fields.GetField("Approved");
                        Appprove.ShowInEditForm = false;
                        Appprove.ShowInDisplayForm = true;
                        Appprove.ShowInNewForm = false;
                        Appprove.Update();

No comments:

Post a Comment