Solved

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

Posted on 2013-01-02
13
1,099 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
 

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

We had a requirement to extract data from a SharePoint 2010 Customer List into a CSV file and then place the CSV file into a directory on the network so that the file could be consumed by an AS400 system. I will share in Part 1 how to Extract the Da…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now