Sharepoint/WSS – Getting a list of files in a folder
Recently I had a request from a client to upload a random set of files on a user’s desktop folder into a SharePoint folder. This part of it was not tricky since you can use the System.Net.WebClient.UploadFile() method to accomplish the task. The problem I encountered was that the UploadFile() method does not check for an existing file with the same name on the specified SharePoint folder.
For this reason, I created a custom function that returns to me the list of files in a SharePoint Folder. I could then use this to warn the user if a file with the same name already exists on the SharePoint folder and have him/her take a manual action. The function uses the SharePoint List Web Service GetListItems() method and parses the XML in order to get the files in the folder queried.
Enjoy!
Private Function GetListOfFilesInSPFolder(ByVal listsWS As WebSvcLists.Lists, _ ByVal folderPath As String) As List(Of String) Dim _files As List(Of String) = New List(Of String) ' set up xml doc for getting list of files under a folder Dim doc As XmlDocument = New XmlDocument() Dim queryOptions As XmlElement = doc.CreateElement("QueryOptions") queryOptions.InnerXml = "<Folder>" & folderPath & "</Folder>" ' get the list of files Dim listItemsNode As XmlNode = listsWS.GetListItems( _ WssDocumentLibraryRootFolder, Nothing, _ Nothing, Nothing, Nothing, queryOptions, Nothing) Dim xmlResultsDoc As XmlDocument = New XmlDocument() xmlResultsDoc.LoadXml(listItemsNode.OuterXml) Dim ns As XmlNamespaceManager = New XmlNamespaceManager(xmlResultsDoc.NameTable) ns.AddNamespace("z", "#RowsetSchema") For Each row As XmlNode In xmlResultsDoc.SelectNodes("//z:row", ns) _files.Add(row.Attributes("ows_LinkFilename").Value) Next Return _files End Function
Leave a Reply