简体   繁体   English

共享点-通过CAML按用户ID过滤列表

[英]Sharepoint - Filtering lists by userid via CAML

Using Sharepoint 2007 and trying to filter a list of items by a field called StudentName: 使用Sharepoint 2007并尝试​​通过名为StudentName的字段过滤项目列表:

<Field
ID="{GUID-REDACTED}"
Name="StudentName"
DisplayName="Student Name"
Type="User"
Group="STUDENT COLS" />

Currently, this is the CAML used to filter, when its run through U2U CAML Query Builder, it returns the correct list items without a problem, however when its deployed to SharePoint, it returns the entire list (ie no filtering). 当前,这是用于筛选的CAML,通过U2U CAML查询生成器运行时,它会返回正确的列表项而不会出现问题,但是,当将其部署到SharePoint时,它将返回整个列表(即,没有筛选)。

SPQuery userQuery = new SPQuery();
userQuery.Query = "<OrderBy>
<FieldRef Name='Rank'>
</FieldRef>
</OrderBy>
<Where>
    <Eq>
        <FieldRef Name='StudentName' LookupId='TRUE' />
        <Value Type='Integer'><UserID /></Value>
    </Eq>
</Where>"

SPListItemCollection userProjectBasket = PBL.GetItems(userQuery);

I've tried it with and without Query tags, to no avail and i've also changed the type of the userID to User as a last resort, still no joy. 我已经尝试过使用和不使用Query标记,但都无济于事,并且我也已将userID的类型更改为User,这是不得已而为之的选择,仍然没有乐趣。

Very stumped, so any input warmly welcomed. 非常难堪,因此欢迎任何输入。 Thanks. 谢谢。

The OrderBy caluse needs to come after the Where clause: OrderBy运算符需要放在Where子句之后:

userQuery.Query = 
@"<Where>
    <Eq>
        <FieldRef Name='StudentName' LookupId='TRUE' />
        <Value Type='Integer'><UserID /></Value>
    </Eq>
</Where>
<OrderBy>
    <FieldRef Name='Rank'/>
</OrderBy>";

Good answer - but note one thing - when using the SPQuery object, you NEVER specify the tags (these are internally included in the SPQuery.Query object). 好的答案-但要注意一件事-使用SPQuery对象时,切勿指定标记(这些标记内部包含在SPQuery.Query对象中)。

Also, you can still use the U2U CAML creator to save you the time of building the query itself (http://www.u2u.be/Tools/SharePointCamlQueryBuilder.aspx); 另外,您仍然可以使用U2U CAML创建器来节省构建查询本身的时间(http://www.u2u.be/Tools/SharePointCamlQueryBuilder.aspx); though it was built for 2007, it works for 2010 too. 尽管它是针对2007年构建的,但也适用于2010年。

David Sterling/SICG - http://www.sterling-consulting.com 大卫·斯特林/ SICG- http://www.sterling-consulting.com

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

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