简体   繁体   English

CAML查询以从SPList获取项目

[英]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 . FiledRef应该是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> . 小修改:我不确定CAML是否区分大小写,但万一是这样的话:将开头位置更改为<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. 下面的函数获取项目存储文件夹= item.folder的子文件夹的文件夹名称和ID。

Initially it will be null. 最初它将为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. 下面的函数获取项目存储文件夹= item.folder的子文件夹的文件夹名称和ID。 Initially it will be null. 最初它将为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 感谢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. 但是我知道在SharePoint 2013中使用CAML时绝对区分大小写。

Just means that the opening <where> should be <Where> . 仅表示开头<where>应该是<Where>

Hope you got your issue solved! 希望您能解决您的问题!

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

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