Sharepoint File Upload via VB.NET

I recently had reason to upload a lot of files to Sharepoint 2010. Unfortunately the Upload item in the menu would only allow me to do this in batches of 100.  Fortunately however, you can do this from code.

    Public Function UploadToSharepoint(ByVal localFile As String, ByVal remoteFilename As String, spUrl As String, username As String, password As String, domain As String) As Byte()
 
        ' Ensure that we've got a valid URL.
        If Not spUrl.EndsWith("/") Then spUrl &= "/"
 
        ' Build the remote url, escape spaces and replace backslashes.
        Dim rfURL As String = String.Format("{0}{1}", spUrl, remoteFilename).Replace(" ", "%20").Replace("\", "/")
 
        ' Create a webclient.
        Dim wc As New WebClient
        wc.Credentials = New System.Net.NetworkCredential(username, password, domain)
 
        ' Attempt the upload.
        Try
            Using s As New System.IO.FileStream(localFile, IO.FileMode.Open, IO.FileAccess.Read)
                Using reader As New System.IO.BinaryReader(s)
                    UploadToSharepoint = wc.UploadData(rfURL, "PUT", reader.ReadBytes(CInt(s.Length)))
                End Using
            End Using
 
        Catch ex As Exception
            UploadToSharepoint = Nothing
 
        End Try
 
    End Function

VB.NET Iteration Time

I recently had reason to perform an operation on a bunch of files in a directory. I wanted to view a running average iteration time. It would have been easy to use a collection / generic / array, sum the contents and divide by the collection / generic / array length, but I knew there was a simpler way, having done it before. Linq to the rescue! Here the function :

 

    Public Function IterateFiles(path As String) As Double
 
        Dim iterationDurations As New List(Of Double)
 
        For Each fileName As String In IO.Directory.GetFiles(path, "*.*", IO.SearchOption.AllDirectories)
 
            Dim iterationStartTime As Date = Now
 
            ' ... Your operation here...
 
            Dim iterationEndTime As Date = Now
 
            iterationDurations.Add((iterationEndTime - iterationStartTime).TotalMilliseconds)
 
            Console.Write("{0} Avg Iteration Time {1}", vbCr, System.Linq.Enumerable.Average(iterationDurations).ToString("F2"))
 
        Next
 
        Return System.Linq.Enumerable.Average(iterationDurations)
 
    End Function

SAP Business One Connection

SAPbobsCOM Company

In the first of (hopefully) many (providing I keep the momentum going) posts regarding coding, I have documented the VB.NET code required to connect and return a SAP Business One Company. So here we go.

    Private Function Company(server As String, userName As String, SecurePW As Security.SecureString, _
                             companyDB As String, dbUsername As String, dbPassword As String, _
                             ByRef errorMsg As String) As SAPbobsCOM.Company
 
        Company = New SAPbobsCOM.Company
 
        ' Get the secure password 
        Dim ptr As IntPtr = Runtime.InteropServices.Marshal.SecureStringToBSTR(SecurePW)
 
        ' Set up the connection to SAP 
        Dim returnCode, errorCode As Integer
        With Company
            .Server = server
            .CompanyDB = companyDB
            .UserName = userName
            .Password = Runtime.InteropServices.Marshal.PtrToStringBSTR(ptr)
            .language = SAPbobsCOM.BoSuppLangs.ln_English
            .DbUserName = dbUsername
            .DbPassword = dbPassword
            .UseTrusted = False
            .DbServerType = SAPbobsCOM.BoDataServerTypes.dst_MSSQL2008
 
        End With
 
        ' Try and connect to the database 
 
        returnCode = Company.Connect
        If returnCode = 0 AndAlso Company.Connected = True Then
            ' Success! 
        Else
            Company.GetLastError(errorCode, errorMsg)
            If Company.Connected Then Company.Disconnect()
            Company = Nothing
        End If
 
    End Function

We’re assuming that we are connecting to a SQL Server 2008 instance. Also, the SAP User password is passed in via a SecureString for maximum security. I’ll post the code for a login form that exposes the SecureString password at a later date.

1 2 3 4 5