Pulling Data From SQL Server for Use in Sharepoint 2007

Here is the much anticipated code sample:

Dim Site As New SPSite("{Enter Your Sharepoint Site}")

            Dim Web As SPWeb = Site.OpenWeb

            'Sql Connection Information
            Dim ClientTable As New DataTable 'Datatable

            'Sql Connection Information
            Dim Connect As New SqlConnection("Initial Catalog ={Enter a Database};Data Source={Enter a Server};User ID={Enter a User ID};password={Enter a Password}")
            Dim GetStoredProcedureData As New SqlCommand 'Gets Stored Procedure
            Dim ClientsDataset As New DataSet 'Dataset Variable
            Dim StoredProcedureAdapter 'Data Adapter for Stored Procedure

            Connect.Open()

            'saves data to dataset then appends to invoice files
            GetStoredProcedureData.Connection = Connect
            GetStoredProcedureData.CommandType = CommandType.Text
            GetStoredProcedureData.CommandText = "{A Select Statement Needs to be Inserted Here}"
            StoredProcedureAdapter = New SqlDataAdapter(GetStoredProcedureData)
            StoredProcedureAdapter.Fill(ClientsDataset)

            Dim i As Integer 'row increment
            Dim RowCount As Integer 'row length
            Dim DocumentLibraryName As String
            Dim NameDescription As String

'gets Sharepoint Site Information, this example only has one table, it starts at 0 for the first tables and goes on
            ClientTable = ClientsDataset.Tables(0)
            RowCount = txtCount.Text
            i = txti.Text
            While i < RowCount
                'My example has two items coming from the database, whereas yours may have more items
                'The
                DocumentLibraryName = ClientTable.Rows(i).Item(0).ToString()
                NameDescription = ClientTable.Rows(i).Item(1).ToString()

                'Create a Document library with a Microsoft Office Word
                'document(template)
                Dim newListGuid As Guid = Web.Lists.Add(NameDescription, NameDescription, Web.ListTemplates("Document Library"), Web.DocTemplates(1))

                'Get the new list using the returned GUID
                Dim list As SPList = Web.Lists(newListGuid)

                'Makes the Quick Launch visible in the Document Library
                list.OnQuickLaunch = True

                'Allows you to use versioning if you want
                list.EnableVersioning = True

                'You Need this statement to commit updates int he document library, otherwise none of the settings will take into effect
                'If you are creating a regular list you will still need this statement
                list.Update()
                i = i + 1
            End While

There may be other ways to pull this data.  Also, you can probably push it into a datagrid, rather than create the document library.  It's really not that hard.  I am sure someone has a code sample for creating a datagrid in VB .Net or use it to create sites.  You can deploy this in a webpart.  You would just put the code into a class library.  It is really simple to translate into C#.  You can pretty much do anything in Sharepoint that you can do in .Net.  The new webpart I am working on rips off a 3 digit code at the end of a url and finds a specific name in the database, then creates a list entry based on the user and what site they visited.  I am really loving playing with the object model.  I would encourage you guys to experiment and let me know some of the ideas you find.

Leave a Reply