Un adattamento delle risposte di Vadim a Upload file to SharePoint 2010 using PowerShell and the OData API e SharePoint 2010 REST API JQUery Insert, Update, Delete.Sostituzione di un file allegato utilizzando l'API OData di SharePoint 2010
Il tentativo di caricare una nuova versione di un allegato:
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 {}
}
Utilizzando il comando tiri (405) Metodo non ammessi:
Update-Attachments -WebUrl "http://contoso.intranet.com/" -ListName "Tasks" -ItemId 1 -Paths "C:\Users\user\Documents\SharePointUserGuide.docx"
Ho provato variazioni sull'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')")
e commutazione tra PUT
e MERGE
.
Cosa mi manca?
Cosa succede quando si "manualmente" tenta di utilizzare l'API utilizzando invoke-WebRequest o invocare-restmethod (quindi senza l'utilizzo di uno script)? Invia anche un errore? E l'API è configurata per accettare HTTPpost, put, delete e altri metodi? – bluuf