VBScript to upload a file to SharePoint DocLib

I am trying to automate uploading files to a SharePoint document library. I came across countless posts (on this forum and others), but can't seem to get something that works. I am not a developer, although I made some simple VBA and VB Script.

I am looking for a solution to automatically upload a file (especially .xlsx and .zip) from a local computer to a specific SharePoint document library (use "... / sharepoint / Metrics / Forms / AllItems.aspx" as a list) using VBA or VB Script .

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

  • I can’t change anything on the SharePoint server.
  • I need to be able to transfer credentials when uploading a file
  • I am only looking for VBA / VBS solutions (without C # or .NET)
  • I may need to set metadata on boot

Thanks in advance for any help.

+3
source share
4 answers

The following VBScript file loads the 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 must consist of ASCII characters only. Otherwise, the above script will not work.

+8
source

How to map drive letter in a SharePoint document library. Then just copy / move files as usual?

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

0
source
0

, vbscript.

function SPCopy(InFileName, InPath, oFS)

    SPURL = ""
    SPSource = InPath & "\" & InFileName

    ' determine which sharepoint path to copy the excel workbook to
    If InStr(InFileName, "Email") > 0 Then
        SPURL = "\\sharepoint\sites\IS\Shared Documents\Email Reports"
    ElseIf InStr(InFileName, "FTP") > 0 Then
        SPURL = "\\sharepoint\sites\IS\Shared Documents\FTP Reports"
    ElseIf InStr(InFileName, "SSL") > 0 Then
        SPURL = "\\sharepoint\sites\IS\Shared Documents\SSL Reports"
    End If 

    If SPURL = "" Then
        MsgBox "File: " & SPSource & " is not a valid file from STRM..." & vbCrLf & _
            "Not sure where to upload it to SharePoint... " & vbCrLf & vbCrLf & _
            SPSource & " will be deleted...", 48, "myScript"
        Exit Function
    End If

    ' build the final part of the path based on the year and month
    MyDate = Left(InFileName, 4) & "_" & MonthName(Mid(InFileName, 5, 2))
    MyTitle = Mid(InFileName, 10, InStr(InFileName, ".") - 10)
    SPURL = SPURL & "\" & MyDate
    DestFile = SPURL & "\" & InFileName

    ' copy the file(s) to the sharepoint path if its not already there
    If Not oFS.FileExists(DestFile) Then
        oFS.CopyFile SPSource, SPURL, True
    End If
end function
0

Source: https://habr.com/ru/post/1766407/


All Articles