An adaptation of Vadim's answers to Upload file to SharePoint 2010 using PowerShell and the OData API and SharePoint 2010 REST API JQUery Insert, Update, Delete.
Attempting to upload a new version of an attachment:
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 {}
}
Using the command throws (405) Method Not Allowed:
Update-Attachments -WebUrl "http://contoso.intranet.com/" -ListName "Tasks" -ItemId 1 -Paths "C:\Users\user\Documents\SharePointUserGuide.docx"
I've tried variations on the endpoint:
$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')")
and switching between PUT
and MERGE
.
What am I missing?
Can you check whether this SharePoint 2013 reference works with the 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With