Skip to content

Uploading Documents To SharePoint

Uploading documents to SharePoint is a common requirement in any SharePoint project. There are many tools at your disposal to accomplish that task but you can also leverage PowerShell in combination with the .NET FrameWork and the SharePoint object model. The script below is an example of how to upload files to SharePoint (save it as a PowerShell script with a ps1 extension):

begin
{
 $propbag=@{”ContentType”=”Document”
                       “MyCol” =”PowerShell About Docs” }
 $docliburl=”http://moss.newtech.local/Docs/Documents”
 $relweburl=”/Docs”
 [System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”) > $null
 $site=new-object Microsoft.SharePoint.SPSite($docliburl)
 $web=$site.openweb($relweburl)
 $folder=$web.getfolder($docliburl)
 $list=$web.lists[$folder.ContainingDocumentLibrary]
}

process
{

 ## I expect FileInfo objects in the pipeline
 $bytes=get-content $_ -encoding byte
 $bytes=[byte[]]$bytes
 $folder.files.Add($_.Name,$bytes,$propbag, $true) > $null
}

The $propbag variable contains a hashtable with keys and values. It is used to set document properties. In this case, we set the ContentType (using the special key ContentType) and a custom column called MyCol.

Because we will use the classes in Microsoft.SharePoint.dll, that assembly needs to be loaded first (LoadWithPartialName). Then we get the SPSite in $site (http://moss.newtech.local), the SPWeb in $web (/docs, relative to the SPSite), the folder (Documents) in $folder and the SPList in $list.

The process{} script block does the actual uploading and it expects file objects in the pipeline. First, we get the content of each file in byte encoding and convert that to a byte array. To add files to the library, you need to specify 4 parameters to $folder.files.add:

  1. The name for the file in the document library. $_.name is used to use the same name as the original file.
  2. The contents of the file as a byte array.
  3. The hashtable with the properties that need to be set.
  4. $true to overwrite and $false if not.

How to use the script? It is pretty easy actually. Do something like this:

gci C:\WINDOWS\system32\windowspowershell\v1.0\*.txt | ./upload-file.ps1

The above example gets all the txt files from the PowerShell installation directory and uploads them to SharePoint according to the settings in the script. In SharePoint, you will see something like:

Note that you need to run the script on a server with SharePoint installed (WSS or MOSS) and that server needs to have PowerShell as well. Also note that this script is just an example of what is possible with some basic functionality. But it should be easy to extend according to your needs. Have fun!

One Comment

  1. Alexey wrote:

    A nice example, but it works some 10-60 times slower than WebDAV drag & drop (16 KB/sec local %windir%\system32\LogFiles upload - about 280 KB each). Any ideas as to how we can improve performance?

    Posted on 07-Feb-08 at 7:08 am | Permalink

2 Trackbacks/Pingbacks

  1. […] have used SPFile before, for example to upload files. Just check this previous post for more info. But uploading can be as simple as $web.files.add(”filename”, […]

  2. […] a long time ago I blogged about uploading files to SharePoint using PowerShell (see original article). That script used the get-content cmdlet and is very slow. The script below should work better as […]

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*