Solved

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

Posted on 2013-01-02
13
1,113 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

In case you ever have to remove a faulty web part from a page , add the following to the end of the page url ?contents=1
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

752 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