If the list of possible file names is known we can build a query and have SharePoint return the files that exist. In this case I don't need a reusable function, so I decided that I would take the chance and use an area of JavaScript that I almost never get to utilize: self-referencing anonymous functions or recursive anonymous functions. Below is my implementation, it writes the result of the function to the FireBug/IE Developer Toolbar console.
NOTE: Below there is an issue with the syntax highlighter functionality. If you copy and paste the test below, you must change the "" + field + "" to '" + field + "' and the Type="Text" to Type='Text'
Hopefully there will be a fix to the rendering functionality, but until then, there we are.
1 2 3 4 5 6 7 8 | files = [ "file3.pdf" , "file1.pdf" , "file2.pdf" ]; ors = ( function buildOR(field, filelist) { var BuildEq = function (field, value) { return "<eq><fieldref name=" " + field + " "><value type=" Text ">" + value + "</value></fieldref></eq>" ; } return (filelist.length == 1) ? BuildEq(field, filelist.shift()) : "<or>" + BuildEq(field, filelist.shift()) + buildOR(field, filelist) + "</or>" ; })( "FileLeafRef" , files) console.log(ors); //buildOR("FileLeafRef", files); |
Running this example produces the following output (I have formatted it for readability). Note: The FieldRef nodes are self closing, the syntax highlighter appears does not handle these correctly, so I have changed the self closing nodes to accommodate the limitation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | < or > < or > < eq > < fieldref name = "FileLeafRef" ></ fieldref > < value type = "Text" >file3.pdf</ value > </ eq > < or > < eq > < fieldref name = "FileLeafRef" ></ fieldref > < value type = "Text" >file1.pdf</ value > </ eq > < eq > < fieldref name = "FileLeafRef" ></ fieldref > < value type = "Text" >file2.pdf</ value > </ eq > </ or > </ or > </ or > |
No comments:
Post a Comment