I need to loop through SharePoint subfolders, programmatically from Excel VBA, to get files properties, move files to archive subfolders and replace archived files with new ones. The folder structure is:
MyLibrary/Shared Documents/MainFolder/Subfolders/Archive. First I move files from Subfolders to their respective Archive subfolders, and then I save a new version of the files to each Subfolder, replacing the archived ones. I found methods to move and create files using WebDAV but I need a method to loop through subfolders. Do you have nay solutions to share? Thanks a lot.
This is what I tried, which gives error 200:
Dim xmlhttp As Object Dim folderPath As String Dim sharepointURL As String Dim xmlDoc As Object Dim node As Object Dim nodes As Object Dim subfolder As String
' SharePoint URL and library path
sharepointURL = "https://mycompany.sharepoint.com/sites/mysite/Shared%20Documents"
' Create an XMLHTTP object for WebDAV request
Set xmlhttp = CreateObject("MSXML2.XMLHTTP.6.0")
' Send WebDAV request to get the folder structure
folderPath = sharepointURL & "/_vti_bin/_vti_aut/author.dll?method=PROPFIND&Depth=1"
' Set up the WebDAV request (PROPFIND method)
xmlhttp.Open "PROPFIND", folderPath, False
xmlhttp.setRequestHeader "Content-Type", "text/xml"
xmlhttp.setRequestHeader "Depth", "1"
xmlhttp.setRequestHeader "Authorization", "Bearer " & GetAccessToken()
' Send the request
xmlhttp.send
' Check for successful response
If xmlhttp.Status = 207 Then
' Load the XML response
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.LoadXML xmlhttp.responseText
' Get all nodes representing subfolders
Set nodes = xmlDoc.getElementsByTagName("D:href")
' Loop through each node to process subfolder names
For Each node In nodes
subfolder = node.text
Debug.Print "Subfolder URL: " & subfolder
' You can perform any operations here with the subfolder URLs
Next node
Else
MsgBox "Error: " & xmlhttp.Status & " - " & xmlhttp.statusText
End If
Function GetAccessToken() As String GetAccessToken = "YOUR_ACCESS_TOKEN" End Function
Ago is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1