[英]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.