• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5889
  • Last Modified:

File Upload to SharePoint site using VB and Windows forms

Does anyone have any good links to do the following:

Upload a file using VB/ Windows forms to a SharePoint Document Library and updating the columns? (using VS2010 with SharePoint 2010)

I am splitting the name of the file in a event receiver when addeded, but I would think that there is a better way to update the columns than splitting the name of the file, as it has its limits.

  • 3
  • 2
1 Solution
Rainer JeschorCommented:
sorry, not in VB (but should be easily convertable):
- Using SharePoint Web Services

- Using SharePoint Client Object model:
(method 2)

BKennedy2008Author Commented:
I have the code here, I am just having trouble converting it to VB. It doesn't like the code in bold

public Boolean UploadDocument(String fileName, String filePath, List metaDataList)
SP.ClientContext ctx = new SP.ClientContext(“http://yoursharepointURL”);
Web web = ctx.Web;
FileCreationInformation newFile = new FileCreationInformation();
newFile.Content = System.IO.File.ReadAllBytes(@”C:\TestFile.doc”);
newFile.Url = “/” + fileName;
List docs = web.Lists.GetByTitle(“Shared Documents”);

Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);

SPClient.ListItem item = uploadFile.ListItemAllFields;
//Set the metadata
string docTitle = string.Empty;
item["Title"] = docTitle ;
Rainer JeschorCommented:
sorry for the delay but had to find a dev env and time to code.
Here is a fully working VB.NET code (I used it inside of a small windows form app):
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim metaData = New Dictionary(Of String, String)
        metaData.Add("Title", "My Document Title")
        metaData.Add("CustomProp", "My custom property")

        MessageBox.Show(UploadDocument("TestFile1.txt", "D:\\TestUpload1.txt", metaData))
    End Sub

    Public Function UploadDocument(fileName As String, filePath As String, metaDataDictionary As Dictionary(Of String, String)) As Boolean
        Dim ctx As New ClientContext("http://dev.rainerj.local/sites/ee")
        Dim currentWeb As Web = ctx.Web

        Dim fileToUpload As FileCreationInformation = New FileCreationInformation

        fileToUpload.Content = System.IO.File.ReadAllBytes(filePath)
        fileToUpload.Url = "http://dev.rainerj.local/sites/ee/EEMCOM/" + fileName
        Dim docLib As Microsoft.SharePoint.Client.List = currentWeb.Lists.GetByTitle("EEMCOM")

        Dim uploadedFile As Microsoft.SharePoint.Client.File = docLib.RootFolder.Files.Add(fileToUpload)

        Dim item As ListItem = uploadedFile.ListItemAllFields
        For Each key As String In metaDataDictionary.Keys
            item(key) = metaDataDictionary(key)
        Return True
    End Function

Open in new window

Just download and install the appropriate SharePoint Client Objects and in your Visual Studio project add the reference to
Microsoft.SharePoint.Client and
Then add on top of your class / code behind file
Imports Microsoft.SharePoint.Client

Then you simply create a dictionary of string / string to contain the meta data and afterwards you just have to adjust the SharePoint site url. Important is that the file upload needs the full path where the file should be stored.

BKennedy2008Author Commented:
Sorry, Rainer, I have been working on another project. I am going to jump on this and let you know. Looks good
BKennedy2008Author Commented:
Hi Rainer-
 It looks like with this code, I need the dll from the SharePoint Server . I know that DLL sits on the SharePoint Server, and from my understanding, you cannot get that DLL to run on a client computer, correct?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now