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.
            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

Sharepoint 2010 & Google Analytics

Discovered that you can set a custom variable in Google Analytics to the ID of the Sharepoint user (that’s ID, not Name) :

This is the tracking code I used. The important part here is the _gaq.push([‘_setCustomVar’ line. We can use the Sharepoint JS variable _spUserId:

<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-xxxxxxxx-x']);
		1,         // This custom var is set to slot #1.  Required parameter.
		'User',    // The name of the custom variable.  Required parameter.
		_spUserId, // This is the Sharepoint JS variable _spUserId. Required!
		2          // Sets the scope to session-level.  Optional parameter.
  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);

Don’t forget to change the UA string from


to the value for your site!


Sharepoint Error, Part 2

Sharepoint Error

“The list cannot be imported because a Microsoft SharePoint Foundation-compatible spreadsheet application is not installed or is not compatible with your browser.”

 I resolved the above error by running the Office Setup routine from the installation disk. Incidentally, you’ll need to use 32 bit Internet Explorer when importing your spreadsheet as a list.

However, I still had an issue, where once i’d clicked ‘Import’, the following error occurred:

“An unexpected error has occurred.”
Handy. Not.

However, the following Microsoft Support Article helped resolved the issue:

It would appear that you have to append ‘&ListBaseType=0&DisplayName=Spreadsheet+Import‘ to the URL of the import page.

Thanks to the following websites for help with this:

1 2