SharePoint 的 CAML 查询“创建者”带有用户名的字段

发布于 2024-09-02 19:02:42 字数 359 浏览 2 评论 0 原文

嘿,我有一个供管理员使用的表单,他们在其中插入用户名(“域\名称”),代码从中获取和设置一些信息。

这是一个巨大的项目,有些列表包含字符串形式的用户名(“域名\名称”),但有些列表仅依赖于自动创建的“创建者”列。

我想知道使用用户名字符串查询这些列表的最快方法是什么。我尝试使用与第一种列表相同的查询,但它显然不起作用 -

<Where><Eq><FieldRef Name='UserName'/><Value Type='Text'>domain\\username</Value></Eq></Where>

谢谢。

Hey, I have a form for administrators where they insert a user name ("domain\name") and the code gets and sets some information out of it.

It's a huge project and some of the lists contain the username as a string ("domain\name"), but some lists only count on the "Created By" column, which is auto-created.

I want to know what's the fastest way to query these lists using the username string. I tried to use the same query as the one I use for the first kind of lists and it obviously didn't work -

<Where><Eq><FieldRef Name='UserName'/><Value Type='Text'>domain\\username</Value></Eq></Where>

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恏ㄋ傷疤忘ㄋ疼 2024-09-09 19:02:42

有两个不同的“创建者”字段,一个用于所有项目,另一个专门用于文档库(“文档创建者”)。 “创建者”字段的内部名称是“作者”,因此 将是最好的开始。第二个字段是文档库中实际文件的作者,其内部名称为“Created_x0020_By”。从你的场景来看,我有一种感觉你只需要前者,但无论如何知道后者是很好的,因为它们存储的数据是不同的。

“创建者”是一个用户字段,因此其数据的字符串版本为ID;#Full Name。同时,“文档创建者”是单行文本的文本字段,其数据存储为实际用户名(带有域,如果适用)。因此,您的上述查询适用于文档库并在 上进行搜索。但是,如果您想在“创建者”字段中进行搜索,则需要稍微复杂一些,但您应该能够通过引用用户的 SharePoint ID 来完成此操作。以下是我正在使用的不同用户字段查询的示例,以进行比较。

<Eq><FieldRef Name='AssignedTo' /><Value Type='Integer'><UserID Type='Integer' /></Value></Eq>

这专门过滤掉了当前用户。要将其用于您的目的,请将 'AssignedTo' 替换为 'Author',并将 替换为相关用户的 ID。

There are two different "Created By" fields, one for all items and one specifically for document libraries ("Document Created By"). The internal name for the "Created By" field is "Author", so <FieldRef Name='Author'/> would be the best start. The second field, which is the author of the actual file in a document library, has an internal name of "Created_x0020_By". Judging from your scenario I have a feeling you'll only need the former, but the latter is good to know anyway, because the data stored in them is different.

"Created By" is a user field, so the string version of its data is ID;#Full Name. Meanwhile, "Document Created By" is a text field for a single line of text, and its data is stored as the actual username (with domain, if applicable). So your above query would work in the case of a document library and searching on <FieldRef Name='Created_x0020_By'/>. If you wanted to search on the "Created By" field, however, you'll have to be a bit trickier, but you should be able to accomplish it by referencing the user's SharePoint ID. The following is an example of a different User field query that I am using, for comparison.

<Eq><FieldRef Name='AssignedTo' /><Value Type='Integer'><UserID Type='Integer' /></Value></Eq>

This specifically filtered out the current user. To use it for your purposes, replace 'AssignedTo' with 'Author' and <UserID Type='Integer' /> with the ID of the user in question.

浮生未歇 2024-09-09 19:02:42

我建议执行以下操作:

  1. 通过调用 EnsureUser 方法获取 SharePoint 用户,即:

     SPUser myUser = SPContext.Current.Web.EnsureUser(@"DOMAIN\USERNAME");
    
  2. 通过SharePoint-User的ID查询列表:

     SPQuery spQuery = new SPQuery();
     StringBuilder queryString = new StringBuilder(String.Empty);
     queryString.Append(@"");
     queryString.Append(@" ");
     queryString.Append(@" "); //作者字段
     queryString.Append(@" " + myUser.ID + @"");
     queryString.Append(@" ");
     queryString.Append(@"");
     spQuery.Query = queryString.ToString();
    
  3. 针对列表触发查询

    myList.GetItems(spQuery);
    

I would recommend the following:

  1. Get the SharePoint-User by invoking the EnsureUser method, i.e.:

     SPUser myUser = SPContext.Current.Web.EnsureUser(@"DOMAIN\USERNAME");
    
  2. Query the list by using the ID of the SharePoint-User:

     SPQuery spQuery = new SPQuery();
     StringBuilder queryString = new StringBuilder(String.Empty);
     queryString.Append(@"<Where>");
     queryString.Append(@"   <Eq>");
     queryString.Append(@"       <FieldRef ID=""{1df5e554-ec7e-46a6-901d-d85a3881cb18}"" LookupId=""True"" />"); //Author Field
     queryString.Append(@"       <Value Type=""Lookup"">" + myUser.ID + @"</Value>");
     queryString.Append(@"   </Eq>");
     queryString.Append(@"</Where>");
     spQuery.Query = queryString.ToString();
    
  3. Fire the query against the list

    myList.GetItems(spQuery);
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文