Thursday, July 7, 2011

How to get Username , UserID in CAML Query

If you are want to get the userID you can simple use following code.

<Where>
   <Eq>
      <FieldRef Name='userfieldname' />
      <Value Type='Integer'>
           <UserID Type='Integer' />
      </Value>
   </Eq>
</Where>

in here you should declare the UserID variable before it uses. normally If you make a user filter in the SharePoint designer it will automatically create the parameter in parameter binding section in the web part.

<ParameterBinding Name="UserID" Location="CAMLVariable" DefaultValue="CurrentUserName"/>

But assume you want to filter using a user name and the field is not a persongroup feild and it is just a text field. then you can use following query to archive it.

<Where>
   <Eq>
      <FieldRef Name='userfieldname' />
      <Value Type=’Text’>
           <UserID Type=’Text’/>
      </Value>
   </Eq>
</Where>
Post a Comment