1

I'm running SharePoint 2013, I would like to have a Current User Filtered View using Javascript.

the [Me] would not be feasible because I'm not using the "People Picker" as the field type. I used a text field in 4 different text fields. For instance, Field1_Text, Field2_Text, Field3_Text and Field4_Text can different Users. The form will have four different approval process; however, I would like to display the form whenever one of those text field has the current user listed. The Current User Filter Web Part, only allows for one field to be selected not all four fields, so that approach won't work either.

A custom view will be used for the date of the item. For instance, "Created Date" Filter = [Today] + 15. The data would only be displayed if the current user is listed on one of those approval "Text_Field" as noted above and the form was created 15 days from [Today]. But, my main concern is the Javascript aspect. I'm trying to get myself up to speed on the topic. Let me know if there is anything else that I can provide. As far as the .JS code, I'm very limited since I'm trying to read and learn from many of the posting on this site.

Thank you.

  • 1
    Could you try to describe what you want to achieve by doing this? Why isn't filtering using [Me] working? – Rune Sperre Mar 18 at 6:37
  • You should update your question to include those details - the more the better! How would you like the data to be displayed, any requirements there? It could be that a custom listing would be best here, but it all really depends on how you want to display it... – Rune Sperre Mar 18 at 18:45
  • You should edit the original question and add the information from the comments, it will be easier for people to understand what you are trying to do... – Rune Sperre Mar 18 at 22:07
  • Ok. Done. Any other suggestions. Thanks. – JuaP Mar 18 at 22:36
0

Add a script editor web part to your view and add the following code. Adjust for column names:

<script type="text/javascript">
(function () { 
    var bodyFieldContext = {}; 
    bodyFieldContext.OnPreRender = preRender;    
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(bodyFieldContext); 
})(); 

function preRender(ctx) {   
    var userName = _spPageContextInfo.userDisplayName;
    console.log(userName);     
    var rows = ctx.ListData.Row;     
    for (var i = rows.length - 1; i >= 0; i--) {
        if ((rows[i].Field1Text != userName) && (rows[i].Field2Text != userName) && (rows[i].Field3Text != userName)) {
            rows.splice(i, 1);
        }
    }
}
</script>

This function will run before the ListViewWebPart is rendered and modify the rows. Any rows that do not match the users display name will be removed.

All items:

enter image description here

With filter:

enter image description here

| improve this answer | |
  • Thank you for the sample. I have followed your instructions and I added another "Field4Text" to the function as listed below. It doesn't filter by "FieldTexts", it only shows the entire list. if ((rows[i].Field1Text!= userName) && (rows[i].Field2Text!= userName) && (rows[i].Field3Text != userName)&& (rows[i].Field4Text != userName)) { rows.splice(i, 1); – JuaP Mar 19 at 14:44
  • Check the console, it is probably throwing an error and thus not modifying the rows. Maybe you didn't update the other column names? – Rune Sperre Mar 19 at 14:51
  • I added screenshots to the answer – Rune Sperre Mar 19 at 15:04
  • <script type="text/javascript"> (function () { var bodyFieldContext = {}; bodyFieldContext.OnPreRender = preRender; SPClientTemplates.TemplateManager.RegisterTemplateOverrides(bodyFieldContext); })(); – JuaP Mar 19 at 15:56
  • function preRender(ctx) { var userName = _spPageContextInfo.userDisplayName; console.log(userName); var rows = ctx.ListData.Row; for (var i = rows.length - 1; i >= 0; i--) { if ((rows[i].BCName != userName) && (rows[i].DirName != userName) && (rows[i].DXDName != userName)&& (rows[i].XDName != userName)) { rows.splice(i, 1); } } } </script> – JuaP Mar 19 at 15:59

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.