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>

9 comments:

eg10013 said...
This comment has been removed by the author.
eg10013 said...

Hi, Melick

I follow your sample code and modify my view query as the follow. The "Supervisor" and "Attn Monitor" are text fields that contain Windows login name (domain\username). My code does not work for me. Can you tell me what is wrong. Many thanks.


eg10013 said...

I do not while it not allow me post my code in the comment. One more try. If it still does not work, can I email my code to you and would please provide your email address. Thanks again.

//
//
//…
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//

Melick Rajee said...

hi,
what is your code. You are checking against the group or individual user id.

if you are searching like domain/username, change the display to "Account" in the people/group field.

Brian Gullo said...
This comment has been removed by the author.
Brian Gullo said...

Hello, I'm having issues implementing what you have, and it's not working for, I'm trying to implement a non code solution.

I need to query listitems based on current logged in user account name, AD\joe.user in a content query webpart. The column I'm comparing against
is not a user field, it is a text column that contains a string like AD\joe.user. I need to be able to compare that string against the current logged in user.

This is what I have

Parameter Binding:

<property name="ParameterBindings" type="string">
<ParameterBinding Name="LogonUser" Location="ServerVariable(LOGON_USER)" DefaultValue="CurrentUserName"/>
</property>


CAML:

<Where>
<Eq>
<FieldRef Name='ReportAssignedTo' />
<Value Type='Text'>
<LogonUser Type='Text'/>
</Value>
</Eq>
</Where>


It return no results. Am I doing something wrong?

melick baranasooriya said...

@Brian Gullo

You can check whether the server variable has a value. If you are not sure about it you can put a list web part to a page with filter with current user view and check the page code in designer

Sedat Behar said...

Is it possible to manipulate text within? For example, from the LogonUser example above, I would like to get Substring(4) which would be the last name:





<% LogonUser.Substring(4) %>


melick baranasooriya said...

@sedat
Hi you can get the result using caml and then manipulates either using string manipulation and xslt