1

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:

<View>
  <Query>
    <Where>
      <Neq>
        <Membership Type="CurrentUserGroups"><FieldRef Name="AssignedTo"/></Membership>
      </Neq>
    </Where>
  </Query>
</View>

Any idea?

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

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 '18 at 11:51
  • are comfortable with rest but there is also direct endpoint but can share script – Sunil Sahu Aug 14 '18 at 11:56
1

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.

Demo:

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.load(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);
            } 
            console.log(queryString1);
            var queryString3 = queryString1 + "<Neq><FieldRef Name= 'AssignedTo'/><Value Type='Integer'><UserID Type='Integer'/></Value></Neq>"+queryString2;
            queryString3 += "</Where></Query></View>";
            console.log(queryString3);
            queryListItems(queryString3);
}
    }
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   
        caml.set_viewXml(queryString);  
        //Specify the query and load the list oject  
        returnedItems = list.getItems(caml);  
        context2.load(returnedItems);  
        //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>'; 
            console.log(listItem.get_item('AssignedTo')); 
            if (listItem.get_item('AssignedTo') == null){
                        markup += 'Assigned To:  none <br>';
            }else{
                        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;  
    }  
</script>

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.