How do you rename an attachment using workflow designer GetFileBvServerRelativeURL MoveTo

UnderwoodMarlene-1858 75 Reputation points
2024-08-13T22:05:41.4533333+00:00

I am having trouble with the POST command. I get 'BADREQUEST', and of course the attachment is not renamed.

Goal is to rename the files attached to a List if they are not prefixed with a standard naming convention (i.e., Year+Building+Issue No+).

  1. Build ... Dictionary (Output to variable:strHTTPServiceWith)

NAME Value

  • accept = application/json;odata=verbose
  • content-type = application/json;odata=verbose
  • X-HTTP-METHOD = PATCH ... I also tried PUT & POST
  • IF-MATCH = * ... I tried removing this
  1. Call

[%Workflow Context:Current Site URL%]_api/web/GetFileBvServerRelativeURL(' "/Lists/[% Workflow Context:List Name%]/Attachments/[%Current Item:ID%]/[%Variable: strCurrentAttachment%]" ')/moveto(newurl=' "/Lists/[%Workflow Context:List Name%] /Attachments/[%Current Item:ID%]/[%Variable: strNewAttachmentPrefix1%] [%Variable:strCurrentAttachments%]" ',flags=1)

HTTP web service with variable:dicRequestContent (ResponseContent to Variable:jsonResponse | strResponseHeaders | ResponseStatusCode to Variable:strResponseCode

The property of #2 has RequestHeaders = strHTTPServiceWith

Request Type is HTTP POST

Platform: SharePoint 2016 on-premise

SharePoint Designer 2013

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,818 questions
SharePoint Workflow
SharePoint Workflow
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Workflow: An orchestrated and repeatable pattern of business activity, enabling data transformation, service provision, and information retrieval.
601 questions
{count} votes

1 answer

Sort by: Most helpful
  1. UnderwoodMarlene-1858 75 Reputation points
    2024-09-17T21:34:44.6533333+00:00

    Opened a case with Microsoft TrackingID#2408270040008802.

    Resolution:

    ·   There is not a simple way using the built-in cmdlets to rename an existing file attachment

    ·  I suggested you would need to download the bytes for the attachment, delete the original attachment, then add a new one with the new name.

    You were able to get it to work using the SpListItem.Attachments.Add and SpListItem.Attachments.Delete methods in SharePoint Management Shell by writing a PowerShell script.

    Here are a few snippets of the code:

    $webURL = "
    $listName = "{ListName}" 
    #Get the SPWeb object and save to variable
    $listWeb = Get-SPWeb $webURL
    #Get the SPList object to retrieve the list
    $list = $listWeb.Lists[$listName]
    $listItems = $list.Items
    $intRenamedCounter = 0
    $intRenamedErrorCounter = 0
    
    foreach ($listItem in $listItems)
    {
      [string]$theAttachmentFlag = $listItem["Attachments"]
      if ($theAttachmentFlag.Equals("True"))
      {
         $intRenamedForThisItem = 0
     
        $AttachmentsColl = @($listItem.Attachments)  #This was needed to prevent an error on the foreach attachment loop because the attachments were being changing by this script.
         Foreach ($attachment in $AttachmentsColl)   
         {
              Try
              {  
    			$file = $listWeb.getfile($listitem.attachments.UrlPrefix + $attachment)  
    			$newfile = $listWeb.GetFile($listItem.Attachments.UrlPrefix + $strNewAttachmentName)
    			$bytes = $file.OpenBinary() 
    			$listItem.Attachments.Add($strNewAttachmentName,$bytes) 
    			$listItem.Attachments.Delete($attachment) 
                $intRenamedCounter = $intRenamedCounter + 1
    			$intRenamedForThisItem = $intRenamedForThisItem + 1
     
        } #End of Try
               Catch
               {
                  Write-All "   ERROR. A problem occurred renaming the attachment above. Check the hazard attachments for duplicate names. Contact IT for assistance, if needed."
                  Write-Warning "Caught an exception:"
                  Write-Warning "Exception Message: $($_.Exception.Message) - Attachment:$($attachment) NewName: $($strNewAttachmentName) "
                  $intRenamedErrorCounter = $intRenamedErrorCounter + 1
               }
     
         }# End of ForEach Attachment
         
         if ($intRenamedForThisItem -gt 0)
            {
            Retry{
                $listItem.Update()  #Attachments aren't changed without this 
                 }
    			Catch
    			 {
                   	Write-All "   ERROR. A problem occurred updating the list item above. Contact IT for assistance, if needed."              
    				Write-Warning "Caught an exception:"              Write-Warning "Exception Message: $($_.Exception.Message) - List Item:$($strIssueNo) "
                  $intRenamedErrorCounter = $intRenamedErrorCounter + $intRenamedForThisItem
    			  $intRenamedCount = $intRenamedCounter - $intRenamedForThisItem
    	        } 
    
            }
     
     } # End of Attachment = True (Item has an attachment)
     
    } #end of Item Loop
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.