Filter SharePoint List Items by Current User or Group

Below is the Query to get the Items,

  1. camlQuery.set_viewXml("<View><Query><Where><Or><Membership Type=\"CurrentUserGroups\"><FieldRef Name=\"AssignedTo\"/></Membership><Eq><FieldRef Name=\"AssignedTo\"></FieldRef><Value Type=\"Integer\"><UserID/></Value></Eq></Or></Where></Query></View>");  
Use the below code to get the items from host web using SharePoint app,

Use strict:
  1. //var context = SP.ClientContext.get_current();  
  2. //var user = context.get_web().get_currentUser();  
  3. var hostWebURL, appWebURL;  
  4.   
  5. // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model  
  6. $(document).ready(function () {  
  7.     // getUserName();  
  8.     GetDatafromList();  
  9.   
  10. });  
  11.   
  12. function GetDatafromList() {  
  13.   
  14.     hostWebURL = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));  
  15.     appWebURL = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'))  
  16.     var context = new SP.ClientContext(appWebURL);  
  17.     var appCtxSite = new SP.AppContextSite(context, hostWebURL);  
  18.     var web = appCtxSite.get_web();  
  19.     var list = web.get_lists().getByTitle(‘TestList’);  
  20.     var camlQuery = new SP.CamlQuery();  
  21.     camlQuery.set_viewXml("<View><Query><Where><Or><Membership      Type=\"CurrentUserGroups\"><FieldRef Name=\"AssignedTo\"/></Membership><Eq><FieldRef Name=\"AssignedTo\"></FieldRef><Value Type=\"Integer\"><UserID/></Value></Eq></Or></Where></Query></View>");  
  22.     var liscoll = list.getItems(camlQuery);   
  23.     context.load(liscoll);  
  24.     context.executeQueryAsync(function () {  
  25.         console.log("My Portfolio");  
  26.         var listItemCount = liscoll.get_count();  
  27.         if (listItemCount > 0) {  
  28.             var enumerator = liscoll.getEnumerator();  
  29.             while (enumerator.moveNext()) {  
  30.                 var currentItems = enumerator.get_current();  
  31.                 var title = currentItems.get_item("Title");  
  32.                 console.log("Title is : " + title);  
  33.   
  34.             }  
  35.         }  
  36.     }  
  37.         , function (sender, args) {  
  38.             console.log("List Items failed" + args.get_message());  
  39.         });  
  40.   
  41. }  
  42.   
  43. function manageQueryStringParameter(paramToRetrieve) {  
  44.     var params = document.URL.split("?")[1].split("&");  
  45.     var strParams = "";  
  46.     for (var i = 0; i < params.length; i = i + 1) {  
  47.         var singleParam = params[i].split("=");  
  48.         if (singleParam[0] == paramToRetrieve) {  
  49.             return singleParam[1];  
  50.         }  
  51.     }  
  52. }  
Make sure to provide permission for list in appmanifest.xml file as shown in below,

permission

Summary

In this article we have explored how to get the list items for assigned to current user or to the group in which the current user is a member.