1

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 = 'Text'> " + site.CurrentUser.Name + " </Value></Contains>"+
          "</Or></Where></Query>

This query is not fetching desired results. Especially the <Contains> part of the query. The <Eq> part works fine and it fetches results, but the contains part is not. Contains part is checking a note field in SharePoint list

8
  • Are you storing display name of users in OtherUsers column?
    – Gaurravs
    Sep 13, 2016 at 5:19
  • 'OtherUsers' is single line or multiline text? Sep 13, 2016 at 5:24
  • @Gaurravs Yes, I have their user id in it Sep 13, 2016 at 5:27
  • 1
    Then change Value Type of 'OtherUsers' to 'Note' and then try. Sep 13, 2016 at 5:27
  • 1
    It should work with 'Text' as well
    – Gaurravs
    Sep 13, 2016 at 5:49

3 Answers 3

0

You are almost there. If 'OtherUsers' is multiline text field then just change Value Type of OtherUsers to 'Note'.

<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>
1
"<Where><Or>"+
      "<Eq><FieldRef Name='UserName' /><Value Type='User'>" + User.Name + "</Value></Eq>"+
      "<Contains><FieldRef Name='OtherUsers'/><Value Type='Text'> " + site.CurrentUser.LoginName + " </Value></Contains>"+
"</Or></Where>";

You need to use site.CurrentUser.LoginName instead of site.CurrentUser.Name to get LoginName of user. If you want to use Display name of user, then site.CurrentUser.Name

Check the various options and use based on what you are storing:

site.CurrentUser.LoginName : LoginName of user
site.CurrentUser.Name : Display Name of user
site.CurrentUser.ID : Integer ID of user

0

I found out the issue after some debugging and comments from @Gaurrav

The issue was the additional space that was there in the query!

Old Query

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

Corrected Query

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

Notice that the Value inside the <contains> query had additional spaces

Lessons learnt:

  1. Don't add additional spaces for readability inside your CAML query.

  2. Both Note and Text will work for multiple lines of text field

1
  • 1
    I would suggest not to keep additional space in <Value Type = 'Text'>" this as well . Refer my answer for same
    – Gaurravs
    Sep 13, 2016 at 6:10

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.