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.

Thanks
BKennedy2008Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rainer JeschorCommented:
Hi,
sorry, not in VB (but should be easily convertable):
- Using SharePoint Web Services
http://dmitriybelous.com/2011/07/13/uploading-file-programmatically-and-setting-the-metadata/

- Using SharePoint Client Object model:
(method 2)
http://www.learningsharepoint.com/2010/07/12/programmatically-upload-document-using-client-object-model-sharepoint-2010/

HTH
Rainer
0
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);

context.Load(uploadFile);
 
context.ExecuteQuery();
 
SPClient.ListItem item = uploadFile.ListItemAllFields;
 
//Set the metadata
 
string docTitle = string.Empty;
item["Title"] = docTitle ;
 
item.Update();
 
context.ExecuteQuery();
0
Rainer JeschorCommented:
Hi,
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)
        ctx.Load(uploadedFile)
        ctx.ExecuteQuery()

        Dim item As ListItem = uploadedFile.ListItemAllFields
        For Each key As String In metaDataDictionary.Keys
            item(key) = metaDataDictionary(key)
        Next
        item.Update()
        ctx.ExecuteQuery()
        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
Microsoft.SharePoint.Client.Runtime
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.

HTH
Rainer
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BKennedy2008Author Commented:
Sorry, Rainer, I have been working on another project. I am going to jump on this and let you know. Looks good
0
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?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SharePoint

From novice to tech pro — start learning today.