Uploading files and metadata to Sharepoint using vb.net with out Sharepoint libraries.

Was this post useful to you?

Hi,

I'm new to Sharepoint and am a little rusty on VB.net. I'm trying to write code in vb.net (Visual Studio Express) that would upload a file to Sharepoint and also update metadata to a category field. I'm currently able to upload a file using the code below. However I have not been able to update the metadata for the uploaded file. I tried using fp-rpc as suggested here: http://www.textndata.com/forums/fp-rpc-put-document-meta-217769.html
http://stackoverflow.com/questions/3787870/vbscript-to-upload-file-to-sharepoint-doclib
The code below runs and the file is uploaded, but the metadata is not updated. I’ve tried many variations with no success. My problem is that the code runs without throwing any errors and this makes it hard to debug. I would greatly appreciate if someone could provide a working example or perhaps point to what I'm doing wrong.
Thanks,

Martin

Code:
Public Sub PostToSharePoint(PathAndFileNameToPost As String, SharePointURL As String)
If mbrError Then Exit Sub

'PathAndFileNameToPost = "D:\Reports Training\MDG020\Sent\2014-08 Report Name (MDG020) Test 2.xlsx"
'SharePointURL = "http://sharepoint/productmgt/reference/Z OTHER/"

WriteLog("PostToSharePoint: " & PathAndFileNameToPost & " --> " & SharePointURL)
If Not File.Exists(PathAndFileNameToPost) Then
ReportError("PostToSharePoint", "File not found: " & PathAndFileNameToPost)
Exit Sub
End If
Try

'' Send contents of stream to SharePoint
Dim SPFileName As String
SPFileName = SharePointURL & Path.GetFileName(PathAndFileNameToPost)

'Test ###################################################
Dim strHeader As String
'Dim bytearray As Array

strHeader = "method=put+document%3a12.0.4518.1016" + _
"&service_name=%2f" + _
"&document=[document_name=" + """2014-08 Report Name (MDG020) Test 2""" + _
";meta_info=[vti_title%3bSW%7c" + "test" + "%3bCategory%3bSW%7c" + "CTEST" + "]]" + _
"&put_option=overwrite,createdir,migrationsemantics" + _
"&Comment=" + _
"&keep%5fchecked%5fout=false"
'bytearray = StringToByteArray(strHeader)

Dim Stream, Stream2, XMLHTTP2 As Object
Dim sourcePath As String = "D:\Reports Training\MDG020\Sent\"

Stream = CreateObject("ADODB.Stream") ' 1
Stream.Type = 1 ''adTypeBinary ' 2
Stream.Open() ' 3
'Stream.Write(bytearray) '?
Stream.LoadFromFile(PathAndFileNameToPost) '4
'WriteLog(" Loaded " & PathAndFileNameToPost & " (size: " & Stream.Size & ")")

Stream2 = CreateObject("ADODB.Stream") '1
Stream2.Open() '3
Stream2.Type = 1 ''adTypeBinary
Stream2.LoadFromFile(PathAndFileNameToPost)
Stream2.CopyTo(Stream, -1)
Stream.Position = 0

XMLHTTP2 = CreateObject("MSXML2.XMLHTTP.3.0") '5
XMLHTTP2.open("PUT", SPFileName, False, mbrUserID, mbrPW) '6
XMLHTTP2.setRequestHeader("Content-Type", "application/x-vermeer-urlencoded")
XMLHTTP2.setRequestHeader("X-Vermeer-Content-Type", "application/x-vermeer-urlencoded")
XMLHTTP2.setRequestHeader("User-Agent", "FrontPage") XMLHTTP2.send((Stream.Read)) '7

'strHeader = "method=checkin+document%3a12.0.4518.1016" + _
' "&service_name=%2f" + _
' "&document_name=" + "2014-08%20Report%20Name%20(MDG020)%20Test 2.2" + _
' ";meta_info=[vti_title%3bSW%7c" + "test" + "%3bCategory%3bSW%7c" + "CTEST" + "]]" + _
' "&comment=" + "TEST1" + _
' "&keep%5fchecked%5fout=false"

'strHeader = "method=put+document%3a12.0.4518.1016" + _
' "&service_name=%2f" + _
' "&document=[document_name=""2014-08%20Report%20Name%20(MDG020)%20Test%202.2""" + _
' "%3bmeta%5finfo%3d%5bMarket%3bSW%7cMarketA%3bCategory%3bSW%7c%5c%3b%23Cat1%5c%3b%23Cat2%5c%3b%23%5d"

'XMLHTTP2 = CreateObject("MSXML2.XMLHTTP") '1
'XMLHTTP2.open("Post", SPFileName, False, mbrUserID, mbrPW)
'XMLHTTP2.setRequestHeader("Content-Type", "application/x-vermeer-urlencoded")
'XMLHTTP2.setRequestHeader("X-Vermeer-Content-Type", "application/x-vermeer-urlencoded")
'XMLHTTP2.setRequestHeader("User-Agent", "FrontPage")
'XMLHTTP2.send((strHeader))

'End test #############################################

' Send email if file didn't post - ?
Dim iXmlHttpStatus As Integer
'change back to XMLHTTP.Status
iXmlHttpStatus = XMLHTTP2.Status ' 200 = OK; 201 = CREATED
'iXmlHttpStatus = XMLHTTP.Status ' 200 = OK; 201 = CREATED
If (iXmlHttpStatus <> 200 And iXmlHttpStatus <> 201) Then
ReportError("PostToSharePoint", "File did not post successfully; status code is " & iXmlHttpStatus, CCAdmin:=True, LogSystemError:=True)
Else
WriteLog(" File successfully posted to SharePoint.")
End If
Catch e As Exception
ReportError("PostToSharePoint", e.Message, ToAdmin:=True, LogSystemError:=True)
End Try
End Sub

Martin's picture
Martin
Thu, 09/18/2014 - 16:19

Post your reply

Filtered HTML

  • Allowed HTML tags: <b> <br> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.