How can I get the correct SPFieldUserValue to use SPSiteDataQuery to aggregate tasks for a certain user?

sharepoint.stackexchange https://sharepoint.stackexchange.com//questions/91625

  •  10-12-2019
  •  | 
  •  

Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top