简体   繁体   中英

CAML query to get items from SPList

I wanted to update the Name field in a list which contain 'finance' in the department list. I wrote the following code but it updates every item in the Name column whether it contains 'finance' or not.

What am i doing wrong?

SPListItemCollection Items = RiskAssesment.GetItems(new SPQuery()
{
     Query = @"<where>
                   <Eq>
                       <FiledRef Name 'Department'/>
                       <Value Type='Text'>Finance </Value>
                   </Eq>
               </Where>"
});

foreach (SPListItem item in Items)
{
    item["Name"] = "abcdef";
    item.Update();
}     

FiledRef should be FieldRef . And you forgot the equal sign, it should be like this:

<FieldRef Name='Department' />

Small edit: I'm not sure if CAML is case-sensitive, but in case it is: change the opening-where to <Where> .

SPListItemCollection Items = RiskAssesment.GetItems(new SPQuery()
{
     Query = @"<Where>
     <Eq>
         <FieldRef Name='Department'/>
         <Value Type='Text'>Finance </Value></Eq></Where>"
 });

  foreach (SPListItem item in Items)
  {
         item["Name"]="abcdef";
         item.Update();
  }  

The below function get the folder name and id of subfolder where items stored folder = item.folder.

Initially it will be null.

static string GetParentFolder(SPListItem itemToFind, SPFolder folder)  
        { 
            SPQuery query = new SPQuery(); 
           // query.Query =  "<OrderBy><FieldRef Name='Title'/></OrderBy>";
            query.Query = "<Where><Eq><FieldRef Name=\"ID\"/><Value Type=\"Integer\">"+ itemToFind.ID +"</Value></Eq></Where>";
            query.Folder = folder;
            query.ViewAttributes = "Scope=\"Recursive\"";
            SPListItemCollection items = itemToFind.ParentList.GetItems(query);
            int intpartentFolderID=0 ;
            if (items.Count > 0)
            {
            foreach (SPListItem item in items) 
            {

                SPFile f = item.Web.GetFile(item.Url);

                string test11 = f.ParentFolder.Name;
                intpartentFolderID = f.ParentFolder.Item.ID;

                //string test1 = item.File.ParentFolder.Name;

                 return (intpartentFolderID.ToString()); 

             }
            }
            return (intpartentFolderID.ToString());     
        }  

The below function get the folder name and id of subfolder where items stored folder = item.folder. Initially it will be null.

static string GetParentFolder(SPListItem itemToFind, SPFolder folder)  
        { 
           SPQuery query = new SPQuery(); 
           // query.Query =  "<OrderBy><FieldRef Name='Title'/></OrderBy>";
            query.Query = "<Where><Eq><FieldRef Name=\"ID\"/><Value Type=\"Integer\">"+ itemToFind.ID +"</Value></Eq></Where>";
            query.Folder = folder;
            query.ViewAttributes = "Scope=\"Recursive\"";
            SPListItemCollection items = itemToFind.ParentList.GetItems(query);
            int intpartentFolderID=0 ;
            if (items.Count > 0)
            {
            foreach (SPListItem item in items) 
            {
                SPFile f = item.Web.GetFile(item.Url);

                string test11 = f.ParentFolder.Name;
                intpartentFolderID = f.ParentFolder.Item.ID;

                //string test1 = item.File.ParentFolder.Name;

                 return (intpartentFolderID.ToString()); 

             }
            }
            return (intpartentFolderID.ToString());     
        }  

Thanks Deva- Cheraideva

So I just came across this older post, and I don't have enough reputation to comment on the selected answer.

But I know that when using CAML is definitely case sensitive in SharePoint 2013.

Just means that the opening <where> should be <Where> .

Hope you got your issue solved!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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