How to rename an attachment before uploading, and when deleting an attachment from a form, delete it from the document library as well.

  • 8 March 2016
  • 3 replies
  • 198 views

Userlevel 3
Badge +10


 

Symptoms


How to rename an attachment before uploading, and when deleting an attachment from a form, delete it from the document library as well.
 

Diagnoses


SharePoint Document Libraries force uniqueness for the 'Name' column. Filename is grayed out in the 'Set a Control's Properties' rule.
 

Resolution

Documents will need to be uploaded then renamed. This ensures that future uploads are unique, though it will still throw an error if a duplicate document name is submitted, so you may need to clear any existing documents that could conflict.

- Pass back the 'Name' field from the Output Mappings of the Upload Document method to a Parameter
- After the Upload Document method, execute the Document Library SmartObject's 'Rename' method, passing in the name in that parameter and your new unique name (could be something like [Parameter][ID] to make it unique)

Regarding deletion:
- On the Edit/Display forms' Get Documents calls that pull in the form's associated attachments, return the Output Mappings 'Name' value to a parameter associated with each attachment
- On each attachment's delete button, use the associated filename parameter (the Delete Document method requires the file extension along with the file's name)




 

3 replies

Badge +7

I am using the above methods to rename the files, however through an expression I would like to add the name of the file with the ID of the file, but I am not able to retrieve the filename.

Userlevel 5
Badge +18

The following is another possible way to rename the file before uploading to the backend (Sharepoint or SQL) as of 4.7:


 


* The File Attachment Control holds the following XML information:


 


<collection><object><fields><field name='FileName'><value>Leave Policy.docx</value></field><field name='FilePath'><value>C70B5D47E514A2F3146ACED9057F5EF5Leave Policy.docx</value></field><field name='FileRequestData'><value></value></field></fields></object></collection>


 


** The File is stored in Isolated Storage (C:ProgramDataIsolatedStorage) before it is uploaded to Sharepoint or SQL


 


*** Essentially, the '<value>Leave Policy.docx</value>' XML will need to be renamed before uploading to affect the file name change


 



 


1.  A View with some hidden Data Label to store and Manipulate this XML


 


 



2.  Rules bound to the Upload button and performs the following.


 


 



a.  Transfer the Attachment Control XML and the Attachment Control File Name to hidden Data Label


 



** At this step, an expression is executed another Data Label control to target the '<value>Leave Policy.docx</value>' portion of the XML and apply the name change (i.e. '<value>HR_Leave Policy.docx</value>')


 



b. Transfer data rule to clear the Attachment Control of the original attachment


 



c.  Transfer Data rule to pass the modified XML with renamed file, into the File Attachment Control


 



d.  Then Sharepoint Library or SQL SmartObject can be called to upload this renamed file to the backend


 



Renamed File is uploaded.


 

Badge +5
Thanks tin, that solution is perfect.

Reply