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

cra*_*aig 5 powershell sharepoint sharepoint-2010 odata powershell-3.0

使用PowerShell和OData API以及SharePoint 2010 REST API JQUery插入,更新,删除,将Vadim对上载文件的答案改编为SharePoint 2010.

尝试上传新版本的附件:

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 {}

}
Run Code Online (Sandbox Code Playgroud)

使用命令throws(405)方法不允许:

Update-Attachments -WebUrl "http://contoso.intranet.com/" -ListName "Tasks" -ItemId 1 -Paths "C:\Users\user\Documents\SharePointUserGuide.docx"
Run Code Online (Sandbox Code Playgroud)

我尝试了端点上的变体:

  • $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')")

并在PUT和之间切换MERGE.

我错过了什么?

小智 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
Run Code Online (Sandbox Code Playgroud)

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