5

I have a CAML query

<Query><Where><Or>"+
             "<Eq><FieldRef Name='UserName' /><Value Type='User'>" + User.Name + "</Value></Eq>"+
             "<Contains><FieldRef Name ='OtherUsers'/><Value Type = 'Note'>" + site.CurrentUser.Name + "</Value></Contains>"+
          "</Or></Where></Query>

This works and brings desired results. Now coming to the issue

The OtherUsers column holds the list of selected users in .csv format. Say if User1&User11 are selected the column will contain User1,User11

When User11 is selected the column will just have User11

Now according to the query, when User1 logs in, it will bring the record containing User11 in OtherUsers column as User11 contains User1. How can I overcome this issue?

4 Answers 4

6
"<Query><Where>"+
                "<Or>"+
                    "<Or>"+
                        "<Or>"+
                             "<Eq><FieldRef Name='UserName' /><Value Type='User'>" + User.Name + "</Value></Eq>"+
                             "<Contains><FieldRef Name ='OtherUsers'/><Value Type = 'Note'>" + site.CurrentUser.Name + ",</Value></Contains>"+
                        "</Or>"+
                        "<Contains><FieldRef Name='OtherUsers' /><Value Type='Note'>," + site.CurrentUser.Name + "</Contains>"+
                    "</Or>"+
                "<Eq><FieldRef Name='OtherUsers' /><Value Type='Note'>" + site.CurrentUser.Name + "</Value></Eq>"+
                "</Or>"+
            "</Where></Query>"

Use this query. It should do the trick

If user1 logs in below scenarios It will work on:

  • OtherUsers = "user1,user11,user12";
  • OtherUsers = "user2,user1,user12";
  • OtherUsers = "user2,user1";
  • OtherUsers = "user1";

And for User11 it will work on below scenarios only:

  • OtherUsers = "user1,user11,user12";
  • OtherUsers = "user2,user11";
  • OtherUsers = "user11";
7
  • excellent workaround, may be when user1 logs in and OtherUsers = "user12,user11" the condition might fail? Commented Sep 13, 2016 at 8:23
  • no it will not fail, it will work
    – Gaurravs
    Commented Sep 13, 2016 at 8:25
  • "<Contains><FieldRef Name='OtherUsers' /><Value Type='Note'>," + site.CurrentUser.Name + "</Contains></Contains>" this condition is true? Commented Sep 13, 2016 at 8:26
  • may be I have construct the csv like ,name1,name2,name3 etc so that I can just check for one contains with ","+name+"," Commented Sep 13, 2016 at 8:30
  • only 3 which added is sufficient, This condition you mentioned represents, user id must have comma in first and last position
    – Gaurravs
    Commented Sep 13, 2016 at 9:52
2

I had also same problem, But unfortunately you cant check exact match of string in comma separated value of multilne text.

But alternatively you can achieve this,

  1. Use contains operator that gives you a set of result items,
  2. Then split items separated by comma and check exact match using script/c#/regular expression.
2

I think your value type is wrong. I have developed such kind of CAML query like below:

<Contains><FieldRef Name='otherUser' /><Value Type='User'>" + site.CurrentUser.Name + "</Value></Contains>

Thanks

0

You could also use "In" element

<In>
  <FieldRef Name = "Field_Name"/>
  <Values>    <Value Type = "Field_Type"/>
  </Values>  <XML />
</In>

Ref: https://learn.microsoft.com/en-us/sharepoint/dev/schema/in-element-query

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.