2
votes

I'm using SharePoint web services in C#. I have my code working to check files and check them out using the Lists web service. I need to test to see if a file exists; I can find lots of examples for doing this using the object model API, but I can't seem to find a straightforward way of doing this using web services.

3

3 Answers

3
votes

Try the Lists.GetListItems with a suitable CAML query.

A CAML query like

<Query><Where><Eq><FieldRef Name="FileLeafRef" /><Value Type="Text">Filename.rtf</Value></Eq></Where></Query>

should work; the field 'FileLeafRef' is where the filename is stored.

0
votes

This code may do, it's a little rough, but demonstrates how to get a list of files based on the title.

        public static bool PageExists(string listName, string webPath, string pageTitle)
        {
            string pageId = "";
            IntranetLists.Lists lists = new IntranetLists.Lists();
            lists.UseDefaultCredentials = true;
            lists.Url = webPath + "/_vti_bin/lists.asmx";
            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<Document><Query><Where><Contains><FieldRef Name=\"Title\" /><Value Type=\"Text\">" + pageTitle + "</Value></Contains></Where></Query><ViewFields /><QueryOptions /></Document>");
            XmlNode listQuery = doc.SelectSingleNode("//Query");
            XmlNode listViewFields = doc.SelectSingleNode("//ViewFields");
            XmlNode listQueryOptions = doc.SelectSingleNode("//QueryOptions");

            Guid g = GetWebID(webPath);

            XmlNode items = lists.GetListItems(listName, string.Empty, listQuery, listViewFields, string.Empty, listQueryOptions, g.ToString());

            }
            return items.Count > 0;            
        }

        public static XmlNodeList XpathQuery(XmlNode xmlToQuery, string xPathQuery)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xmlToQuery.OuterXml);
            XmlNamespaceManager mg = new XmlNamespaceManager(doc.NameTable);
            mg.AddNamespace("sp", "http://schemas.microsoft.com/sharepoint/soap/");
            mg.AddNamespace("z", "#RowsetSchema");                                   
            mg.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
            mg.AddNamespace("y", "http://schemas.microsoft.com/sharepoint/soap/ois");
            mg.AddNamespace("w", "http://schemas.microsoft.com/WebPart/v2");
            mg.AddNamespace("d", "http://schemas.microsoft.com/sharepoint/soap/directory");
            return doc.SelectNodes(xPathQuery, mg);
        }
0
votes

I also had similiar problems with this. I have tried the following FieldRefs without success: "Name", "FileLeafRef" and "LinkFilenameNoMenu".

The post located at http://www.johanolivier.blogspot.com details what I had to do to get it working.