Transfer sharePoint list attachments across site and sitecollection using Jquery.

One of the major tasks I was never able to accomplish was transferring list attachments between sites or site collections in SharePoint using Jquery. It can be easily done using PowerShell or code behind solutions but you hit a roadblock when you don’t have access to those options. In this article, we will learn how can we accomplish that using Jquery, Rest and HTML.

I was able to transfer list attachments to another list or library but within the same site. When i tried the same thing for between sites and to next level between site collection i was running out of ideas, but luckily while my research i stumbled upon this link.  I can’t thank enough to the author of that article who deserved a lot credit for the work.

Few things to note before we go to my code file.

Overwrite default javascript function of SharePoint for the successful transfer of binary data between cross-sites. I urge you to read the link more carefully which explains more detail what it is.

I have problems while moving files to new list items so I end up creating a fake attachment for the destination list items and deleted later. I have attached the code in the HTML file. Use it as you need. Looks like SharePoint doesn’t create attachment folder for new list items unless there is an attachment added. In our code, we use
GetFolderByServerRelativeUrl to find the attachment folder and use
Files/Add method to add an attachment. Looks like a SharePoint new list item will not have that folder until an attachment is added manually or programmatically. Remember in our code we are copying an attachment as we copy files from one folder to another folder. So i ended up creating a fake attachment for all the list items and then delete it later.

This html file can be used as an utility applcation, it has following option for the file transfer.

This application works following ways

  • List Attachments to List Attachments
    • Individual- which means you can pick the source item and destination item to transfer files
    • All- Which lets you chose the source list and destination list. But you have to pick a column name whose value will equal with the column name in the destination list. So if a list item has the same value for title in source and destination list item then the transfer will happen within those items only.
List to List All Items
List to List Individual Items
  • List to Library
    • With this option, you will pick a source list and destination library. All the attachments from the source list will be uploaded to the destination library as picked in the application.
List to library

Below is the HTML file that has all the code and CSS I am using. You can download this file and upload it to your SharePoint document library. Use a content editor web-part to load it in a page and start using it.

I have used comments in almost every section possible. Start little by little and you should be able to transfer any amount of list attachments using Jquery. I ran in to transfer issue while working remote so i used my VPN which worked like a charm.

If you have lot of files and few of them are very very large let’s say more than 10 MB then try the individual approach or take a look at your network configuration for timeouts.

Fell free to modify the code and make it better, which will be highly appreciated.

Let me know how does it works for you

Happy Transferring 🙂

Advertisements

Leave a Reply

Your email address will not be published. Required fields are marked *