简体   繁体   English

如何使用SPQuery从SharePoint文档库中获取单个项目(不使用for循环)?

[英]How to get single item from SharePoint document library using SPQuery (without using for loop)?

Sometimes there is a requirement to get only one SharePoint item from a large document library. 有时需要从大型文档库中仅获取一个SharePoint项目。 So, how to get single item from SharePoint document library using SPQuery (without using for loop)? 那么,如何使用SPQuery从SharePoint文档库中获取单个项目(不使用for循环)?

SPList class has several methods for getting only single item: SPList类具有几种仅获取单个项目的方法:

Except the last one they need integer ID (ID property of SPListItem) of item. 除了最后一个之外,它们还需要项的整数ID(SPListItem的ID属性)。 The last one uses the guid ID (UniqueId property of SPListItem). 最后一个使用GUID ID(SPListItem的UniqueId属性)。

Here is the C# code to achieve the above requirement: 这是达到上述要求的C#代码:

public SPListItemCollection GetSpecificLibraryItem(fileName)
{
SPList list = web.Lists["MyDocName"];
SPQuery dQuery = new SPQuery();
dQuery.ViewAttributes = "Scope=\"Recursive\"";
string QueryString = "<Where>" +
                      "<Eq>" +
                        "<FieldRef Name=\"FileLeafRef\"/>" +
                        "<Value Type=\"Text\">" + fileName + "</Value>" +
                      "</Eq>" +
                     "</Where>";
         dQuery.Query = QueryString;
        SPListItemCollection collListItems = list.GetItems(dQuery);
  return collListItems;
}

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

相关问题 有没有一种方法可以确定单个SharePoint列表项是否与SPQuery / SPView匹配? - Is there a way to determine if a single SharePoint list item matches a SPQuery/SPView? 如何在使用 C# 将文档作为项目上传到 SharePoint 文档库时为自定义列添加值? - How to add value to a custom column while uploading document into a SharePoint document library as an item using C#? "Sharepoint 文档库使用 REST API 获取文件计数" - Sharepoint Document Library get file count using REST API 如何使用C#将文件上传到Sharepoint在线文档库 - How to upload files to sharepoint online document library using c# 如何使用C#从SharePoint 2013文档库中删除用户权限 - How to remove user permissions from a SharePoint 2013 document library using c# 使用SharePoint工作流在文档库中创建文件夹 - Create folder in document library using SharePoint workflow 如何在不使用 C# 中字符串数组的循环的情况下获取每个项目 - how to get each item without using a loop from string array in c# 使用sharepoint文档ID获取文档名称 - get document name using sharepoint document id 在不使用 item.Document.FullName 的情况下获取 ProjectItem 路径 - Get ProjectItem path without using item.Document.FullName 如何使用 SharePoint CSOM 获取文档预览图像 - How to get document preview image using SharePoint CSOM
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM