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

Tuesday, May 7, 2013

ODP.NET oracleexception with no error message :S

My oracle version is 11.2.0.2.

I tried to use ODAC 11.2.0.3 to connect to the database from my .net program.
And it threw this error ORA-1017: invalid username/password; logon denied when my password expires and I tried to change my password.

So I installed ODAC 11.2.0.2 and try to connect to the database in VB.Net however I got error OracleException with no error message -> Oracle.DataAccess.Client.OracleException: {""}

But when I code my program in C# it gave the error OracleException {"ORA-28001: the password has expired"}

Solution to this is to uninstall all the ODAC versions in my PC.
Then install ODAC 11.2.0.2 and now it should work fine.

Btw in order to change password when the password expired, u can use this method OpenWithNewPassword.

See my thread at Oracle forum here.