Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

upload a pdf to SharePoint 2010 document collection site through VB Windows Form

Posted on 2013-01-02
13
Medium Priority
?
1,126 Views
Last Modified: 2013-01-03
I have a windows form in VB, and I am trying to accomplish the following:
1.) Convert a Crystal report to PDF and save it to a folder on local drive
2.) Upload the file to a known SharePoint Document Site collection through VB Code.

I have accomplished step 1, trying to complete step2.

Does anyone have code for uploading through VB a file to a SharePoint Document Site collection, or point me in the right path?
0
Comment
Question by:BKennedy2008
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 2
13 Comments
 
LVL 18

Expert Comment

by:vasto
ID: 38737568
Some existing report viewers and schedulers can do that :
http://www.r-tag.com/Pages/default.aspx
http://remicrystal.com/
http://www.jeff-net.com/jnrrb.htm

However, I don't think somebody will share code from a commercial tool.
0
 

Author Comment

by:BKennedy2008
ID: 38737588
I found this document:
http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/programmatically-adding-document-to-sharepoint-document-library/

Converted to VB, now just researching how to add reference for SharePoint.Dll, not showing up.
0
 

Author Comment

by:BKennedy2008
ID: 38737616
I guess because it's .net 3.5. I hate to revert my project backwards
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:BKennedy2008
ID: 38738170
I take it that you just can't take sharepoint dll from the server, because SPS does not support remote calls unless the app resides on the SPS Server. I must use the web services.
OK - Got that, so how do I incorporate web services from SPS with Windows App?
0
 

Author Comment

by:BKennedy2008
ID: 38738174
Do I use a an event reciever, and will I be able to call that remotely?
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 38738309
0
 

Author Comment

by:BKennedy2008
ID: 38738345
Thanks. I guess I do not want to use web services. I have a program already built in VB, and need to put an add on that will upload to SPS. I don't want to create a web app, but upload it directly from a windows form.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38738436
You do not need a web app to use web services. You can use that from winforms.
0
 

Author Comment

by:BKennedy2008
ID: 38739899
I have this code, but getting a 500 internal error. If I change the username to domain\username I get a 401 unauthorized. I feel I am close...

Imports System.IO
Imports System.Net

Public Class Form1
    Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles UploadButton.Click

        OpenFileDialog1.Title = "Please Select a File"
        OpenFileDialog1.InitialDirectory = "C:temp"

        OpenFileDialog1.ShowDialog()


    End Sub

    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk


        Dim strm As System.IO.Stream
        strm = OpenFileDialog1.OpenFile()
        TextBox1.Text = OpenFileDialog1.FileName.ToString()
        If Not (strm Is Nothing) Then

            Dim uploadedFilePath As String = textbox1.Text
            Dim sharePointListPath As String = _
                "http://SharePointserver/test5/Shared%20Documents/"

            strm.Close()
           

            UploadFileToSharePoint( _
                uploadedFilePath, sharePointListPath)
               
        End If

    End Sub

    Sub UploadFileToSharePoint(ByVal UploadedFilePath As String, _
                ByVal SharePointPath As String)

        Dim response As WebResponse = Nothing

        Try
            ' Create a PUT Web request to upload the file.
            Dim request As WebRequest = _
                WebRequest.Create(SharePointPath)
            Dim networkCredential As New NetworkCredential("test", "test")


            request.Credentials = networkCredential
            request.Method = "PUT"

            ' Allocate a 1 KB buffer to transfer the file contents.
            ' You can adjust the buffer size as needed, depending on
            ' the number and size of files being uploaded.
            Dim buffer() As Byte = New Byte(102300) {}

            ' Write the contents of the local file to the
            ' request stream.
            Using stream As Stream = request.GetRequestStream()
                Using fsWorkbook As FileStream = _
                    File.Open(UploadedFilePath, _
                        FileMode.Open, FileAccess.Read)
                    Dim i As Integer = fsWorkbook.Read(buffer, 0, _
                        buffer.Length)

                    Do While i > 0
                        stream.Write(buffer, 0, i)
                        i = fsWorkbook.Read(buffer, 0, buffer.Length)
                    Loop

                End Using
            End Using

            ' Make the PUT request.
            response = request.GetResponse()
        Catch ex As Exception
            Throw ex
        Finally
            response.Close()
        End Try

    End Sub


End Class
0
 

Author Comment

by:BKennedy2008
ID: 38739955
when I change the path to  "http://SharePointserver/test5/Shared%20Documents/allitmes.aspx" it writes the file to the site, however           "http://SharePointserver/test5/Shared%20Documents/allitems.aspx" becomes corrupt and PDF tries to open when navigating to that site. I uploaded a PDF file and the web browser is tring to open that file when you navigate to it.
0
 

Author Comment

by:BKennedy2008
ID: 38740037
Ok - I got it-

 only change in the above code is this and it works.

UploadFileToSharePoint( _
                uploadedFilePath, sharePointListPath + "/" + "1.pdf")

Now, if a user selects a file from the above code, How can I just pull the filename from the full path so I can make it a string the above "1.PDF" ?
Thanks
0
 

Author Comment

by:BKennedy2008
ID: 38740094
Ok Success- create a function and add:
 Dim sList() As String
        Dim sAns As String
        Dim iArrayLen As Integer
        Dim fullpath As String = textbox1.Text
        Dim namefromfullpath As String

        If Len(fullpath) = 0 Then Exit Sub
        sList = Split(fullpath, "\")
        iArrayLen = UBound(sList)
        sAns = IIf(iArrayLen = 0, "", sList(iArrayLen))

        namefromfullpath = sAns


Thanks CodeCrusier for directing me in the right path....
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Summary In SharePoint 2010 it is easy to create custom color themes to jazz up a site. Theme colors can also be created in PowerPoint 2010 with a few clicks. But how do the chosen colors actually look in the SharePoint site? The attached PowerPoint…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question