Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# caml sharepoint2010
I have a caml query in my code to return some items of SharePoint list. I added the IF statement in case if query not find any matching item or returns NULL.
  SPListItemCollection Items = RiskAssesment.GetItems(new SPQuery()
                    {
                        Query = @"<Where>
                                         <Eq>
                                            <FieldRef       Name='Department'/>
                                            <Value Type='Text'>"+Department+"</Value>                           </Eq></Where>"
                    });
 
                    foreach (SPListItem item in Items)
                    {
                        if (item != null)
                        {
                            item["Name"]="abcd";
                            item.Update();
                        }
                        else
                        {
                            newListItem["Name"] = "xyz";
                            newListItem.Update();
                        }
                    }
if it not finds Department in the list, it doesn't goes to ELSE statement.
Posted 11-Oct-12 7:22am
Edited 11-Oct-12 8:24am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Did you tried that query with caml query builder? I think while giving that variable Department, it is giving some problem. Just remove that variable and include in the query as below.
 
@"<Where>
         <Eq>
          <FieldRef       Name='Department'/>
          <Value Type='Text'>Department</Value>                                       </Eq>
</Where>"
 
Now verify the result is coming correctly or not. If not then entire query need to be changed. If yes, then the problem coming when we insert that variable into the query.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 15 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100