Ok, if you’re like me, 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 | SPWeb web = SPControl.GetContextWeb(Context); |
Scenario 2
Get me all items in a list WHERE fullName equals the currently logged in user AND status equals ‘Complete’.
1 | SPWeb web = SPControl.GetContextWeb(Context); |
Scenario 3
Get me all items in a list WHERE fullName equals the currently logged in user AND status equals ‘Complete’ AND manager is James Lane.
This is where it gets a bit tricky. The following example is INCORRECT and will produce an error when run:
1 | SPWeb web = SPControl.GetContextWeb(Context); |
Scenario 4
Get me all items WHERE fullName equals the currently logged in user AND status equals ‘Complete’ OR status equals ‘On Hold’.
1 | SPWeb web = SPControl.GetContextWeb(Context); |
I really do recommend if you’re doing any work with CAML queries that you download the U2U CAML Query Builder 2007. It doesn’t come without it’s issues, for example, doing a query on a list with a column of type Lookup does not always yield the results I would expect, but it’s still quite helpful. I also often find myself changing the Value Type from Lookup to Text but other than that it’s a huge resource when trying to determine if the data is actually in the list or if there is a problem with the query i’ve written.
Any feedback or questions, please drop me a line.
If you have better solution, just tell me !
1 comments:
Post a Comment