Using [Me] in CAML Queries

Sometimes you need to put a custom View into a List Definition.  That View could be based on the current user (for example “Show all List Items assigned to me”).  The problem is that the usual GUI method uses [Me] to filter the items appropriately, which breaks CAML.

The trick is twofold.  First, the Value Type is “User” and second, the magic value is <UserID />.  Therefore your code is.

<Query>
  <Where>
    <FieldRef="AssignedTo" />
      <Value Type="User"><UserID /></Value>
  </Where>
</Query>

Hat Tip: Macedonian SharePoint Group (which incidentally also demonstrates how to use [Today] in CAML.  Also MSDN.

Advertisements

Posted on October 8, 2012, in SharePoint and tagged , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: