Move/copy a folder with its children to Livelink

2019-08-30 09:41发布

问题:

I'm already using the following code to copy a file to Livelink:

Public Function saveFileLL(target As Long, pathSource As String, fileName As String) As Boolean
    Dim dav As New ADODB.Record
    Dim files As New ADODB.Recordset
    Dim objStream As New ADODB.Stream
    Dim url As String

    If Not Val(Nz(target, 0)) > 0 Or Not pathSource Like "*.*" Or Not fileName Like "*.*" Then
        saveFileLL = False
        Exit Function
    End If

    url = URL_LIVELINK_DAV & target

    dav.Open url, , adModeReadWrite
    Set files = dav.GetChildren

    If Not (files.BOF And files.EOF) Then files.MoveFirst

    Do Until files.EOF
        If fileName Like Replace(files("RESOURCE_DISPLAYNAME"), "_", "?") Then Exit Do
        files.MoveNext
    Loop

    If files.EOF Then
        files.addnew "RESOURCE_PARSENAME", fileName
        files.Update
    End If

    files.Close
    dav.Close

    objStream.Open "URL=" & url & "/" & fileName, adModeWrite
    objStream.Type = adTypeBinary
    objStream.LoadFromFile pathSource
    objStream.Flush
    objStream.Close

    Set dav = Nothing
    Set files = Nothing
    Set objStream = Nothing

    saveFileLL = True
End Function

Now, as the title says, I would like to do the same but with a folder. I guess my question isn't really related to Livelink but more to the way to handle folders in general. Is it possible to move a folder with all his children without looping through all the subfolders/files? How could I adapt my saveFileLL() function to do so?

EDIT:

Here is another portion of code that allows me to directly create one folder into the Livelink folder designed by objId.

Public Function CreateFolderToLLFolder(ObjId As String, folderName As String, Optional getId As Boolean = False) As String
    Dim davfile As New ADODB.Record
    Dim davFiles As New ADODB.Recordset
    Dim davDir As New ADODB.Record

    Dim newDirFields(1) As Variant
    Dim newDirValues(1) As Variant

    newDirFields(0) = "RESOURCE_PARSENAME"
    newDirValues(0) = folderName
    newDirFields(1) = "RESOURCE_ISCOLLECTION"
    newDirValues(1) = True

    Set davDir = connection(ObjId, "")
    Set davFiles = davDir.GetChildren()
    If (davFiles.Supports(adAddNew)) Then
        davFiles.addnew newDirFields, newDirValues
    End If

    davfile.Open davFiles, , adModeReadWrite
    CreateFolderToLLFolder = davfile.fields("urn:x-opentext-com:ll:properties:nodeid").value
End Function

Public Function connection(ObjId As String, Optional filename As String = "") As ADODB.Record
    Dim davDir As New ADODB.Record
    davDir.Open filename, "URL=http://livelink-server/livelinkdav/nodes/" & ObjId & "/", adModeReadWrite, adFailIfNotExists, DelayFetchStream, "", ""
    Set connection = davDir
End Function

Don't ask me why this works, I found this and it does work. objId for those wondering is the unique ID that Livelink gives to all his files/folders.

Thank you.

回答1:

You could save a whole set of folder and it's constituent files/subfolders as one block by zipping them and then upload the zip file. Like this http://www.rondebruin.nl/windowsxpzip.htm

If you want to reflect the structure in LiveLink then you'd have to explore the folder tree to replicate content - but there's plenty of vba code around to loop through files & folder trees and it can all call into your saveFileLL function if you have to go that route.



回答2:

The easiest way to perform what I want is finally to use the web service integrated into Livelink. Because there is really not much information on the Web about the whole Livelink API (that's surprising considering all we can find on Google these days), I'm willing to post here my solution. That's in fact really easy.

All I had to do is to install some add-on to Firefox (I used Fox but others would do the job too) to see the HTTP headers/packets when doing some work in Livelink. Most of the times, operations are done with the POST method. I realized with the Fox plugin that 3 POST are sent, the 2 first returned a 401 error while the 3rd returns the right 200 response and got the action done.

Then, I can deduce that Livelink is a SOAP-service based on a NTLM authentification. It looks like it has been developped in ASP.net.

To use the webservice within VBA, nothing more easy. You will need the Microsoft XML v6.0 livrary for the MSXML2 object, and there you go:

Dim sMsg As String
Dim sURL As String, postData As String
Dim ObjHTTP As Object

Set ObjHTTP = New MSXML2.XMLHTTP
sURL = "http://server.com/livelink/livelink.exe"
postData = "your-post-data"

ObjHTTP.Open "Post", sURL, False
ObjHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
ObjHTTP.send (postData)

Set ObjHTTP = Nothing

As simple as that, just find the right POST data with some kind of browser's plugin and you got it. Most of the post-data isn't encoded and easy to deal with.