如何获得正确的SPFieldUservalue以使用SpsitedataQuery为某些用户汇总任务?
-
10-12-2019 - |
题
我正在尝试为某个用户收集某个内容类型的所有任务。要完成此目的,我正在使用spsitedataquery,它看起来像这样:
string where = String.Format(@"<Where>
<And>
<BeginsWith>
<FieldRef Name='ContentTypeId'/>
<Value Type='Text'>0x0108010031C20CDC28544f93B27958D36ACBE2F5</Value>
</BeginsWith>
<Eq>
<FieldRef Name='AssignedTo'/>
<Value Type='Text'>{0}</Value>
</Eq>
</And>
</Where>", userValue.ToString());
query.Query = where;
DataTable results = web.GetSiteData(query);
.
如果我只想过滤内容类型但是过滤用户失败的情况下,它会正常工作。
这里是我认为问题是..分配的字段包含视图的值
17;#TestUser01
.
但我的常规loginname包含索赔字符串和域,看起来像这样
i:0#.w|test\TestUser01
.
我试图创建spfielduservalue以获取位于AssendedTo-Field中找到的相同的用户字符串..如此:
SPFieldUserValue userValue = new SPFieldUserValue(web, SPContext.Current.Web.CurrentUser.ID, SPContext.Current.Web.CurrentUser.LoginName);
.
..但是我只是获得一个如下所示值:
17;#i:0#.w|test\TestUser01
.
..而且我仍然没有来自我的查询的结果..
如何获得正确的字符串来执行我的查询?
解决方案
By changing the CAML like this:
string where = @"<Where>
<And>
<BeginsWith>
<FieldRef Name='ContentTypeId'/>
<Value Type='Text'>0x0108010031C20CDC28544f93B27958D36ACBE2F5</Value>
</BeginsWith>
<Eq>
<FieldRef Name='AssignedTo' LookupId='TRUE' />
<Value Type='Integer'><UserID /></Value>
</Eq>
</And>
</Where>";
query.Query = where;
DataTable results = web.GetSiteData(query);
it will fetch for current user :)
If you need to specify another user you can use:
string where = String.Format(@"<Where>
<And>
<BeginsWith>
<FieldRef Name='ContentTypeId'/>
<Value Type='Text'>0x0108010031C20CDC28544f93B27958D36ACBE2F5</Value>
</BeginsWith>
<Eq>
<FieldRef Name='AssignedTo' LookupId='TRUE' />
<Value Type='Integer'>{0}</Value>
</Eq>
</And>
</Where>", userValue.ToString());
query.Query = where;
DataTable results = web.GetSiteData(query);
and just make sue you have the ID of the user in userValue
其他提示
The CAML Query for fetching the values for current user is:
"<Where><Eq><FieldRef Name='AssignedTo' /><Value Type='Integer'><UserID Type='Integer' /></Value></Eq></Where>";
And for another user it should be as mentioned by @Robert.