查询以查找文件夹是否存在

本文关键字:是否 存在 文件夹 查找 查询 | 更新日期: 2023-09-27 18:04:34

我有这个查询,试图在Sharepoint内容数据库中的AllDocs表中找到这个记录(我知道存在):

记录有以下值

DirName:     /MYSITE/MYLIST/A_FOLDER
LeafName:    B_FOLDER

所以URL看起来像:/MYSITE/MYLIST/A_FOLDER/B_FOLDER

这是我要写的方法:

public bool CheckFolderExist(string folderPath)
{
    var pathArray = folderPath.Split('/');
    string folderName = pathArray.Last(); //The 'LeafName' part...
    var temp = pathArray.Take(pathArray.Length - 1);
    string folderOnly = string.Join("/", temp); //The 'DirName' part...
    CamlQuery cq = new CamlQuery();
    string query = @"<View>
                         <ViewAttributes Scope='RecursiveAll' />
                         <Query>
                            <Where>
                                <And>
                                        <Eq>
                                            <FieldRef Name='FileLeafRef' />
                                            <Value Type='Text'>{0}</Value>
                                        </Eq>
                                        <Eq>
                                            <FieldRef Name='FileDirRef' />
                                            <Value Type='Text'>{1}</Value>
                                        </Eq>
                                </And>
                            </Where>
                         </Query>
                       </View>";
    query = string.Format(query, folderName, folderOnly);
    cq.ViewXml = query;
    //_accountList is initiated somewhere else for the moment, and it doesn't seem to be the problem for now...
    ListItemCollection colFolder = _accountList.GetItems(cq);
    _ctx.Load(colFolder);
    _ctx.ExecuteQuery();
    if (colFolder.Count == 0)
        return false;
    else
        return true;
}

我的方法总是返回false。我只能想到我可能在CAML查询中使用了错误的字段名。

有什么想法吗?

谢谢。

查询以查找文件夹是否存在

我最终纠正了CAML查询:

string query = @"<View Scope='RecursiveAll'><Query><Where>
                                                <And>
                                                        <Eq>
                                                            <FieldRef Name='FSObjType' />
                                                            <Value Type='Text'>1</Value>
                                                        </Eq>
                                                        <Eq>
                                                            <FieldRef Name='FileRef' />
                                                            <Value Type='Text'>{0}</Value>
                                                        </Eq>
                                                </And>
                                            </Where></Query></View>";