[英]To fetch a specific SharePoint Office 365 list item using CAML query?
我正在创建一个示例程序来检查Sharepoint列表中ListItem的存在。
我的SharePoint列表具有名为“标题”的列(字段)之一,也可以是文本类型的任何其他名称。
我知道,当我们在SharePoint中为列表中的每个项目创建一个列表时,SharePoint列表项目本身都会分配一个“ ID”字段。 我正在使用Microsoft.SharePoint.Client和Microsoft.SharePoint.Client.Runtime dll在C#中创建示例应用程序。
问题:我想使用CAML查询基于“标题”列的值在列表中获得特定项目。 我的列表可能包含数千个项目, 使用CAML查询:
"<View><Query><Where><Leq>" +
"<FieldRef Name='ID'/><Value Type='Number'>100</Value>" +
"</Leq></Where></Query><RowLimit>7000</RowLimit></View>"
我成功检索ListItemCollection并抛出可以获取ListItem的提示。 但是遍历整个列表以获得特定项是非常耗时且低效的方式。
尽管可以使用CAML查询通过ID字段在列表中获取特定项目,但是由于我不知道该项目的ID是什么,因此我想通过“ Title”字段来获取该项目, 为此尝试了以下CAML查询 `
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View><Query><Where><eq>" +
"<FieldRef Name='TITLE'/><Value Type='TEXT'>2</Value>" +
"</eq></Where></Query></View>";`
但是运行代码时出现以下异常
1.值不在预期范围内。 当我尝试通过FieldRef Name参数中的“标题”而不是“ ID”字段获取项目时,会生成此异常。
2.当我在列表中手动创建列并在FieldRef Name参数中传递它时, 一种或多种字段类型未正确安装
我的代码段如下
class sharepoint1
{
ClientContext context = null;
string OBJECTMETATADTA_ID = "Title";
private class Configuration
{
public static string ServiceSiteUrl = "";
public static string ServiceUserName = "";
public static string ServicePassword = "";
}
private ListItemCollection getListItemCollectionFromSP(String listName)
{
Web oWebsite = context.Web;
ListCollection collList = oWebsite.Lists;
List oList = collList.GetByTitle(listName);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View><Query><Where><eq>" +
"<FieldRef Name='Title'/><Value Type='Text'>abc</Value>" +
"</eq></Where></Query></View>";
ListItemCollection collListItem = oList.GetItems(camlQuery);
context.Load(collListItem,
items => items.IncludeWithDefaultProperties(
item => item.DisplayName));
context.ExecuteQuery();
return collListItem;
}
ListItem checkItem(string listname, string fileName)
{
ListItem result = null;
ListItemCollection collListItem =
getListItemCollectionFromSP(listname);
string itemName = fileName.Substring(0, 3);
foreach (ListItem oListItem in collListItem)
{
if (oListItem[OBJECTMETATADTA_ID].Equals(itemName))
{
{
// my business logic;
}
}
}
context.Dispose();
return result;
}
}
如果我从任何知道这一点的人那里得到一些帮助,那将是非常好的。
您是否在查询中尝试使用“ Eq”而不是“ eq”? CAML查询区分大小写
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.