5

का उपयोग करके संलग्न फ़ाइल को प्रतिस्थापित करना Upload file to SharePoint 2010 using PowerShell and the OData API और SharePoint 2010 REST API JQUery Insert, Update, Delete पर वादिम के उत्तरों का एक अनुकूलन।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 {} 

} 

आदेश का उपयोग करते हुए थ्रो (405) पद्धति की अनुमति नहीं:

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

और PUT और MERGE के बीच स्विचिंग।

मुझे क्या याद आ रही है?

+0

क्या जब आप "मैन्युअल" आह्वान-webrequest या आह्वान-restmethod (ताकि एक स्क्रिप्ट का उपयोग किए बिना) का उपयोग कर एपीआई का उपयोग करने की कोशिश क्या होता है? क्या यह भी एक त्रुटि फेंक देता है? और क्या एपीआई HTTPpost, put, delete और अन्य तरीकों को स्वीकार करने के लिए कॉन्फ़िगर किया गया है? – bluuf

उत्तर

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