【发布时间】:2017-09-15 20:56:46
【问题描述】:
我正在尝试构建一个 VBA 函数来更新 Sharepoint 列表中的值:
Sub testUpdate()
Dim XmlHttp As MSXML2.XMLHTTP60
Dim result As String
Dim url As String
Dim body As String
Dim RequestDigest As String
Set XmlHttp = New MSXML2.XMLHTTP60
url = "https://sps.utility.xyz.com/sites/xyz/_api/web/lists/GetByTitle('REST Test List')/items(1)"
RequestDigest = GetDigest("https://sps.utility.xyz.com/sites/xyz")
body = "{ '__metadata': { 'type': 'SP.Data.REST_x0020_Test_x0020_ListListItem' }, 'Title': 'updating item with new title'}"
XmlHttp.Open "POST", url, False
XmlHttp.setRequestHeader "IF-MATCH", "*"
XmlHttp.setRequestHeader "accept", "application/json;odata=verbose"
XmlHttp.setRequestHeader "content-type", "application/json;odata=verbose"
XmlHttp.setRequestHeader "X-Http-Method", "MERGE"
XmlHttp.setRequestHeader "X-RequestDigest", RequestDigest
XmlHttp.setRequestHeader "Content-Length", Len(body)
XmlHttp.Send body
result = XmlHttp.responseText
End Sub
Function GetDigest(url As String)
Dim oHttp As New MSXML2.XMLHTTP60
Dim s As String
Dim l1 As Long
Dim l2 As Long
With oHttp
.Open "POST", url + "/_api/contextinfo", False
.setRequestHeader "content-type", "application/json;odata=verbose"
.Send ""
End With
s = oHttp.responseText
l1 = InStr(1, s, "FormDigestValue")
If l1 > 10 Then
l1 = l1 + 16
l2 = InStr(l1, s, "</d:FormDigestValue")
End If
If l2 > 10 Then GetDigest = Mid$(s, l1, l2 - l1)
Set oHttp = Nothing
End Function
但是当testUpdate 排队时:
XmlHttp.Send body
它会抛出这个错误:
Run-time error '-2147467260 (80004004)':
Operation aborted
尽管出现错误,但更新成功——列表项的 Title 值发生变化。
简单地处理这个异常并绕过错误对我来说是安全的,还是表明我需要解决一个真正的问题?
【问题讨论】:
-
我刚读了这篇文章....stackoverflow.com/questions/46247627/… ......也许可以尝试使用
MSXML2.ServerXMLHTTP60 -
@jsotola,我尝试改用
ServerXMLHTTP60,但收到401 Unauthorized错误并且更新没有通过。 -
看起来服务器可能需要登录凭据
标签: vba rest sharepoint http-post