Guest User

Untitled

a guest
Nov 23rd, 2017
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.48 KB | None | 0 0
  1. Set objXML = New XMLHTTP 'CreateObject("Microsoft.XMLHTTP")
  2. Dim bytBinary() As Byte
  3. Dim strSharePointUrl As String
  4. Dim strSharePointFileName As String
  5. Dim strFileName As String
  6.  
  7. ReDim bytBinary(lngFileLength)
  8. Open strFullfileName For Binary As #1
  9. Get #1, , bytBinary
  10. Close #1
  11.  
  12. ' Convert to variant to PUT.
  13. varBinData = bytBinary
  14. strTargetURL = strSharePointFileName
  15.  
  16. ' Put the data to the server, false means synchronous.
  17. objXML.Open "PUT", strTargetURL, False ', username, password
  18.  
  19. On Error Resume Next
  20. ' Send the file in. Note that the Name field in SharePoint is updated with the filename.
  21. objXML.send varBinData
  22.  
  23. 'IMORTANT: The WILDCARD is % and NOT * because this is a SQL Table and ADO Connection!
  24. rst.Open "SELECT * FROM [" & strListName & "] WHERE [Name] Like '%" & strFileName & "%'", cnn, adOpenDynamic, adLockOptimistic
  25.  
  26. 'Note that Manditory fields will cause the List/Library Item to check Out the item
  27. 'Not sure how to check in at the moment.
  28. If Not rst.EOF Then
  29. 'Debug.Print "ID:" & rst("ID") & " Name:" & rst("Name")
  30.  
  31. 'SharePoint Item Update
  32. rst("Name") = strFileName
  33. rst("Title") = !Title
  34. rst("Originator") = !Originator
  35. etc....
  36.  
  37. ' http://stackoverflow.com/questions/3787870/vbscript-to-upload-file-to-sharepoint-doclib
  38.  
  39. ' Find Front Page Server Extentions version here! Currently 15.0.0.4517
  40. ' http://websrvstg:2015/_vti_pvt/service.cnf
  41. ' http://intrwebsrvstg01:17404/_vti_pvt/service.cnf
  42. ' Good Atricle found here: http://thuansoldier.net/?p=4298
  43.  
  44. ' http://websrvstg:2015/_vti_pvt/service.cnf -> returns this
  45. ' vti_encoding:SR|utf8-nl
  46. ' vti_extenderversion:SR|15.0.0.4517
  47.  
  48. ' http://intrwebsrvstg01:17404/_vti_pvt/service.cnf -> returns this
  49. ' vti_encoding:SR|utf8-nl
  50. ' vti_extenderversion:SR|15.0.0.4763
  51.  
  52. ' http://websrvstg:2015/_vti_inf.html -> View Source gets this
  53. ' <!-- FrontPage Configuration Information
  54. ' FPVersion = "15.00.0.000"
  55. ' FPShtmlScriptUrl = "_vti_bin/shtml.dll/_vti_rpc"
  56. ' FPAuthorScriptUrl = "_vti_bin/_vti_aut/author.dll"
  57. ' FPAdminScriptUrl = "_vti_bin/_vti_adm/admin.dll"
  58. ' TPScriptUrl = "_vti_bin/owssvr.dll"
  59. ' -->
  60.  
  61. ' http://webdesign.about.com/library/bl_url_encoding_table.htm
  62. ' %3a as a colon :
  63. ' %2f is a slash /
  64. ' %5f is underscore _
  65. ' %7C is pipe |
  66. ' %3b is semi colon ;
  67. ' %3bSW%7c is ;SW|
  68.  
  69. ' Adding Metadata explained below.
  70. ' Inside the above script, the metadata "vti_title" is added.
  71. ' You can add further metadata properties, by adding them inside the meta_info=[...] brackets.
  72. ' E.g. ";meta_info=[vti_title%3bSW%7c" + Escape(title) + ";project%3bSW%7c" + Escape(project) + "]]".
  73. ' You must have a custom field called project in the document library.
  74. ' "SW" for String
  75. ' "BW" for Boolean (true/false)
  76. ' "IW" for Integer
  77. ' "TR" for Date. Must use dd mmm yyyy hh:nn:ss format even if it's a Date Only field
  78.  
  79. strMetaData = "vti_title" & "%3b" & "SW" & "%7c" & URLEncode(!Title, True) 'FixStringForSQL(strFileName)
  80. strMetaData = strMetaData & ";Originator" & "%3b" & "SW" & "%7c" & URLEncode(!Originator, True)
  81. strMetaData = strMetaData & ";Project" & "%3b" & "IW" & "%7c" & CStr(m_colProjects.Item(Trim(!ProjectNumber)))
  82. If Not IsNull(!TransmittalNumber) Then
  83. strMetaData = strMetaData & ";Transmittal+Number" & "%3b" & "SW" & "%7c" & URLEncode(!TransmittalNumber, True)
  84. End If
  85. If Len(Nz(!TransmittalDate, "")) > 0 Then
  86. strMetaData = strMetaData & ";Transmittal+Date" & "%3b" & "TR" & "%7c" & Format(ToUTC(Trim(!TransmittalDate)), "dd mmm yyyy hh:nn:ss")
  87. End If
  88.  
  89. strDataID = Split(!DataID, "-")
  90. strMetaData = strMetaData & ";Data+ID" & "%3b" & "SW" & "%7c" & !DataID
  91.  
  92. 'Extract From JACOS Data ID This returns the ID of the Collection for the name fed in.
  93. strMetaData = strMetaData & ";Facility+Code" & "%3b" & "IW" & "%7c" & CStr(m_colFacilityCodes.Item(strDataID(0)))
  94. strMetaData = strMetaData & ";Area+Designation" & "%3b" & "IW" & "%7c" & CStr(m_colAreaDesignations.Item(strDataID(1)))
  95. strMetaData = strMetaData & ";Discipline+Code" & "%3b" & "IW" & "%7c" & CStr(m_colDisciplineCodes.Item(strDataID(2)))
  96. strMetaData = strMetaData & ";Document+Type" & "%3b" & "IW" & "%7c" & CStr(m_colDocumentTypes.Item(strDataID(3)))
  97. strMetaData = strMetaData & ";Sequence+Number" & "%3b" & "SW" & "%7c" & strJACODDataID(4)
  98. strMetaData = strMetaData & ";Sheet+Number" & "%3b" & "SW" & "%7c" + strJACODDataID(5)
  99.  
  100. strMetaData = strMetaData & ";Document+Revision" & "%3b" & "SW" & "%7c" & !DocumentRevision
  101.  
  102. If IsDate(!RevisionDate) Then
  103. strMetaData = strMetaData & ";Revision+Date" & "%3b" & "TR" & "%7c" & Format(ToUTC(!RevisionDate), "dd mmm yyyy hh:nn:ss")
  104. End If
  105. strMetaData = strMetaData & ";File+Status" & "%3b" & "IW" & "%7c" & CStr(m_colFileStatuses.Item(!FileStatus))
  106. strMetaData = strMetaData & ";Tag+Number" & "%3b" & "SW" & "%7c" & URLEncode(Nz(!TagNumber, ""), True)
  107. strMetaData = strMetaData & ";Cross+Reference" & "%3b" & "SW" & "%7c" & URLEncode(Nz(!CrossReference, ""), True)
  108.  
  109. strHeader = "method=put+document%3a" & str_vti_extenderversion & _
  110. "&service_name=%2f" & _
  111. "&document=[document_name=" & FixStringForSQL(Replace(strListName, " ", "") & "/" & strFileName) & _
  112. ";meta_info=[" & strMetaData & "]]" & _
  113. "&put_option=overwrite,createdir,migrationsemantics" & _
  114. "&comment=" & _
  115. "&keep%5fchecked%5fout=false" & vbLf
  116.  
  117. ByteArray = StringToByteArray(strHeader)
  118.  
  119. Set Stream1 = New ADODB.Stream
  120. Stream1.Open
  121. Stream1.Type = adTypeBinary
  122. Stream1.Write ByteArray
  123.  
  124. Set Stream2 = New ADODB.Stream
  125. Stream2.Open
  126. Stream2.Type = adTypeBinary
  127. Stream2.LoadFromFile strFullfileName
  128. Stream2.CopyTo Stream1
  129. Stream1.position = 0
  130. Stream2.Close
  131.  
  132. strTargetURL = strSharePointFileName
  133.  
  134. ' Upload the data to the server, false means synchronous.
  135. Set objXML = New MSXML2.XMLHTTP
  136.  
  137. objXML.Open "POST", strSharePointUrl + "/_vti_bin/_vti_aut/author.dll", False
  138. objXML.setRequestHeader "Content-Type", "application/x-vermeer-urlencoded"
  139. objXML.setRequestHeader "X-Vermeer-Content-Type", "application/x-vermeer-urlencoded"
  140. objXML.setRequestHeader "User-Agent", "FrontPage"
  141.  
  142. On Error Resume Next
  143. objXML.send Stream1
  144.  
  145. Stream1.Close
  146.  
  147. If Err.Number <> 0 Then
  148. 'Release the handle on the files
  149. Set objXML = Nothing
  150. Set Stream1 = Nothing
  151.  
  152. ErrorNumber = Err.Number
  153. ErrorMessage = Err.Description
  154. .Edit
  155. !ErrorMessage = !ErrorMessage & "File:" & strFileName & " (" & Format(dblFileSize, "#,###") & " KB) Error:" & ErrorMessage
  156. .Update
  157. g_rstADODB.Close
  158. GoTo ExitHereTechnicalData
  159. End If
  160. On Error GoTo ErrorHandler
  161.  
  162. 'Debug.Print "Upload: " & objXML.Status & " " & objXML.StatusText '& " :: " & objXML.responseText
  163. If objXML.Status = 200 Then
  164. If InStr(objXML.responseText, "successfully") = 0 And Len(objXML.responseText) > 0 Then
  165. If objXML.responseText <> "" Then
  166. .Edit
  167. !ErrorMessage = !ErrorMessage & "File:" & strFileName & " Status:" & objXML.Status & " XML Error:" & objXML.responseText
  168. .Update
  169.  
  170. 'g_rstADODB.Close
  171. GoTo ExitHereTechnicalData
  172. Else
  173. .Edit
  174. !ErrorMessage = !ErrorMessage & "File:" & strFileName & " Status:" & objXML.Status & " Error: No Error returned. Check File in SharePoint!"
  175. .Update
  176. 'Carry on!
  177. End If
  178. Else
  179.  
  180. ' Check In Document whether it needs it or not!
  181. strHeader = "method=checkin+document%3a" & str_vti_extenderversion & _
  182. "&service_name=%2f" & _
  183. "&document=[document_name=" & FixStringForSQL(Replace(strListName, " ", "") & "/" & strFileName) & _
  184. "&comment=MS Access Check In" & _
  185. "&keep%5fchecked%5fout=false" & vbLf
  186.  
  187. Set objXML = New MSXML2.XMLHTTP
  188. objXML.Open "POST", strSharePointUrl + "/_vti_bin/_vti_aut/author.dll", False
  189. objXML.setRequestHeader "Content-Type", "application/x-vermeer-urlencoded"
  190. objXML.setRequestHeader "X-Vermeer-Content-Type", "application/x-vermeer-urlencoded"
  191. objXML.setRequestHeader "User-Agent", "FrontPage"
  192. objXML.send strHeader
  193.  
  194. 'Debug.Print "CheckIn: " & objXML.Status & " " & objXML.StatusText
  195.  
  196. 'At ths point we know the SharePoint Metadata was updated so update the temporary record
  197. .Edit
  198. !MetadataUpdated = True
  199. !UpdateDate = Now
  200. .Update
  201.  
  202. End If
  203.  
  204. Else
  205. .Edit
  206. !ErrorMessage = !ErrorMessage & "File:" & strFileName & " Status:" & objXML.Status & " XML Error:" & objXML.responseText
  207. .Update
  208. g_rstADODB.Close
  209. GoTo ExitHereTechnicalData
  210. End If
  211.  
  212. 'Clear Memory
  213. Set objXML = Nothing
  214. Set Stream1 = Nothing
  215. Set Stream2 = Nothing
  216.  
  217. 'At ths point we know the file was uploaded so update the temporary record
  218. .Edit
  219. !UploadedToSharePoint = True
  220. !FileSize = dblFileSize
  221. !UpdateDate = Now
  222. .Update
  223.  
  224. DoEvents
  225.  
  226. 'Clear Memory
  227. Set g_rstADODB = Nothing
  228.  
  229. 'Keeping track of the uploads as we have intMaxDocsForUpload Set
  230. intUploadCounter = intUploadCounter + 1
  231.  
  232. Public Function URLEncode(StringVal As String, Optional SpaceAsPlus As Boolean = False) As String
  233. Dim bytes() As Byte
  234. Dim b As Byte
  235. Dim i As Integer
  236. Dim Space As String
  237.  
  238. If SpaceAsPlus Then Space = "+" Else Space = "%20"
  239.  
  240. If Len(StringVal) > 0 Then
  241. With New ADODB.Stream
  242. .Mode = adModeReadWrite
  243. .Type = adTypeText
  244. .Charset = "UTF-8"
  245. .Open
  246. .WriteText StringVal
  247. .position = 0
  248. .Type = adTypeBinary
  249. .position = 3 ' skip BOM
  250. bytes = .Read
  251. End With
  252.  
  253. ReDim Result(UBound(bytes)) As String
  254.  
  255. For i = UBound(bytes) To 0 Step -1
  256. b = bytes(i)
  257. Select Case b
  258. Case 97 To 122, 65 To 90, 48 To 57, 45, 46, 95, 126
  259. Result(i) = Chr(b)
  260. Case 32
  261. Result(i) = Space
  262. Case 0 To 15
  263. Result(i) = "%0" & Hex(b)
  264. Case Else
  265. Result(i) = "%" & Hex(b)
  266. End Select
  267. Next i
  268.  
  269. URLEncode = Trim(Join(Result, ""))
  270. End If
  271. End Function
  272.  
  273. Public Function ToUTC(ByVal DateTime As Date) As Date
  274. Dim ftLoc@, ftUtc@
  275. ftLoc = (DateTime - #1/1/1601#) * 86400000
  276. LocalFileTimeToFileTime ftLoc, ftUtc
  277. ToUTC = ftUtc / 86400000# + #1/1/1601#
  278. End Function
Add Comment
Please, Sign In to add comment