views:

54

answers:

3

Hi All,

I'm trying to automate the uploading of files to a SharePoint document library. I've run across countless posts (on this forum and others) but can't seem to get something that works. I'm not really a developer, though I have done some simple VBA and VB Script.

What I'm looking for is a solution to automatically upload a file (.xlsx and .zip types specifically) from a local machine to a specific SharePoint document library (let's use ".../sharepoint/Metrics/Forms/AllItems.aspx" as the list) using VBA or VB Script.

In researching the issue, here are some other thoughts/comments that hopefully will help someone in providing me a solution:

  • I cannot change anything on the SharePoint server
  • I need to be able to pass credentials when uploading the file
  • I am only looking for VBA/VBS solutions (no C# or .NET)
  • I might need to set metadata when uploading

Thank you in advance for any help.

A: 

What about mapping a Drive letter to a SharePoint document library. Then just copy/move files like normal?

http://blog.crowe.co.nz/archive/2005/08/31/244.aspx

Shane
A: 

Your best solution would be using FP RPC (that's frontpage remote procedure call). This is basically a web request, where you pass you metadata and file content as parameters. This can been done from any language, including VBA/VBS This is the formal description of the method: http://msdn.microsoft.com/en-us/library/ms479623.aspx You can find quite a few resources and code sample on how to build the utility

Vladi Gubler
+1  A: 

The following VBScript uploads a file using FrontPage RPC:

Function StringToByteArray(str)
   Set stream = CreateObject("ADODB.Stream")
   stream.Open
   stream.Type = 2 ''adTypeText
   stream.Charset = "ascii"
   stream.WriteText str
   stream.Position = 0
   stream.Type = 1 ''adTypeBinary
   StringToByteArray = stream.Read()
   stream.Close
End Function

Sub UploadFile(sourcePath, siteUrl, docName, title, checkincomment, userName, password)

   strHeader = "method=put+document%3a12.0.4518.1016" + _
      "&service_name=%2f" + _
      "&document=[document_name=" + Escape(docName) + _
      ";meta_info=[vti_title%3bSW%7c" + Escape(title) + "]]" + _
      "&put_option=overwrite,createdir,migrationsemantics" + _
      "&comment=" + _
      "&keep%5fchecked%5fout=false" + vbLf
   bytearray = StringToByteArray(strHeader)

   Set stream = CreateObject("ADODB.Stream")
   stream.Open
   stream.Type = 1 ''adTypeBinary
   stream.Write byteArray

   Set stream2 = CreateObject("ADODB.Stream")
   stream2.Open
   stream2.Type = 1 ''adTypeBinary
   stream2.LoadFromFile sourcePath
   stream2.CopyTo stream, -1
   stream.Position = 0

   Set xmlHttp = CreateObject("MSXML2.XMLHTTP")
   xmlHttp.open "POST", siteUrl + "/_vti_bin/_vti_aut/author.dll", false, userName, password
   xmlhttp.setRequestHeader "Content-Type","application/x-vermeer-urlencoded"
   xmlhttp.setRequestHeader "X-Vermeer-Content-Type","application/x-vermeer-urlencoded"
   xmlhttp.setRequestHeader "User-Agent", "FrontPage"
   xmlHttp.send stream

   If xmlHttp.status = 200 Then

       If Instr(xmlHttp.responseText, "successfully") = 0 Then

          MsgBox "ERROR: " & vbCrLf & xmlHttp.responseText       

       Else

          ''Checkin

          strHeader = "method=checkin+document%3a12.0.4518.1016" + _
             "&service_name=%2f" + _
             "&document_name=" & Escape(docName) + _
             "&comment=" + Escape(checkincomment) + _
             "&keep%5fchecked%5fout=false" + vbLf

          Set xmlHttp = CreateObject("MSXML2.XMLHTTP")
          xmlHttp.open "POST", siteUrl + "/_vti_bin/_vti_aut/author.dll", false, userName, password
          xmlhttp.setRequestHeader "Content-Type","application/x-vermeer-urlencoded"
          xmlhttp.setRequestHeader "X-Vermeer-Content-Type","application/x-vermeer-urlencoded"
          xmlhttp.setRequestHeader "User-Agent", "FrontPage"
          xmlHttp.send strHeader



       End If

   End If

   If xmlHttp.status / 100 <> 2 Then
      MsgBox "ERROR: status = " & xmlHttp.status & vbCrLf & xmlHttp.responseText
   End If

End Sub

UploadFile "C:\Users\myusername\Desktop\Test File.zip", _
    "http://computername/Sites/sitename", _
    "Requirements/Test File.zip", _
    "Test title", _
    "Test checkin comment", _
    "MYDOMAIN\myusername", "mypassword"
MsgBox "Done"

Please note that the file name should consist of ASCII characters only. Otherwise, the above script will not work.

fmunkert
THank you so much! This is just what I needed. I've already got this 'plugged in' and working. The only question I have now is: how can I set the values for other metedata fields? Example...we're required to provide a description and the project name for each document when uploading...so how/where do I set that?
Inside the above script, the metadata "vti_title" is added. You can add further metadata properties, by adding them inside the `meta_info=[...]` brackets. E.g. `";meta_info=[vti_title%3bSW%7c" + Escape(title) + ";project%3bSW%7c" + Escape(project) + "]]"`. You must have a custom field called `project` in the document library. "SW" means that it must be a string value (use "BW" for boolean (true/false), or "IW" for integer).
fmunkert