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.

4
  • 1
    Could you try to describe what you want to achieve by doing this? Why isn't filtering using [Me] working? Commented Mar 18, 2020 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... Commented Mar 18, 2020 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... Commented Mar 18, 2020 at 22:07
  • Ok. Done. Any other suggestions. Thanks.
    – JuaP
    Commented Mar 18, 2020 at 22:36

1 Answer 1

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

9
  • 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
    Commented Mar 19, 2020 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? Commented Mar 19, 2020 at 14:51
  • I added screenshots to the answer Commented Mar 19, 2020 at 15:04
  • <script type="text/javascript"> (function () { var bodyFieldContext = {}; bodyFieldContext.OnPreRender = preRender; SPClientTemplates.TemplateManager.RegisterTemplateOverrides(bodyFieldContext); })();
    – JuaP
    Commented Mar 19, 2020 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
    Commented Mar 19, 2020 at 15:59

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.