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.

My SharePoint list has one of column (field) named 'Title' or it can be any other name with text type.

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. I am creating my sample application in C# using Microsoft.SharePoint.Client and Microsoft.SharePoint.Client.Runtime dlls.

PROBLEM: I want to get a specific item in the list on the basis of the value of 'Title' column using CAML query. My List may contains thousands of items, Using CAML Query :

"<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. 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 `

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. this exception is generated when I try to fetch Item through the 'Title' instead of 'ID' field, in the FieldRef Name parameter.

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

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? CAML query is case sensitive

链接地址: http://www.djcxy.com/p/64148.html

上一篇: SharePoint 2013 CAML查询OrderBy未正确排序

下一篇: 使用CAML查询获取特定的SharePoint Office 365列表项目?