我正在尝试使用VBA获取Sharepoint列表的元素,以便在Excel工作表中显示和使用它们。
MSXML2.ServerXMLHTTP60
Dim xmlObj as Object
Dim sUrl as String
sUrl = "http://<SarepointUrl>/_api/web/lists/GetByTitle('<listTitle>')/items"
Set xmlObj = NEw MSXML2.ServerXMLHTTP60
xmlObj.Open "GET", sUrl, False
xmlObj.setRequestHeader "Content-Type", "text/xml; charset=UTF-8"
xmlObj.send
Debug.Print xmlObj.responseText
以异常作出回应:
System.UnauthorizedAccessException拒绝访问。您无权执行此操作。
我尝试使用InternetExplorer对象访问xml,导航到请求的网址:
.../getbytitle('listTitle')/items
但是Internet Explorer上的此页面被视为html页面。我可以使用InternetExplorer.Document.body.outerText
获取其内容,并且我将拥有一个包含所有xml的字符串,但我无法在实际的xml中解析它以提取数据。
Dim argUrl As String
Dim getUrl As String
Dim oHTMLDoc As HTMLDocument
Dim oXml As MSXML2.DOMDocument60
Dim objIE As New InternetExplorerMedium
argUrl = "https://<SharepointUrl>"
objIE.Visible = False
objIE.Silent = False
objIE.Navigate (argUrl)
objIE.Visible = False
Application.StatusBar = argUrl & " is loading. Please wait..."
Do While objIE.ReadyState = 4: DoEvents: Loop
Do Until objIE.ReadyState = 4: DoEvents: Loop
getUrl = "https://<SharepointUrl>/_api/web/lists/GetByTitle('ListTitle')/items"
objIE.Navigate (getUrl)
Application.StatusBar = getUrl & " is loading. Please wait..."
Do While objIE.ReadyState = 4: DoEvents: Loop
Do Until objIE.ReadyState = 4: DoEvents: Loop
Set oHTMLDoc = objIE.Document
Set oXml = New MSXML2.DOMDocument60
sXML = oHTMLDoc.body.outerText
Debug.Print sXML
'at this point it doesnt load the string as an XML because maybe this method (loadXML) needs an xml file instead of a string'
If oXml.LoadXML(sXML) Then
Debug.Print "IF"
Set nodeXML = xmlDoc
End If
任何人都能解决所有这些问题,以便在vba的sharepoint列表上发出get请求吗?
答案 0 :(得分:0)
你可以试试这个:
Sub GetItems()
Const sUrl As String = "http://server/_api/Web/Lists/getByTitle('Parent')/items"
Dim oRequest As WinHttp.WinHttpRequest
Dim sResult As String
'On Error GoTo Err_DoSomeJob
Set oRequest = New WinHttp.WinHttpRequest
With oRequest
.Open "GET", sUrl, True
.setRequestHeader "Content-Type", "application/json"
'.setRequestHeader "Accept", "application/json"
.SetCredentials "domain\user", "pw", HTTPREQUEST_SETCREDENTIALS_FOR_SERVER
.send
.waitForResponse
sResult = .responseText
Debug.Print sResult
sResult = oRequest.Status
Debug.Print sResult
End With
End Sub