Guilherme Batista
Guilherme Batista

Reputation: 195

Get all folders and sub folders in SharePoint using Web Service

I am developing a web application who needs to get the folders and sub folders who is on SharePoint and put on a TreeView representing the hierarchy. My application is not running in the same server that SharePoint, so I think the best way to do this is through Web Services.

So I added a Web Reference to SiteData.asmx to my project and found the following code:

 Private Sub GetSiteData()
    Dim RootFolder As String = "http://mySharepointServer/site/doc_site"
    Dim DirWSSP As String = "http://mySharePointServer/_vti_bin/SiteData.asmx"


    'Definitions of TreeView
    Dim tree As TreeView
    Dim raiz As TreeNode
    Dim no As TreeNode

    tree = Page.FindControl("trvFolder")
    raiz = New TreeNode(RootFolder)
    tree.Nodes.Clear()
    tree.Nodes.Add(raiz)

    ' Definitions of web service
    Dim service As New SP_SiteData.SiteData

    service.Credentials = New System.Net.NetworkCredential("userID", "password", "domain")


    Dim enArray() As SP_SiteData._sFPUrl

    service.EnumerateFolder(RootFolder, enArray)

    Dim en As SP_SiteData._sFPUrl
    For Each en In enArray
        If en.IsFolder Then
            no = New TreeNode(en.Url)
            raiz.ChildNodes.Add(no)
        End If
    Next

End Sub

I copied this code from a forum on msdn but is not working, the service.EnumerateFolder always return an empty array, that is , enArray always comes Nothing and I get an error : Object Reference not set to an instance of an object.

This code works? There is another way to do this? I am very novice with web services and web applications. OBS: I'm using Visual Studio 2010 and SharePoint 2010

Upvotes: 3

Views: 5521

Answers (2)

Guilherme Batista
Guilherme Batista

Reputation: 195

I found the solution using the web service Lists.asmx.

The problem is I am very novice in web develpment and I don't know nothing about Sharepoint, so i don't knew how to use the web services.

The problem was I was providing the wrong url. The url must be like:

http://mysharepointsite/site/subsite_or_list/_vti_bin/Lists.asmx

and I was using

http://mysharepointsite/_vti_bin/Lists.asmx

The difference is I call the web Service lists.asmx on subsite.

Another thing I didn't know, is that what I was calling the folders are actually lists in sharepoint, so im method getlistitems(), must put the list Name as parameter.

Anyway for futher help, if anybody have the same problem as i had follow this link :

http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/99f3e9d0-6ecf-4b1d-8b68-d108f36aaacc

And the code are that here from msd: http://msdn.microsoft.com/en-us/library/lists.lists.getlistitems(v=office.12)

Sorry for the bad english....

Thanks everybody

Upvotes: 1

Amicable
Amicable

Reputation: 3101

ListItems both Folders and Documents, from the webservice you can tell the different from the "ows_ContentType" attribute. In the object model they have an IsFolder property.

IEnumerable<XElement> result = from child in root.Descendants(xns + "row")
    where child.Attribute("ows_ContentType").Value == "Folder"
    select child;

This LINQ query can be used on the on the web methods result to return folder types only. Pretty easy to see how to change this to work for "Document" even if you don't understand LINQ calls.

*Related note the web service for "GetFolderCollection" is talking about Sharepoint folders, it means web site directory folders.

Upvotes: 1

Related Questions