How to CAML query workflow tasks assigned to other users?
Hello,
I am trying to do a CAML query to get all the workflow tasks assigned to a specific user, not the current context user. The query would be something like below:
<Eq><FieldRef Name='AssignedTo'/><Value Type='Integer'>17</Value></Eq>
or
<Eq><FieldRef Name='AssignedTo'/><Value Type='User'>[user ID]</Value></Eq>
However, none of the above approaches works. Putting an Integer there gave me exception and putting userID there finds no result. Some post mentioned that using a user name instead of userID will work, but this is not a good approach since users can have duplicate names and my users basically don't have names.
I know putting <UserID/> as value returns tasks for the current user. Please let me know what should be put in as AssignedTo Value and Type to get tasks for other users. BTW, the script is running on admin account, so there is no permission issue to see other users tasks. Please advise on the right approach.
Thanks,
Jun
Antworten
Jun,
Go to the task list setting and check properties (Edit Column) of Assigned To column. If the Show field: is set to Name (with presence) then change it to Account and try the with abou CAML.
I know this is not a solution. If you are using Show field as Name then you need to construct a user name string like <user id> + ";#" + <user name> eg "2;#Thomas Mathew".
Thanks
Alle Antworten
- If you have the SPUser object which you can get by looping through Web.AllUsers
try
<Eq><FieldRef Name='AssignedTo'/><Value Type='Text'>SPUserObject.LoginName</Value></Eq>
Thanks
Thanks for your reply. I tried your approach with LoginName (which is known to the application), but the query returns no result. Below is my query part:
string
.Format("<Eq><FieldRef Name='AssignedTo'/><Value Type='Text'>{0}</Value></Eq>", loginName));Is there any other SPUser property to use as AssignedTo value?
Thanks,
Jun
Jun,
Go to the task list setting and check properties (Edit Column) of Assigned To column. If the Show field: is set to Name (with presence) then change it to Account and try the with abou CAML.
I know this is not a solution. If you are using Show field as Name then you need to construct a user name string like <user id> + ";#" + <user name> eg "2;#Thomas Mathew".
Thanks
Hi,
Thanks for your input. Very helpful although not exactly match my test result. I have played with the column Show Field between Name (with presence) and Account and below is my test reaults:
1) If the Show Field is set to Account, query by full account name will work
<Eq><FieldRef Name='AssignedTo'/><Value Type='Text'>accountName</Value></Eq>
2) If the Show Field is set to Name or Name (with presence), query by User Name alone will work. The User Name string cannot contain "<userID>;#" at the beginning, like the example below.
<Eq><FieldRef Name='AssignedTo'/><Value Type='Text'>Andy Smith</Value></Eq>
Once I put the value as "26;#Andy Smith", the query dosen't find anything. I don't know why it cannot contain user id in the value part in my case and this is not a good situation either since the duplicate user name will become a problem then.
Since I want to leave the Show Field as Name (with presence), please let me know how to make the query work with userID as part of the value.
Thanks,
Jun
- We had trouble with this so I built a view using the UI and set the filter condition to [Me], then got a handle to the SPView object and pulled the Query from it - the result is below - works for us with AD as authentication provider.
SPQuery myqry;
myqry.Query = "<Where><Eq><FieldRef Name=\"AssignedTo\" /><Value Type=\"Integer\"><UserID Type=\"Integer\" /></Value></Eq></Where>";
- I know this post is quite old, but I've just spent 3 hours on this problem and got a different solution that works well for me.
Add LookupId=\"TRUE\" to the fieldref tag and you can search by just the SPUser.ID
<Where><Eq><FieldRef Name=\"AssignedTo\" LookupId=\"TRUE\" /><Value Type=\"User\">SPUser.ID</Value></Eq></Where>
Hope this helps someone! - Q-Ian, I like your solution better.
Thanks. I know this post is quite old, but I've just spent 3 hours on this problem and got a different solution that works well for me.
Add LookupId=\"TRUE\" to the fieldref tag and you can search by just the SPUser.ID
<Where><Eq><FieldRef Name=\"AssignedTo\" LookupId=\"TRUE\" /><Value Type=\"User\">SPUser.ID</Value></Eq></Where>
Hope this helps someone!
I have a Multiple Users. Now i want to write a caml query for that. Can you help me to create this type of query ......

