Adding Bulk Attachments To SharePoint List Item With PowerShell Remotely

Recently, I was working on eRoom to SharePoint 2013 migration with the help of Metalogix tool. Metalogix has eRoom console which helps you to migrate content from eRoom to SharePoint 2010 or SharePoint 2013 ( I haven't tried SharePoint 2007 version but console doesn't help you to migrate content from eRoom to SharePoint Online aka Office 365).

There is pre-defined object mapping between eRoom to SharePoint which is used when migrating content from eRoom to SharePoint, so eRoom databases are migrated as SharePoint custom lists and Database Entry is migrated as SharePoint list item. eRoom has very nasty content hierarchy where user can embed any object in any other object and create nested structure which is unlikely in SharePoint.

During migration we found that few database entries have large nested structure involving thousands of documents and Metalogix was failing to migrate those documents as attachments due to the following reasons which I found:

  • While attaching Blocked File Types in SharePoint.
  • Attaching 0 kb file size item to list Item.

In both of these above cases, Metalogix was failing to migrate remaining documents if it encounters any of the above situation. We can't delete such files from source as it would not be valid migration scenario nor we can allow blocked file types in SharePoint to make it a successful migration. It would have taken days to upload thousands of attachments manually and it would not have helped in case of recurring activity. So I decided to create PowerShell script which will add bulk attachments to particular list item.

How it works?

  1. It uses Client Side SharePoint PowerShell. That means you don't need to login to SharePoint CA box to run the PowerShell. What you need is administrative privileges on target SharePoint site.

  2. It works for SharePoint 2010, SharePoint 2013 and SharePoint Online.

  3. You can use different credentials to get SharePoint context to do bulk attachment.

  4. Reads information of files to attach from local or network drive.

  5. If file is already attached, doesn't try to attach it again.

  6. If file with the same name exists in different path in the list of files to be attached, it generates unique name to the file (by appending _1, _2 etc.) and attaches the file (similar to Metalogix behavior).

How to run the script?

Pre-requisite to run this script is you need to install the Client Side SharePoint PowerShell from CodePlex. The API uses the managed .NET Client-Side Object Model (CSOM) of SharePoint 2013. It's a library of PowerShell Scripts.

Here are the steps to make it work:

  • Download Client side SharePoint PowerShell. If you want to run it from your non-SharePoint machine, you also need to install SharePoint Client Libraries.

  • Download Add-ListItemAttachment.ps1 (attached) and store in the same folder as Client Side SharePoint PowerShell scripts.

  • Open the command prompt as Administrator.

  • Change the directory where Client Side SharePoint PowerShell Scripts and Add-ListItemAttachment.ps1 file is stored.

  • Load the PowerShell with command "PowerShell".

  • Enter "Add-ListItemAttachment.ps1" and press TAB.

  • Press ENTER and follow on screen instructions.