To create an attachment resource, you must specify the following properties:
Endpoint Uri: http://server/site/_vti_bin/listdata.svc/entityset(itemid)/Attachments Method: POST Headers: Slug: "entityset|itemid|name" ContentType: */* Body: content
Having said that, my expression is that the specified parameter body ( $payload ) is not valid in the above example.
The following example shows how to upload an attachment file through the SharePoint 2010 REST interface:
Function Add-Attachments() { [CmdletBinding()] Param( [Parameter(Mandatory=$True)] [string]$WebUrl, [Parameter(Mandatory=$True)] [string]$ListName, [Parameter(Mandatory=$True)] [int]$ItemId, [Parameter(Mandatory=$True)] [string]$SourcePath ) BEGIN {} PROCESS { $endpointUri = New-Object System.Uri("$WebUrl/_vti_bin/listdata.svc/$ListName($ItemId)/Attachments") $fileName = (Split-Path $SourcePath -Leaf) $fileContent = ([IO.File]::ReadAllBytes($SourcePath)) $headers = @{ 'Slug' = "$ListName|$ItemId|$fileName"; } Invoke-WebRequest -Uri $endpointUri -Method Post -UseDefaultCredentials -Body $fileContent -Headers $headers -ContentType "*/*" }
Using:
Add-Attachments -WebUrl "http://contoso.intranet.com/" -ListName "Tasks" -ItemId 1 -SourcePath "C:\Users\user\Documents\SharePointUserGuide.docx" -verbose
Update
After doing some analysis through Fiddler, it was determined that the correct endpoint URL should be:
/_vti_bin/listdata.svc/Attachments HTTP/1.1
instead:
/_vti_bin/listdata.svc/Tasks(<id>)/Attachments HTTP/1.1
Modified Example
Function Add-Attachments() { [CmdletBinding()] Param( [Parameter(Mandatory=$True)] [string]$WebUrl, [Parameter(Mandatory=$True)] [string]$ListName, [Parameter(Mandatory=$True)] [int]$ItemId, [Parameter(Mandatory=$True)] [string]$SourcePath ) BEGIN {} PROCESS { $endpointUri = New-Object System.Uri("$WebUrl/_vti_bin/listdata.svc/Attachments") $fileName = (Split-Path $SourcePath -Leaf) $fileContent = ([IO.File]::ReadAllBytes($SourcePath)) $headers = @{ 'Slug' = "$ListName|$ItemId|$fileName"; } Invoke-WebRequest -Uri $endpointUri -Method Post -UseDefaultCredentials -Body $fileContent -Headers $headers -ContentType "*/*" }
source share