I ran into a frustrating scenario today, while working with SharePoint 2010’s Managed Client Object Model. My application queries a SharePoint document library using the file’s name (the FileLeafRef field). Given that this field is unique, I was expecting only one result with the following code:
1 CamlQuery qry = new CamlQuery();
2
3 //filter the results to only get back the item with the filename we're looking for
4 qry.ViewXml = string.Format(
5 "<Query><Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='File'>{0}</Value></Eq></Where></Query>",
6 remoteFileName);
7
8 ListItemCollection itms = lst.GetItems(qry);
9 ctx.Load(itms);
10 ctx.ExecuteQuery();
Instead of getting one (or none) records, I was getting more than one. SO, I searched some more and found some examples with slightly different xml:
1 CamlQuery qry = new CamlQuery();
2
3 //filter the results to only get back the item with the filename we're looking for
4 qry.ViewXml = string.Format(
5 "<View><Query><Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='File'>{0}</Value></Eq></Where></Query></View>",
6 remoteFileName);
7
8 ListItemCollection itms = lst.GetItems(qry);
9 ctx.Load(itms);
10 ctx.ExecuteQuery();
See the difference? Look carefully at line 5 – that’s right. I left out the surrounding “<View>” tags!