Upload/Download documents to Sharepoint via PowerShell

I used to have a task to download and upload riport docus to a Sharepoint library. Nothing special as i used the .Net framework possibilities just a few hints at that time.

So here are script samples to download and upload docus. The most important thing at that time that we get the docu not as a byte-stream but a file-stream one. So it’s much more faster 🙂

#Downloading Document from DocumentLibrary
$clnt = New-Object system.net.webclient
$clnt.UseDefaultCredentials = $True
$url = $List_item.ParentList.ParentWeb.Url + "/" + $List_item.Url
$reportfile_path = $riportfile_path_local + $Reportfilename
$clnt.DownloadFile($url,$Reportfile_path)

#Upload Document to DocumentLibrary
$ReportID = $List_item["Riport ID"]
<a href="mailto:$propbag=@%7B%22ContentType%22=%22ocument">$propbag=@{"ContentType"="ocument</a>",
"Riport ID"=$ReportID}
$file_to_upload = (get-item $Reportfile_path).openread()
$templist = $web.Lists | where { $_.Title -eq "something" }
$docliburl = $defaultsite_url + $templist.Rootfolder.tostring() +"/”
$folder=$web.getfolder($docliburl)
folder.files.Add($Reportfilename,$file_to_upload,$propbag, $true) > $null
$file_to_upload.close()</pre>

NOTE that the codepage could be modified by Sharepoint during the download/upload process. To get rid of that situation you should read that post: http://www.aspnetresources.com/blog/unicode_in_vsnet.aspx

The main point of that post is to change the web.config file according to the given webapp to include the next lines (think on your codepage):

<globalization
requestEncoding=”utf-8″
responseEncoding=”utf-8″
fileEncoding=”utf-8″ />

If there is no any req or response encoding definition in hte web.config file the sharepoint will use the given server’s codepage. So the failure could happen when the client’s codepage differs from the server’s codepage 🙂

 

Advertisements

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