BreakHead
BreakHead

Reputation: 10672

Search for a Document by its Name using SharePoint Lists.asmx GetListItems

Here is my code:

StringBuilder sb = new StringBuilder();
sb.AppendLine(@"<Where><Lt>");
sb.AppendLine(@"<FieldRef Name=""FileRef"" /><Value Type=""Text"">momo.txt</Value>");
sb.AppendLine(@"</Lt></Where>");

XmlDocument xmlDoc = new System.Xml.XmlDocument();

XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
ndQuery.InnerXml = sb.ToString();

XmlNode resultsElement1 = listsProxy.GetListItems("Documents", "", ndQuery,
                                                  null, null, null, "");

But it returns all the Documents any Idea what I am doing wrong here?

Thanks.

Upvotes: 2

Views: 3606

Answers (2)

Truong Hong Thi
Truong Hong Thi

Reputation: 384

The FileRef fields contains the server-relative URL to the document, something like this "/Subsite/Lists/ListName/DocumentName". If you provide only the file name (e.g. mono.txt), neither Lt or Eq should work.

To fix the problem, you might try:

  1. Use Eq with the server-relative URL instead of file name.
sb.AppendLine(@"<Where><Eq>");  
sb.AppendLine(@"<FieldRef Name='FileRef' /><Value Type='Text'>/SubSite/Lists/Documents/momo.txt</Value>");  
sb.AppendLine(@"</Eq></Where>");
  1. Or you might use <Contains> operator (CAML does not have <EndsWith> operator).
sb.AppendLine(@"<Where><Contains>");  
sb.AppendLine(@"<FieldRef Name='FileRef' /><Value Type='Text'>/momo.txt</Value>");  
sb.AppendLine(@"</Contains></Where>");

Then looping over the returned results, and remove items which does not end with "/mono.txt".

In addition, if you care about document name without extension, you could you BaseName instead of FileRef field.

Upvotes: 3

James
James

Reputation: 41

Currently your query is for items where the name value is less than momo.text rather than equal to it. Therefore change Lt to Eq.

Upvotes: 4

Related Questions