1

Background infomation

In my company's Sharepoint website, each user account included two account infomations: user ID (DomainName\StaffID) and Display Name (DepartmentID/StaffName).
Now I need to create a list, in which each user can input his private infomation (Name, StaffID, DepartmentID...)

Problem
My Manager has a request: each user only can view all the infomation that related his deparment.
My idea is applying a filter that get current user DISPLAY NAME (which included DepartmentID) and compare each list item's DepartmentID field. if (matched) then show item. When I use [Me] value, it doesn't return user Display Name, but return user ID.
I've already searched many many times but no result. At the moment, I cant't apply code to my Sharepoint website, because i'm being a newbie in this field. if you have any solution, please talk to me.
Thanks all

1 Answer 1

0

I think you can try any of below 2 options. below is high level logic. you might need take care of code in detail while implementing.

Option 1 1. Edit your default view and add content editor webpart 2. Add some script to this content editor webpart. Idea would be to get current user display name which in your case would be "DepartmentID/StaffName"

var currentusername = $().SPServices.SPGetCurrentUser({ fieldName: "Title", debug: false });

assuming above gave you "1231321\Siddharth"

var seprate = currentusername.split("\\");
var deparmentid = currentusername[0];
if(querystring is empty) // take care of syntax here
{
window.location.href= window.location.href + "?FilterField1=DeparmentId&FilterValue1=" + deparmentid;
}

Note - "DeparmentId" should be the field name in your custom list, the query string parameters name and values are case sensitive so take care of it.

Explanation - Basically here we are finding current logged in users deparement id and then passing it to list view to filter data based on deparement id...one disadvantage here you will see some lag because user will be redirected again on same page. make sure content editor web part is first in sequence so that before list view is loaded redirect happens.

Option 2 - Use CSOM code to create custom view. Above code can be reffered to get current logged in users department id. use this departement id to pass in caml query filter.

<script type="text/javascript" src="/Documents/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="/Documents/jquery.SPServices-0.7.2.min.js"></script>

<script language="javascript" type="text/javascript">

$(document).ready(function() {
      $().SPServices({
        operation: "GetListItems",
        async: false,
        listName: "Video",
        CAMLQuery: "<Query><Where><Eq><FieldRef Name='DepartmentId' /><Value Type='Choice'>" + deparmentid + "</Value></Eq></Where></Query>", // here deparmentid is variable which should have value of current logged in user department id
        CAMLViewFields: "<ViewFields>  <FieldRef Name='DepartmentId' /> </ViewFields>", //Fetch other fields which you want.
        completefunc: function (xData, Status) {
          $(xData.responseXML).SPFilterNode("z:row").each(function() {

           var liHtml = "<li>" + $(this).attr("ows_Title") + "</li>";
               $("#tasksUL").append(liHtml);

          });
        }
      });
    });

</script>
<ul id="tasksUL"/>

Option 3 - Create custom webpart using server object model and use listview control to find custom data...

2
  • I've spend serveral days to understand your answer. Finally, I understanded and using javascript to insert filter value to list view successful. thanks for your help.
    – GIANGPZO
    Commented Dec 20, 2016 at 4:30
  • :)....glad it helped...!!! my long answer finally paid of to me also in term of 15 points :) do upvote also, happy coding!! Commented Dec 20, 2016 at 4:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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