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”

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

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

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

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

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

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

Advertisements

9 thoughts on “CAML Query to retrieve data for current user…

  1. Hi

    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 .

    • Hi,

      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
        CurrentUserName

        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>"

  2. 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?

  3. 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
    property.

    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

    Where
    Contains
    FieldRef Name=”FullName”
    Value Type=”Text”
    UserID

    Contains
    /Where
    /Query

    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.

  4. 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.:)

    • 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

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s