C# 从 SPList 获取项目的 CAML 查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12800694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:30:47  来源:igfitidea点击:

CAML query to get items from SPList

c#sharepoint-2010

提问by Rizwan

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

采纳答案by Abbas

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

FiledRef应该是FieldRef。而你忘记了等号,应该是这样的:

<FieldRef Name='Department' />

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

回答by Dmitry Dovgopoly

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

回答by Devarajan S M

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

下面的函数获取文件夹名称和子文件夹的 id,其中项目存储文件夹 = 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());     
        }  

回答by Devarajan S M

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

下面的函数获取文件夹名称和子文件夹的 id,其中项目存储文件夹 = item.folder。最初它将为空。

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

回答by SchwabTheDeck

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!

希望你的问题得到解决!