2011-11-18 47 views
4

我正在尝试从唯一ID(GUID)获取SPListItem对象。从查看几个站点(包括http://sharepoint400.blogspot.com/2011/04/using-spsitedataquery-to-find-list.htmlhttp://www.chakkaradeep.com/post/Retrieving-an-Item-from-the-RootWeb-and-Subwebs-using-its-UniqueId.aspx),我想出了下面的代码。SharePoint:通过唯一ID获取SPListItem

const string QueryFormat = 
@"<Where> 
    <Eq> 
     <FieldRef Name='UniqueId' /> 
     <Value Type='Lookup'>{0}</Value> 
    </Eq> 
</Where>"; 

      SPSiteDataQuery query = new SPSiteDataQuery(); 
      query.Webs = "<Webs Scope='SiteCollection' />"; 
      query.Lists = "<Lists BaseType='0'/>"; 
      query.Query = string.Format(QueryFormat, itemUniqueId); 
      query.RowLimit = 1; 
      //query.ViewFields = "<FieldRef Name='WebID' /><FieldRef Name='ListID' /><FieldRef Name='ID' />"; 

      var results = SPContext.Current.Web.GetSiteData(query); 

但是,无论如何...我似乎总是返回零行。我不明白为什么,因为我知道我使用的Guids是正确的。

任何想法?

+0

您是否考虑过向查询添加递归类型?你能不能告诉我们你在GUID中传递的方式和位置? 你只关心列表项目而不是列表,网站或网站?你知道该项目的目标列表或网页吗? 您是否仅限于使用CAML?你知道'$ listItem = $ spList.GetItemByUniqueId($ targetGuid)'方法吗? 正如你所看到的,在任何人都能提供有意义的答案之前,有很多问题需要答案。 – Panoone

回答

-2

请尝试删除QueryFormat字符串中的所有空格。使用类似这样的代替:

"<Where><Eq><FieldRef Name=\"UniqueId\" /><Value Type=\"Lookup\">{0}</Value></Eq></Where>" 

SharePoint不喜欢这些空间。

我也建议使用库来产生CAML查询: http://camldotnet.codeplex.com/http://camlex.codeplex.com/

-1

存在无效类型唯一识别码,应该GUID。 更新您的查询:

<Where> 
    <Eq> 
     <FieldRef Name='UniqueId'/> 
     <Value Type='Guid'>{0}</Value> 
    </Eq> 
</Where> 
+0

这是不正确的。 UniqueId的字段Type和TypeAsString确实是Lookup。 – Panoone