【问题标题】:How to deal with "Operation aborted" error when sending list update via Sharepoint REST?通过 Sharepoint REST 发送列表更新时如何处理“操作中止”错误?
【发布时间】: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


【解决方案1】:

api 调用需要身份验证。我设法使用 WinHTTP 根据当前用户对请求进行身份验证,我假设他们在下面具有访问权限。我收到 204 响应,并且我的列表项更新正确。 (迭代是因为我在测试性能,可以去掉)。

工具>参考>Microsoft WinHttp 服务版本 5.1

Private Sub UpdateItem2(ID, strFormDigest As String, iteration)
    Dim sUrl As String
    sUrl ="https://123.Sharepoint.net/sites/123/_api/web/lists/getbytitle('MyDemoList')/items(" & ID & ")"
    Dim oRequest As WinHttp.WinHttpRequest
    Dim sResult As String
    sEnv = "{ '__metadata': { 'type': 'SP.Data.MyDemoListListItem' }, 'Title': 'TEST" & iteration & "' }"
    Set oRequest = New WinHttp.WinHttpRequest
    With oRequest
        .Open "POST", sUrl, True
        .setRequestHeader "IF-MATCH", "*"
        .setRequestHeader "X-HTTP-Method", "MERGE"
        .setRequestHeader "accept", "application/json;odata=verbose"
        .setRequestHeader "X-RequestDigest", strFormDigest
        .setRequestHeader "content-type", "application/json;odata=verbose"
        .SetAutoLogonPolicy AutoLogonPolicy_Always
        .send sEnv
        .waitForResponse
 End With
 End Sub

【讨论】:

    猜你喜欢
    • 2014-10-26
    • 1970-01-01
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    • 2010-09-13
    • 1970-01-01
    相关资源
    最近更新 更多