使用SharePoint 2010的OData API替换附加文件

时间:2015-06-23 18:04:53

标签: powershell sharepoint sharepoint-2010 odata powershell-v3.0

改编Vadim对Upload file to SharePoint 2010 using PowerShell and the OData APISharePoint 2010 REST API JQUery Insert, Update, Delete的回答。

尝试上传新版本的附件:

Function Update-Attachments() {

    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$True,Position=1)]
        [string]$WebUrl, 

        [Parameter(Mandatory=$True,Position=2)]
        [string]$ListName, 

        [Parameter(Mandatory=$True,Position=3)]
        [int]$ItemId,

        # pipeline support
        [Parameter(Mandatory=$True,Position=4,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        # associate FileInfo object's FullName property to be bound to parameter
        [Alias('FullName')]
        [string[]]$Paths
    )

    BEGIN {}
    PROCESS {

        # 
        $endpointUri = New-Object System.Uri("$WebUrl/_vti_bin/listdata.svc/$ListName($ItemId)/Attachments")

        Foreach ($Path In $Paths) {
            Write-Verbose "Path: $Path"

            $fileName = (Split-Path $Path -Leaf)
            $fileContent = ([IO.File]::ReadAllBytes($Path))
            $headers = @{
                "X-HTTP-Method" = "MERGE";
                "If-Match" = "*"
            }

            try {
                # reset each pass to ensure that prior response isn't reused
                $response=$null
                $response = Invoke-WebRequest -Uri $endpointUri -Method POST -UseDefaultCredentials -Body $fileContent -Headers $headers -ContentType "*/*"
            }

            # Invoke-WebRequest throws System.Net.WebException
            catch [System.Net.WebException] {
                throw $_
            }

            finally {
                # returns Microsoft.PowerShell.Commands.HtmlWebResponseObject
                $response
            }

        } # Foreach

    } # PROCESS
    END {}

}

使用命令throws(405)Method Not Allowed:

Update-Attachments -WebUrl "http://contoso.intranet.com/" -ListName "Tasks" -ItemId 1 -Paths "C:\Users\user\Documents\SharePointUserGuide.docx"

我尝试了端点上的变体:

  • $endpointUri = New-Object System.Uri("$WebUrl/_vti_bin/listdata.svc/$ListName/Attachments/$ItemId/$fileName")
  • $endpointUri = New-Object System.Uri("$WebUrl/_vti_bin/listdata.svc/Attachments(EntitySet='$ListName',ItemId=$ItemId,Name='$fileName')")

并在PUTMERGE之间切换。

我错过了什么?

1 个答案:

答案 0 :(得分:1)

您可以检查此SharePoint 2013参考是否适用于2010 API吗?

url: http://site url/_api/web/lists/getbytitle('list title')/items(item id)/AttachmentFiles('file name')/$value
method: POST
body: "Contents of file."
headers:
    Authorization: "Bearer " + accessToken
    "X-HTTP-Method":"PUT"
    X-RequestDigest: form digest value
    content-length:length of post body

https://msdn.microsoft.com/en-us/library/office/dn292553.aspx?f=255&MSPPError=-2147217396