MSDN > 論壇首頁 > SharePoint - Workflow > How to CAML query workflow tasks assigned to other users?
發問發問
 

已答覆How to CAML query workflow tasks assigned to other users?

  • 2008年6月12日 下午 03:57JunHOV 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    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

解答

  • 2008年6月13日 下午 10:29wss_cool 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆

    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

     

     

所有回覆

  • 2008年6月12日 下午 09:59wss_cool 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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

  • 2008年6月13日 下午 01:59JunHOV 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    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

  • 2008年6月13日 下午 10:29wss_cool 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆

    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

     

     

  • 2008年6月16日 下午 04:48JunHOV 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    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

  • 2008年8月8日 上午 12:05IDicker 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     提議的解答包含代碼
    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>";

    • 已提議為解答IDicker 2008年8月8日 上午 12:28
    • 已編輯IDicker 2008年8月8日 上午 12:06Clarified how view created
    •  
  • 2009年4月14日 下午 01:33Q-Ian 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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!
  • 2009年6月11日 下午 08:25deepakjg 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Q-Ian, I like your solution better.

    Thanks.
  • 2009年11月11日 上午 08:26Dilip Nikam 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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 ......