【发布时间】:2015-08-04 14:06:21
【问题描述】:
我正在创建一个示例程序来检查 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 是什么,因此我想通过我的“标题”字段获取它, 为此我尝试了以下 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 参数中传递它时,一个或多个字段类型未正确安装
我的代码sn-p如下
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;
}
}
如果我能从任何了解此问题的人那里获得有关此问题的帮助,那就太好了。
【问题讨论】:
标签: c# sharepoint office365