Forum Discussion

rautchetan27's avatar
rautchetan27
Brass Contributor
May 28, 2020
Solved

CAML query to filter sharepoint list on the basis of Currentuser group

I am using following query to filter List items on the basis of current user group, so current user only can see list items which having value in column Vendor same as name of currentuser group.

I have <And> condition in query which working correctly, but Condition for Currentusergroup is not working and its not filtering with value of currentusergroup

 

<Where><And><Eq><FieldRef Name="ADM_x0020_ID"/><Value Type="Text">1234</Value></Eq><Eq><FieldRef Name="Vendor_new"/><Value Type="Text">IBM</Value></Eq></And><Eq><Membership Type="CurrentUserGroups"><FieldRef Name="Vendor"/></Membership></Eq></Where>

 

anyone can tell whats wrong in above query?

  • rautchetan27 : When you check person/groups column settings, you will find properties as per below screenshot

    Allow selection : People and Groups should be selected

    Choose from All users if you want to allow both people and groups or SharePoint Groups only to show SP groups.

     

    Hope it helps !

     

    If I have answered your question, please mark your post as solved.

    if you like my response, please give it a thumbs up.

14 Replies

  • rautchetan27 Try something like below:

    1)<Where><Contains><FieldRef Name=\"Vendor\" /><Value Type='Text'>" + currentUser + "</Value></Contains></Where>

    Note: When you use text <contains> tag will give you the results all that is matching.
    2) <Where><Eq><FieldRef Name=\"Vendor\" /><Value Type='User'>" + currentUser + "</Value></Eq></Where>
    3) <Where><Eq><FieldRef Name='Vendor' LookupId='True'/><Value Type='Lookup'>" + _spPageContextInfo.userId + "</Value></Eq></Where>
    Note: with User ID - it will give you the exact match and will return only one value. 
     
    Hope it helps !
    • rautchetan27's avatar
      rautchetan27
      Brass Contributor

      Ashish_Kohale 

       

      HI,

       

      I dont want to use user,  in vendor column I will have sharepoint group names

      IT

      Infra

      Finance

       

      I have three sharepoint groups with same Name and users are added to one of the sharepoint groups

       

      so when user access list, it will able to see only list items having value on column same as user group name thats why i want to use

       

       

      <Where><Membership Type="CurrentUserGroups"><FieldRef Name="Vendor"/></Membership></Where>

       

      On internet i found same solution many places but somehow its not working for me

       

      • Ashish_Kohale's avatar
        Ashish_Kohale
        Iron Contributor

        rautchetan27 :  Ohk got you ! Yours query looks correct in that case.

        1) First check whether you have access to check others task or not .

        2) Second, if you are doing this programmatically then you can check you query via CAML Query Builder.

        3) 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 assigned to current user and current user’s groups.

  • rautchetan27 : I think you are not comparing Vendor value with the user, kindly check below query. Hope it will help. Try to use user ID instead of username query as below:

    <Eq>
    <Membership Type=\"CurrentUserGroups\">
        <FieldRef Name=\"Vendor\"/>
      </Membership>
      <Eq>
      <FieldRef Name=\"Vendor\"></FieldRef>
      <Value Type=\"Integer\">
      <UserID/>
      </Value>
      </Eq>
    </Eq>

     

    If I have answered your question, please mark your post as Solved.
    If you like my response, please give it a Thumbs Up.

     

    • rautchetan27's avatar
      rautchetan27
      Brass Contributor

      Ashish_Kohale 

       

      I dont want to use UseId, i am trying following query, but no Luck..any idea whats going wrong?

       

      <Query>
      <Where>
      <Eq>
      <Membership Type="CurrentUserGroups">
      <FieldRef Name="Vendor"/>
      </Membership>
      </Eq>
      </Where>>
      </Query>

      • rautchetan27's avatar
        rautchetan27
        Brass Contributor

        Ashish_Kohale 

         

        i tried following query also, but it throw error

         

        <Query><Where><Eq><FieldRef Name="Vendor"/><Value Type="Text"><Membership Type="CurrentUserGroups"></Membership></Value></Eq></Where></Query>

Resources