3

I am really new to SharePoint, and I came across an issue that is driving me crazy. I am trying to use CAML Query to get a specific list item. My query works on hard coded condition, but not when I pass in the value using a variable. Here is my code:

foreach (SPGroup group in groups)
    {

        queryTeams.Query = @"<Where>
                                 <Eq>       
                                    <FieldRef Name='GroupTeams' />
                                      <Value Type='User'>" + group.Name + @"</Value>
                                  </Eq>
                               </Where>
                           "; // group.Name = Pharmacist Assistants                         

            SPListItemCollection itemsTeams = listTeams.GetItems(queryTeams);

            if (itemsTeams.Count > 0)
            {
                //do stuff
            }

And here is the hard coded query that returns values:

queryTeams.Query = @"<Where>
                       <Eq>
                           <FieldRef Name='GroupTeams' />
                             <Value Type='User'>Pharmacist Assistants</Value>
                        </Eq>
                      </Where>";

Please help me figure out what am I doing wrong.

3
  • In the first CAML query (the "dynamic" one) the new line just before "WHERE" is actually present in the real code or just a refuse ? Also - have you checked that group.Name is indeed equal to "Pharmacist Assistants"?
    – SPArcheon
    Oct 25, 2013 at 10:06
  • I just deleted the new line, please see the edited code. Yes, the group.Name is "Pharmacist Assistants". Thank you.
    – Ana
    Oct 25, 2013 at 10:14
  • Write your query in one single line, that will help you understand where you are going wrong. You can format once you have figured out the issue. Possibly @ is causing issue.
    – Ishan
    Oct 25, 2013 at 10:24

1 Answer 1

0

might sound silly but have you tried:

EDIT

foreach (SPGroup group in groups)
    {
        SPQuery queryTeams = new SPQuery();

        queryTeams.Query = @"<Where><Eq><FieldRef Name='GroupTeams' /><Value Type='User'>" + group.Name.ToString() + @"</Value></Eq></Where>"; 

            SPListItemCollection itemsTeams = listTeams.GetItems(queryTeams);

            if (itemsTeams.Count > 0)
            {
                //do stuff
            } 

you could make the SPQuery object global if you wanted to (used in many places) but within the foreach you must create a new object recursivly! like so:

global varible:

private SPQuery queryTeams;

within method:

foreach (SPGroup group in groups)
    {
        queryTeams = new SPQuery();

        queryTeams.Query = @"<Where><Eq><FieldRef Name='GroupTeams' /><Value Type='User'>" + group.Name.ToString() + @"</Value></Eq></Where>"; 

            SPListItemCollection itemsTeams = listTeams.GetItems(queryTeams);

            if (itemsTeams.Count > 0)
            {
                //do stuff
            } 
5
  • When I look at query.Teams using the debugger, this is what I get:{<Where> <Eq> <FieldRef Name='GroupTeams' /> <Value Type='User'>Pharmacist Assistants</Value> </Eq></Where>} and I get the same sequence when I hard code the "Pharmacist Assistants" part.
    – Ana
    Oct 25, 2013 at 10:25
  • just ammended the code, could you try that?
    – Ali Jafer
    Oct 25, 2013 at 10:47
  • So I just figured it out. I had to create a new instance of the SPQuery class, before I would try to use my QueryTeams object.
    – Ana
    Oct 25, 2013 at 10:50
  • ali SharePoint, I see this is your solution too. Thank you very much for your help :)
    – Ana
    Oct 25, 2013 at 10:51
  • yes for each call! lol just beat you to it ;)
    – Ali Jafer
    Oct 25, 2013 at 10:51

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.