CAML Query to retrieve data for current user…

While working with SharePoint CAML queries, you will need to get current user data. (Retrieve data from a list, that filtered by current logged in user).

There are couple of ways to achieve this requirement.

1) Filter by current user “Display Name”

        <FieldRef Name='Person'/>
        <Value Type='Text'>Lastname, Firstname</Value>

Simply pass the display name of the current user to filter the data. But what if there are multiple users with same display name Sad smile.

So the above method is not a ideal solution.

2) Filter by current user “User ID” – By Passing the User ID

        <FieldRef Name='Person' />
        <Value Type='Integer'>45</Value>

In this method, we pass the current users user id as the parameter and filter the data (45 is the user id of the current user). For this you will need to know the current user id. Retrieving the user id is very simple. But there is another CAML variable that can be used to get current user id.

3) Filter by current user “User ID – User id retrieved from CAML Query it self

        <FieldRef Name='Person' />
        <Value Type='Integer'><UserID/></Value>

In this scenario just pass the <UserID/> to the value field in CAML. This will always retrieve the current users user id internally. Smile


  • westerdaled says:


    I think this looks quite elegant. Can I add the CAML in a XSL ListView as well as a DVWP. In the latter dase. I have done a bit of digging I understand When a DVWP is placed into the page, an SPDataSource is also created. This control has an attribute named SelectCommand that contains the CAML .

    • Kamil Anurdeen says:


      Yes you can add this CAML in the select command section of a DVWP. Without writing and modifying it manually, you add a filter parameter from SharePoint Designer (selecting Current User option inside parameters) it will automatically update the CAML query in the select command section for you.

      • Ok

        Yes I added Full Name Contains [CurrentUser] via the filter dialog in in the view menu…..
        I did not add any of my own parameters as such

        This adds the following

        In the code below I changed the type to Text as my Full Name field is text. but no success.

        Removed filer and displayed UserID in my form and guess what it displays the useless string: “CurrentUserName”

        selectcommand="<View><Query><Where><Contains><FieldRef Name="FullName"/><Value Type="Text"><UserID/></Value></Contains></Where></Query></View>"

  • OOps

    Forgot to delimit this


  • Kamil

    Thanks again for your help. I am beginning to see the problem is unique to my environment.

    1) The clients staff list just has a simple Full Name string as the key so no person field is used.
    2) [Current User] can be used in a DVWP Filter . This creates the CAMLVariable Parameter UserID with a default string “CurrentUserName”. I am guessing this gets populated with the current user at runtime. Only in my case is doesn’t; Just like when I attempt a similar with the LOGON_USER server var.

    Hence, I don’t think your Person query is going to work.

    For the interim solution, I am guessing I will need to amend my list to put in a person lookup and produce a calc field for Full Name.
    I do need to figure out why done of the HTTP vars are available to me.

    What do you think?

  • Hi

    This issue continually haunts me! Firstly, I am now able to access the environment variables after Central Admin -> Web Application – Browser File Handling -> Permissive ( previously this was Strict)

    Now, I am building my own XLV Web Part that references a custom XSL Sheet via the

    I found that if I borrow from a DVWP markup I can expose the current user ( I have removed the delimiting below so I could post this)

    ParameterBinding Name=”UserID” Location=”CAMLVariable” DefaultValue=”CurrentUserName”

    I can pass this to the external style sheet and display. So what if I want to filter in my wp

    FieldRef Name=”FullName”
    Value Type=”Text”


    This does not work. I would prefer to do my filtering in the wp if possible rather than my external XSL. You mentioned the ‘Person’ previously but I am not sure what you mean.

  • Mohan Udupa says:

    Thank you very much, I was using the Display name to filter the items. But there were two guys with the same name.! :( So you resolved my problem. Thanks again.:)

    • Kamil Anurdeen says:

      You are welcome Mohan. I had the same issue and had to struggle a lot to overcome. At the end wrote this down as a reference for my self. Glad that helped you out :)

Leave a Reply

Your email address will not be published. Required fields are marked *