views:

2236

answers:

3

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.

+2  A: 

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.

Thanks! I hope you can get that query for me. There is a surprising absence of examples on the web (that I can seem to find, anyways).
Steve
A: 

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);
        }
Nat
Arrggh. I've seen like 15 examples that say do it like this.I'm on the same machine as the SP site, running as the SP and machine admin, I can happily call GetList, GetListCollection and GetWeb on the SiteData web service, but every time I call GetListItems I get a "Value cannot be null.\nParameter name: g" error.
Roger Willcocks
check your site out with SharePoint manager and make sure the default view has the fields you are trying to bring back. Check out each parameter and "proove" it has the value you need. I have had a similar issue, and I vaguely remember it was some small annoying detail the fussy parameters decided not to like.
Nat
Feel free to add a question on this error and see if anyone can get to the root of your specific issue?
Nat
A: 

Hi, 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.

Good luck !

Joe