tags:

views:

217

answers:

2

Using SharePoint 2007 webservices or even Webdav, how can I check if a folder exists in a list (not document library) in SharePoint.

I would also like to check for subfolders...

Anyone have any idea on how this is done? I've asked Microsoft, and their official stance is that Microsoft provide no documentation on this. so any help will be most welcome...

Thanks in advance...

I have this code that creates a folder, but not sure how to modify it to check if the folder exists, also not even sure if this will work with sub folders...

private void CreateFolderUsingWebService(string listName, string folderName)
        {

            //Check Databox Folder Exists
            //string folderAddress = siteAddress + @"/lists/" + listAddress + @"/" + folderName;
            //wsDws.CreateFolder(folderAddress); 
            var doc = new XmlDocument();
            XmlElement batch = doc.CreateElement("Batch");
            string item = "<Method ID=\"1\" Cmd=\"New\">" +
                "<Field Name=\"ID\">New</Field>" +
                "<Field Name=\"FSObjType\">1</Field>" +
                "<Field Name=\"BaseName\">" + folderName + "</Field></Method>";

            batch.SetAttribute("ListVersion", "1");
            //batch.SetAttribute("ViewName", "{GUID of View, including braces}");
            batch.InnerXml = item;

            wsLists.UpdateListItems(listName, batch);
        }
+1  A: 

See my answer on this post Sharepoint 2007, how to check if a folder exists in a document library, this is quite similar.

naivists
+1 Yes that was an excellent answer, and it helped me a lot with document libraries, but unfortunately it doesn't work with lists because the response seems to redirect to the base list, and doesn't produce a 404 like with a document library. Huge pity it doesn't work with lists...
JL
Oh, sorry, I didn't notice it was your question again ;-)
naivists
A: 

Ok - this info might help the next SharePoint developer:

The function above works, and will even create a directory structure. BUT you need to pass the list name, not the list URL, this means if you localize your code, you need to pass the localized list name to the function.

I didn't bother adding a check for ifExists, because it seems to NOT create duplicates or fail if the directory already exists. I know this isn't a great solution, but I just don't have 2-3 weeks to research how to do it properly, so if you have any suggestions, comments are welcome.

Lastly any Microsoft representation reading this - might want to consider why there isn't any really good documentation on this with how to's from MS? Mmmmm

I went as far as downloading the MOSS Web Services SDK, and it contains 1 very vague example of how to use 1 function in the Lists web service, this simply is not enough information for those of us trying to put together robust solutions in MOSS. We need way more documentation please...

JL