Filtering Tasks List To Show Current User’s Tasks

Well, this is just a quick sharing on how you could filter a Sharepoint list view to show only items or rows belong to the current user (i.e. user who login to the Sharepoint site). One of the usage especially in Office 365 Tasks list, anyone who has edit right to the Tasks list will be able to edit (i.e. approve or reject) any tasks in the Tasks list, including one that is not belonging to you.

In the screen capture below, we can see by default anyone who has access to the Tasks list sees every task in the Tasks list.

We are going to add the following JavaScript to the JS Link (i.e. Client Side Rendering) of the Tasks list to remove the tasks that are not belonging to the current user. Just take the below JavaScript code and save it to a file (i.e. in my case I have named it TaskList.js and uploaded it to the Site Asset library).

(function () {
 function renderListItemTemplate(renderCtx) {
 var userId = _spPageContextInfo.userId;
 if(renderCtx.CurrentItem.AssignedTo[0].id != userId)
 {
 return ''; //do not render row
 }
 return RenderItemTemplate(renderCtx);
 }


 function registerListRenderer()
 {
 var context = {};
 context.Templates = {};
 context.Templates.Item = renderListItemTemplate;

 SPClientTemplates.TemplateManager.RegisterTemplateOverrides(context);
 }
 ExecuteOrDelayUntilScriptLoaded(registerListRenderer, 'clienttemplates.js');

})();

Once that is done, just include the file to the JS Link property of the Task list page as shown below:

That’s all we need to filter a list to the current user. The capture below demonstrates the outcome after the JS Link is applied to the page (i.e. showing only Tasks belong to Richard Roe in my example below).

Leave a Reply

Your email address will not be published. Required fields are marked *