简体   繁体   English

使用CAML比较SharePoint“人员或组”字段

[英]Using CAML to compare SharePoint “Person or Group” fields

I have a SharePoint 2007 (MOSS) list with 2 "Person or Group" columns which I would like to compare, essentially: 我有一个SharePoint 2007(MOSS)列表,其中包含2个“人员或组”列,我想对其进行比较:

SELECT * FROM List WHERE (Analyst = Developer)

In my code (C#) I've built this as: 在我的代码(C#)中,我将其构建为:

SPQuery itemQuery = new SPQuery();
itemQuery.Query = "<Where><Eq><FieldRef Name='Analyst' /><FieldRef Name='Developer' /></Eq></Where>";

SPListItemCollection queryResults = list.GetItems(itemQuery);

But this is throwing an exception ("Exception from HRESULT: 0x80020009 (DISP_E_EXCEPTION)"). 但这会引发异常(“ HRESULT异常:0x80020009(DISP_E_EXCEPTION)”)。

I've seen some information regarding the Type attribute of a Value element when comparing against a "Person or Group" column, but nothing about comparing two of these columns against each other. 与“ Person或Group”列进行比较时,我已经看到了一些有关Value元素的Type属性的信息,但没有将这两个列彼此进行比较。

I've also tried adding "LookupId='TRUE'" to each FieldRef, with no change. 我也尝试将“ LookupId ='TRUE'”添加到每个FieldRef中,而没有更改。

It is not possible to compare two fields like that using CAML. 无法像使用CAML那样比较两个字段。 You have to use a literal value. 您必须使用文字值。 This means, that you'll likely have two queries: 这意味着您可能会有两个查询:

  1. Retrieve the Analyst/Developer's user ID 检索分析师/开发人员的用户ID
  2. Retrieve items where Analyst and Developer are the same 检索Analyst和Developer相同的项目

Example of #2: #2的示例:

<Where>
   <And>
      <Eq><FieldRef Name="Analyst" LookupId="TRUE"/><Value Type="Integer">42</Value></Eq>
      <Eq><FieldRef Name="Developer" LookupId="TRUE"/><Value Type="Integer">42</Value></Eq>
   </And>
</Where>

FYI, you can also use <UserID/> for the "Current User" instead of user ID (in this example, 42). 仅供参考,您也可以将<UserID/>用作“当前用户”,而不是用户ID(在此示例中为42)。

The logic you are looking for, I believe, is this: 我相信,您正在寻找的逻辑是:

<Where>
   <Eq>
      <FieldRef Name='Analyst'/>
      <Value Type="Text"><FieldRef Name='Developer'/></Value>
   </Eq>
</Where>

I tested and this is not possible so the two options, as I see it, are: 我进行了测试,但这是不可能的,因此,如我所见,这两个选项是:

  1. Get all list items , then iterate through with JQuery with a find and compare for the two fields being equal. 获取所有列表项,然后使用查找遍历JQuery并比较两个相等的字段。

  2. Create a calculated column that sets a true or false value if the two columns are equal in the list and then do your select based on that column. 创建一个计算列,如果列表中的两列相等,则该列将设置true或false值,然后根据该列进行选择。 This is probably the most expedient and also most effective from a performance perspective. 从性能的角度来看,这可能是最方便的方法,也是最有效的方法。

Try this: 尝试这个:

<Where> 
  <Eq> 
    <FieldRef Name="Analyst" /> 
    <Value Type="Text">Developer</Value> 
  </Eq> 
</Where>

I have found a list that has two matching values to compare and I got a CAML query to work with the comparison; 我发现了一个列表,其中包含两个要比较的匹配值,并且我得到了一个CAML查询来进行比较。 however, it is a boolean value so I am not sure if this is what you are looking for. 但是,它是一个布尔值,所以我不确定这是否是您想要的。 It does compare two fields but I think the literal is still getting in the way. 它确实比较了两个字段,但我认为字面值仍然会妨碍您。 This list has about 25 entries in it and this is the only one that matched so I thought this would be a good test. 该列表中包含约25个条目,这是唯一匹配的条目,因此我认为这将是一个很好的测试。

Here is the code: 这是代码:

private DataTable ExecuteQuery(SPList list) 
{ 
   SPQuery qry = new SPQuery(); 
 qry.Query = "<Where><And><Contains><FieldRef Name='Show' /><Value       Type='Boolean'>1</Value></Contains><Contains><FieldRef Name='Highlight' /><Value     Type='Boolean'>1</Value></Contains></And></Where>"; 
qry.ViewFields = "<FieldRef Name='Show' /><FieldRef Name='Highlight' />"; 
qry.IncludeMandatoryColumns = true; 
return list.GetItems(qry).GetDataTable();

Sorry if this is not what you are looking for. 抱歉,这不是您想要的。 Good Luck!! 祝好运!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM