1

I'm trying to gather all the tasks of a certain content type for a certain user. To accomplish this I'm using a SPSiteDataQuery which looks like this:

string where = String.Format(@"<Where>
                                        <And>
                                            <BeginsWith>
                                                <FieldRef Name='ContentTypeId'/>
                                                <Value Type='Text'>0x0108010031C20CDC28544f93B27958D36ACBE2F5</Value>
                                            </BeginsWith>
                                            <Eq>
                                                <FieldRef Name='AssignedTo'/>
                                                <Value Type='Text'>{0}</Value>
                                            </Eq>
                                        </And>
                                     </Where>", userValue.ToString());
query.Query = where;
DataTable results = web.GetSiteData(query);

It works fine if I just want to filter for the content type but filtering for the user fails.

Here is what I think the problem is.. the AssignedTo field contains values that look like this

17;#TestUser01

But my regular LoginName contains the claims string and domain and looks like this

i:0#.w|test\TestUser01

I tried to create the SPFieldUserValue to get the same user-string that is found in the AssignedTo-field.. like that:

SPFieldUserValue userValue = new SPFieldUserValue(web, SPContext.Current.Web.CurrentUser.ID, SPContext.Current.Web.CurrentUser.LoginName);

.. but then I just get a value that looks like this:

17;#i:0#.w|test\TestUser01

..and I still get no results from my query..

How can I get the correct string to execute my query?

2 Answers 2

2

By changing the CAML like this:

string where = @"<Where>
                                        <And>
                                            <BeginsWith>
                                                <FieldRef Name='ContentTypeId'/>
                                                <Value Type='Text'>0x0108010031C20CDC28544f93B27958D36ACBE2F5</Value>
                                            </BeginsWith>
                                            <Eq>
                                                <FieldRef Name='AssignedTo' LookupId='TRUE' />
        <Value Type='Integer'><UserID /></Value>
                                            </Eq>
                                        </And>
                                     </Where>";
query.Query = where;
DataTable results = web.GetSiteData(query);

it will fetch for current user :)

If you need to specify another user you can use:

string where = String.Format(@"<Where>
                                        <And>
                                            <BeginsWith>
                                                <FieldRef Name='ContentTypeId'/>
                                                <Value Type='Text'>0x0108010031C20CDC28544f93B27958D36ACBE2F5</Value>
                                            </BeginsWith>
                                            <Eq>
                                                <FieldRef Name='AssignedTo' LookupId='TRUE' />
                                                <Value Type='Integer'>{0}</Value>
                                            </Eq>
                                        </And>
                                     </Where>", userValue.ToString());
query.Query = where;
DataTable results = web.GetSiteData(query);

and just make sue you have the ID of the user in userValue

0
0

The CAML Query for fetching the values for current user is:

"<Where><Eq><FieldRef Name='AssignedTo' /><Value Type='Integer'><UserID Type='Integer' /></Value></Eq></Where>";

And for another user it should be as mentioned by @Robert.

3
  • What do you mean is wrong with my first CAML? It does exactly what yours do, but also filters on Content Type ;) Commented Feb 26, 2014 at 15:57
  • @RobertLindgren, you are right it does the same. Sorry, I didn't test it. Commented Feb 26, 2014 at 16:16
  • Ah great, thought I had made an error :) Commented Feb 26, 2014 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.