如何在文档集中搜索文件?
How to search for a file in a document set?
我正在尝试编写 CAML 查询以在特定文档集中按文件名查找特定文件。
这是我正在处理的查询:
<Query>
<Where>
<Eq>
<FieldRef Name='FileLeafRef' />
<Value Type='File'>TestFile.txt</Value>
</Eq>
</Where>
</Query>
<QueryOptions>
<Folder>https://acme.com/sites/mysite/MyDocLib/MyDocSetName</Folder>
</QueryOptions>
由于在名为 MyDocSetName 的文档集中有一个名为 TestFile.txt 的文件,我希望得到结果,但没有任何结果。我做错了什么?
基于 this demo.
的示例测试脚本
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
$.getScript(scriptbase + "SP.Runtime.js", function() {
$.getScript(scriptbase + "SP.js", function() {
$.getScript(scriptbase + "SP.DocumentManagement.js", createDocumentSet);
});
});
});
var docSetFiles;
function createDocumentSet() {
//Get the client context,web and library object.
clientContext = new SP.ClientContext.get_current();
oWeb = clientContext.get_web();
var oList = oWeb.get_lists().getByTitle("DocSet");
clientContext.load(oList);
//Get the root folder of the library
oLibraryFolder = oList.get_rootFolder();
var documentSetFolder = "/DocSet/test1";
//Get the document set files using CAML query
var camlQuery = SP.CamlQuery.createAllItemsQuery();
camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>test2.docx</Value></Eq></Where></Query></View>");
camlQuery.set_folderServerRelativeUrl(documentSetFolder);
docSetFiles = oList.getItems(camlQuery);
//Load the client context and execute the batch
clientContext.load(docSetFiles, 'Include(File)');
clientContext.executeQueryAsync(QuerySuccess, QueryFailure);
}
function QuerySuccess() {
//Loop through the document set files and get the display name
var docSetFilesEnumerator = docSetFiles.getEnumerator();
while (docSetFilesEnumerator.moveNext()) {
var oDoc = docSetFilesEnumerator.get_current().get_file();
console.log("Document Name : " + oDoc.get_name());
}
}
function QueryFailure() {
console.log('Request failed - ' + args.get_message());
}
</script>
我正在尝试编写 CAML 查询以在特定文档集中按文件名查找特定文件。
这是我正在处理的查询:
<Query> <Where> <Eq> <FieldRef Name='FileLeafRef' /> <Value Type='File'>TestFile.txt</Value> </Eq> </Where> </Query> <QueryOptions> <Folder>https://acme.com/sites/mysite/MyDocLib/MyDocSetName</Folder> </QueryOptions>
由于在名为 MyDocSetName 的文档集中有一个名为 TestFile.txt 的文件,我希望得到结果,但没有任何结果。我做错了什么?
基于 this demo.
的示例测试脚本<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
$.getScript(scriptbase + "SP.Runtime.js", function() {
$.getScript(scriptbase + "SP.js", function() {
$.getScript(scriptbase + "SP.DocumentManagement.js", createDocumentSet);
});
});
});
var docSetFiles;
function createDocumentSet() {
//Get the client context,web and library object.
clientContext = new SP.ClientContext.get_current();
oWeb = clientContext.get_web();
var oList = oWeb.get_lists().getByTitle("DocSet");
clientContext.load(oList);
//Get the root folder of the library
oLibraryFolder = oList.get_rootFolder();
var documentSetFolder = "/DocSet/test1";
//Get the document set files using CAML query
var camlQuery = SP.CamlQuery.createAllItemsQuery();
camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>test2.docx</Value></Eq></Where></Query></View>");
camlQuery.set_folderServerRelativeUrl(documentSetFolder);
docSetFiles = oList.getItems(camlQuery);
//Load the client context and execute the batch
clientContext.load(docSetFiles, 'Include(File)');
clientContext.executeQueryAsync(QuerySuccess, QueryFailure);
}
function QuerySuccess() {
//Loop through the document set files and get the display name
var docSetFilesEnumerator = docSetFiles.getEnumerator();
while (docSetFilesEnumerator.moveNext()) {
var oDoc = docSetFilesEnumerator.get_current().get_file();
console.log("Document Name : " + oDoc.get_name());
}
}
function QueryFailure() {
console.log('Request failed - ' + args.get_message());
}
</script>