Building a CAML query in SP. Want to result item where current user not in group that is in the AssignedTo field.

Tried wit Neq, NotIncluded:

        <Membership Type="CurrentUserGroups"><FieldRef Name="AssignedTo"/></Membership>

Any idea?

  • Does my suggestion help you? Aug 17, 2018 at 0:54
  • no, maybe for scripting but I need it in a listview, sorry...
    – M.P.
    Aug 17, 2018 at 6:41

2 Answers 2


We cannot use 'Ne' with 'Membership' as the parent tag supported for <Membership> are

  • And
  • Or
  • Where

For reference you can refer

  • ok good to know, any more ideas?
    – M.P.
    Aug 14, 2018 at 11:51
  • are comfortable with rest but there is also direct endpoint but can share script
    – Sunil Sahu
    Aug 14, 2018 at 11:56

You can use CSOM to display the specific items as you want. Use SPWeb.CurrentUser.Groups to get the collection of groups of which the user is a member. And use CAML query to filter on items which were not assigned to current user and current user’s groups.


Note: Change "task01" to your list name.

<div id="displayDiv"></div>  
<script type="text/javascript">  
    //This example gets the items in the cl_Latest list that created by user or created by user group's memberships.   
    //This variable will hold the list items collection  
    var returnedItems = null;
    var groups = null;  
    var groupId="";
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getGroups);
    function getGroups(){
        //Get the current context   
        var context = new SP.ClientContext.get_current(); 
        var oWeb = context.get_web();
        //Get current user
        var currentUser = oWeb.get_currentUser();
        //Gets the collection of groups of which the user is a member
        groups = currentUser.get_groups();
        context.executeQueryAsync(loopGroups, onFailedCallback);
function loopGroups(){
        if (groups !== null){
            var groupEnumerator = groups.getEnumerator();
            var queryString1 = "<View><Query><Where>";
            var queryString2 = "";
            while (groupEnumerator.moveNext()) {
                var group = groupEnumerator.get_current();
                var gName = group.get_title();
                queryString2 += "<Neq><FieldRef Name= 'AssignedTo'/><Value Type='Text'>"+gName+"</Value></Neq></And>"
                queryString1 += "<And>"
                console.log("name: "+gName);
            var queryString3 = queryString1 + "<Neq><FieldRef Name= 'AssignedTo'/><Value Type='Integer'><UserID Type='Integer'/></Value></Neq>"+queryString2;
            queryString3 += "</Where></Query></View>";
function queryListItems(queryString) { 
        //console.log("run group id:"+groupId);
        var context2 = new SP.ClientContext.get_current();  
        //Get the Announcements list. Alter this code to match the name of your list  
        var list = context2.get_web().get_lists().getByTitle('task01');       
        //Create a new CAML query  
        var caml = new SP.CamlQuery();  
        //Create the CAML that will return   
        //Specify the query and load the list oject  
        returnedItems = list.getItems(caml);  
        //Run the query asynchronously, passing the functions to call when a response arrives  
        context2.executeQueryAsync(onSucceededCallback, onFailedCallback);  
//This function fires when the query completes successfully  
    function onSucceededCallback(sender, args) { 
        console.log("Display items for group:"+groupId);    
        //Get an enumerator for the items in the list  
        var enumerator = returnedItems.getEnumerator();  
        //Formulate HTML from the list items  
        var markup = '';  
        //Loop through all the returned items  
        while (enumerator.moveNext()) {  
            var listItem = enumerator.get_current();  
            markup += 'Item Title: ' + listItem.get_item('Title') + '<br>';  
            markup += 'Item ID: ' + listItem.get_id() + '<br>'; 
            if (listItem.get_item('AssignedTo') == null){
                        markup += 'Assigned To:  none <br>';
                        var users="";
                        for(var i = 0;i < listItem.get_item('AssignedTo').length;i++) {
                                   users += listItem.get_item('AssignedTo')[i].get_lookupValue() +"; "; 
                        markup += 'Assigned To: ' + users + '<br>';
            markup += 'Create By: ' + listItem.get_item('Author').get_lookupValue() + '<br><br>'; 
        //Display the formulated HTML in the displayDiv element  
        displayDiv.innerHTML += markup;  
    //This function fires when the query fails  
    function onFailedCallback(sender, args) {  
        console.log("Display items for group:"+groupId);
        //Formulate HTML to display details of the error  
        var markup = '<p>The request failed: <br>';  
        markup += 'Message: ' + args.get_message() + '<br>';  
        //Display the details  
        displayDiv.innerHTML += markup;  

Add the following code into a page. It will only display the items which were not assigned to the current user and current user’s groups from the specific list.

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.