Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-01-02
13
Medium Priority
?
1,129 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
  • 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

For SharePoint sites, particularly public-facing ones, there are times when adding JavaScript, Meta Tags, CSS Styles or other content to the page <head> section is more practical than modifying master pages.  For instance, you could add the jQuery l…
Microsoft SharePoint Foundation 2010 and Microsoft SharePoint Server 2010 do not offer the option to configure the location of the SharePoint diagnostic trace log files during installation.  This can, however, be configured through Central Administr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

971 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