Skip to main content

Posts

Showing posts from January 4, 2012

How to Write An SPQuery Using Multiple AND OR Operators

Writing CAML queries can sometimes make your head hurt. Not because it’s terribly complicated, but because there’s not much useful information out there that demonstrates, with REAL examples, how these should be written. So let’s see if we can clarify this: Scenario 1 Get me all items in a list WHERE fullName equals the currently logged in user. 1 2 3 4 5 6 7 8 9 10 11 SPWeb web = SPControl. GetContextWeb ( Context ) ; string fullName = web. CurrentUser . Name ; SPQuery oQuery = new SPQuery ( ) ; oQuery. Query = "<Where>" + "<Eq><FieldRef Name='FullName'/><Value Type='Text'>'" + fullName + "'</Value></Eq>" + "</Where>" + "<OrderBy><FieldRef Name='StartTime' Ascending='FALSE'></FieldRef></OrderBy>" ; Scenario 2 Get me all items in a list WHERE fullName equals the currently