Reputation: 11
I'm looking for a way of checking if a file exists on SharePoint Online.
Our Sharepoint on premise was moved to Sharepoint Online, since that my code doesn't work.
Existing recommendations don't work. I've been looking for a solution (google) for couple of weeks with no success.
HTTPRequest always returns .Status="OK"
and .StatusText="200"
even for a file that does not exist in SharePoint folder.
The code below shows what I have tried so far (in many combinations, but result is always the same)
Sub Test_WinHTTPRequest()
Set HttpRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
With HttpRequest
.Open "GET", "https://company.sharepoint.com/sites/wt-teams-CServices/WAN%20Fin/FY19-20/test_file.xlsx"
' .Open "GET", "http://company.sharepoint.com/sites/wt-teams-CServices/WAN%20Fin/FY19-20/test_file123.xlsx"
' .Open "GET", "http://company.sharepoint.com/sites/wt-teams-CServices/WAN Fin/FY19-20/test_file123.xlsx"
.Send
st = .Status
stt = .StatusText
rt = .ResponseText
End With
Debug.Print rt, st, stt
End Sub
The same situation with MSXML2.XMLHTTP60 object
Whatever file name I use in URL, HTTPRequest always returns "URL exist". .ResponceText returns huge output with some HTML code which is exactly the same for any filename I used in URL.
In Internet Explorer or Windows explorer, if I paste the URL with non-existing filename, returns an error as I expect.
Upvotes: 1
Views: 5654
Reputation: 3634
Here is a function for checking whether the URL exists or not...
Private Function urlExists(url As String) As Boolean ' Reference to Microsoft XML V3 required
Dim Request As New MSXML2.XMLHTTP30
On Error Resume Next
With Request
.Open "GET", url, False
.Send
urlExists = IIf(.Status = 200, True, False)
End With
End Function
Upvotes: 0
Reputation: 840
did You consider using SharePoint REST Api methods to get the file from folder by server relative url? something like:
https://{site_url}/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/Files('{file_name}')/$value
for more request calls that could meet Your needs please check -> link
I hope this will put You on the right track :)
Upvotes: 1