Tuesday, May 28, 2013

Sharepoint webservice - create and get list of subfolders

To create folder
            Dim webUrl as string = "http://localhost/sites/test1/"
            Dim listWebService As New ListService.Lists()
            listWebService.Url = webUrl + "/_vti_bin/lists.asmx"
            listWebService.Credentials = System.Net.CredentialCache.DefaultCredentials 'New System.Net.NetworkCredential("user", "pwd")
            Dim doc As New XmlDocument
            Dim xmlCommand As String
            Dim node1 As XmlNode
            xmlCommand = "<Batch OnError='Continue' RootFolder='" & webUrl & "/Main'><Method ID='1' Cmd='New'><Field Name='ID'>New</Field><Field Name='FSObjType'>1</Field><Field Name='BaseName'>" & foldername & "</Field></Method></Batch>"
            doc.LoadXml(xmlCommand)
            Dim batchNode As XmlNode = doc.SelectSingleNode("//Batch")
            node1 = listWebService.UpdateListItems("Main", batchNode)
ListService is the web service http://localhost/sites/test1/_vti_bin/lists.asmx added as reference.

If you want to add subfolder, just need to change the rootfolder on the xmlCommand.

To get the list of subfolders (this can be used to check if subfolder exists)
   Sub getSubFolders(parent As String, ByRef retTable As DataTable)
        Dim query As String = "<mylistitemrequest><Query><Where><Eq><FieldRef Name=""FSObjType"" /><Value Type=""Lookup"">1</Value></Eq></Where></Query><ViewFields><FieldRef Name=""EncodedAbsUrl""/><FieldRef Name=""ID"" /><FieldRef Name=""Title"" /></ViewFields><QueryOptions><Folder>" & parent & "</Folder></QueryOptions></mylistitemrequest>"
        Dim dt As DataTable = Nothing
        Console.WriteLine("Parent is " & parent)
        Using listProxy As ListService.Lists = New ListService.Lists()
            listProxy.Url = webUrl + "/_vti_bin/lists.asmx"
            listProxy.UseDefaultCredentials = True

            Dim doc As XmlDocument = New XmlDocument()
            doc.LoadXml(query)

            Dim queryNode As XmlNode = doc.SelectSingleNode("//Query")
            Dim viewNode As XmlNode = doc.SelectSingleNode("//ViewFields")
            Dim optionNode As XmlNode = doc.SelectSingleNode("//QueryOptions")

            Dim retNode As XmlNode = listProxy.GetListItems("Main", String.Empty, queryNode, viewNode, String.Empty, optionNode, Nothing)

            Dim ds As DataSet = New DataSet()
            Using sr As StringReader = New StringReader(retNode.OuterXml)
                ds.ReadXml(sr)
            End Using



            If Not IsNothing(ds.Tables("Row")) Then
                If ds.Tables("Row").Rows.Count > 0 Then
                    Dim folderUrls = From f In ds.Tables("Row").AsEnumerable() Select f("ows_EncodedAbsUrl")
                    dt = ds.Tables("Row").Copy()

                    For Each folderUrl As String In folderUrls
                        getSubFolders(folderUrl, dt)
                    Next

                    retTable.Merge(dt)
                End If
            End If

        End Using

    End Sub

Comment the getSubFolders(folderUrl, dt) if you do not want to drill down the subfolders.

Subfolders references: here and here

No comments: