Solved

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

Posted on 2013-01-02
13
1,094 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

If you create your solutions on SharePoint sooner or later you will come upon a request to set  permissions of the item depending on some of the item's meta-data - the author, people assigned as approvers, divisions, categories etc. The most natu…
Pimping Sharepoint 2007 without Server-Side Code Part 1 One of my biggest frustrations with Sharepoint 2007 in the corporate world is that while good-intentioned managers lock down the more interesting capabilities of Sharepoint programming in…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

15 Experts available now in Live!

Get 1:1 Help Now