简体   繁体   English

要使用CAML查询获取特定的SharePoint Office 365列表项?

[英]To fetch a specific SharePoint Office 365 list item using CAML query?

I am creating a sample program to check ListItem existence at Sharepoint list. 我正在创建一个示例程序来检查Sharepoint列表中ListItem的存在。

My SharePoint list has one of column (field) named 'Title' or it can be any other name with text type. 我的SharePoint列表具有名为“标题”的列(字段)之一,也可以是文本类型的任何其他名称。

I Know that when we create a list at SharePoint for each item in the list a "ID" field is assigned by SharePoint list item itself. 我知道,当我们在SharePoint中为列表中的每个项目创建一个列表时,SharePoint列表项目本身都会分配一个“ ID”字段。 I am creating my sample application in C# using Microsoft.SharePoint.Client and Microsoft.SharePoint.Client.Runtime dlls. 我正在使用Microsoft.SharePoint.ClientMicrosoft.SharePoint.Client.Runtime dll在C#中创建示例应用程序。

PROBLEM: I want to get a specific item in the list on the basis of the value of 'Title' column using CAML query. 问题:我想使用CAML查询基于“标题”列的值在列表中获得特定项目。 My List may contains thousands of items, Using CAML Query : 我的列表可能包含数千个项目, 使用CAML查询:

"<View><Query><Where><Leq>" +
           "<FieldRef Name='ID'/><Value Type='Number'>100</Value>" +
           "</Leq></Where></Query><RowLimit>7000</RowLimit></View>"

I am successfully retrieving ListItemCollection and throw that I can get ListItem. 我成功检索ListItemCollection并抛出可以获取ListItem的提示。 But its very time consuming and inefficient way, to traverse whole list to get particular item. 但是遍历整个列表以获得特定项是非常耗时且低效的方式。

Although it is possible to get a specific item in list through the ID field using CAML query, but as i don't know what is the ID of the Item, therefore i want to fetch it through my "Title" field, for this i tried the following CAML Query ` 尽管可以使用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>";` 

but I get the following exceptions when I run the code 但是运行代码时出现以下异常

1. Value does not fall within the expected range. 1.值不在预期范围内。 this exception is generated when I try to fetch Item through the 'Title' instead of 'ID' field, in the FieldRef Name parameter. 当我尝试通过FieldRef Name参数中的“标题”而不是“ ID”字段获取项目时,会生成此异常。

2. One or more field types are not installed properly when i created a coloumn manually in the list, and passed it in the FieldRef Name parameter 2.当我在列表中手动创建列并在FieldRef Name参数中传递它时, 一种或多种字段类型未正确安装

My code snippet is as follows 我的代码段如下

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

    }

It would be great if I get some help on this Issue from any person who knowns about this. 如果我从任何知道这一点的人那里得到一些帮助,那将是非常好的。

Did you try with 'Eq' instead of 'eq' in your query? 您是否在查询中尝试使用“ Eq”而不是“ eq”? CAML query is case sensitive CAML查询区分大小写

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM