Adding Attachments to a Specific Item in SharePoint


This is a major thing which is used in our day to day activities but also in our major Migration projects in which we need to move Dot Net applications to SharePoint.

Here is a PowerShell which will do that easily for you, Change the following details down in the script.

Open SharePoint 2010 Management Shell by going to Start >> All Programs >>SharePoint >>Microsoft SharePoint 2010 Products >> SharePoint 2010 Management Shell (Run as Administrator).

Run the following script.

  1. [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)
  2.  function AddAttachment($item, $filePath)
  3.  {
  4.  $bytes = [System.IO.File]::ReadAllBytes($filePath)
  5.  $item.Attachments.Add([System.IO.Path]::GetFileName($filePath),$bytes)
  6.  $item.Update()
  7.  }
  8.  $site = [Microsoft.SharePoint.SPSite](“Your site name”)
  9.  $web = $site.OpenWeb()
  10.  $list = $web.Lists[“Your List name”]
  11.  $item = $list.GetItemById(Your Id)
  12.  AddAttachment $item “Your Document Name”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s