【问题标题】:SharePoint: Get SPListItem by Unique IDSharePoint:按唯一 ID 获取 SPListItem
【发布时间】:2011-11-18 11:16:36
【问题描述】:

我正在尝试从唯一 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),我想出了下面的代码。

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);

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

有什么想法吗?

【问题讨论】:

  • 您是否考虑过在查询中添加递归类型?您能否向我们展示您在 GUID 中传递的方式和位置以及以何种形式传递?您只关心列表项而不关心列表、网站或网站吗?您知道该项目的目标列表或网络吗?您是否仅限于使用 CAML?你知道$listItem = $spList.GetItemByUniqueId($targetGuid) 方法吗?如您所见,有许多问题需要回答才能提供有意义的答案。

标签: c# sharepoint


【解决方案1】:

唯一 ID 的类型无效,应该是 Guid。 更新您的查询:

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

【讨论】:

  • 这是不正确的。 UniqueId 的 Type 和 TypeAsString 字段确实是 Lookup。
【解决方案2】:

请尝试删除 QueryFormat 字符串中的所有空格。改用这样的东西:

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

SharePoint 不喜欢这些空间。

我还建议使用其中一个库来生成 Caml 查询,例如: http://camldotnet.codeplex.com/ 要么 http://camlex.codeplex.com/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-26
    • 2014-08-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多