Objective : programmatically upload documents to Sharepoint
Issues :
When trying to use : [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
it does not error but it does error when I try to use the objects it contains. Such as New-Object Microsoft.SharePoint.SPSite($url)
. The fix to this is to download the "Microsoft.Sharepoint.dll" which is contained in the web extensions folder on the actual server the MOSS is installed on. The problem with that is to be running these scripts on the 2008 server that hosts our Sharepoint site is in no way 'practical' in a big company.
My second approach was to use an internet explorer com object to physically navigate to the page and upload the file from there. After getting the ID of the text box I am not able to set the value, due to what my research shows as "security measures" by Microsoft. when using the get-member
cmdlet I am able to see that this html element has a field value that is supposedly 'System.String value {get;set;}'. When I try to set the value nothing happens. The text box itself on the website does not allow you to type anything in. Instead the text box is clickable and brings up a "choose file to upload" window. If I manually choose a file to upload without submitting it, go back to the ISE and and do $uploadText.Value
it spits out the file I just chose to console.
My third ditch effort was to use the sendkeys object found in VB to send the path to the web browser and upload the file that way. But when running it the program just hangs and never sends it. This strategy I found at : http://technet.microsoft.com/en-us/library/ff731008 was described to be "a little … temperamental ….". I first make the $ie object open the Microsoft file browser and then try to send keys to it.
The attempted code : (using IE com object, AppActivate and Sendkeys):
$spURL = "https://SharepointSite"
[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
$ie = new-object -com "InternetExplorer.Application"
$ie.visible = $true
$ie.navigate2($spURL)
while($ie.Busy -eq $true) {Start-Sleep 1}
($ie.Document.GetElementByID("zz16_UploadMenu")).click()
while($ie.Busy -eq $true) {Start-Sleep 1}
$uploadText = $ie.Document.GetElementByID("ctl00_PlaceHolderMain_ctl01_ctl02_InputFile")
[Microsoft.VisualBasic.Interaction]::AppActivate("Upload Document")
$uploadText.value = "C:\uploadFile.xlsx" #this line does nothing
$uploadText.click() ## opens up "choose file to upload"
[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
[System.Windows.Forms.SendKeys]::SendWait("C:\uploadFile.xlsx")
And that is it, the browser box pops up, and the script never ends. I have found that some other people have had trouble with setting AppActivate to "choose file to upload" (which I had it set to before I changed it to the IE title "Upload Document"). These problems were all in VB though, I did find what someone claimed to be a fix to "Referencing a Microsoft HTML Library" but it is only going to work for VB, and I need this in powershell. That fix can be found here but it does not work for me.
It seems Microsoft just does not want me to be uploading files programmatically. But before I deemed this "Impossible" I just wanted to post it here first. Any help would be greatly appreciated, I thought everything was possible with powershell! :D
Due to Security reasons you can't do a Fileupload with Comobject IE
I solved that issue by using the watin dll within PS.
I can't Access my Sample from here but you find all you Need at www.watin.org