Use Integration Services to Import SharePoint List Items to SQL Server

A recent project I worked on called for data captured into a SharePoint list using InfoPath Forms Services to be ultimately imported into a SQL Server database for use with a set of performance reports. 

Given that InfoPath solutions can be designed to submit data to SQL Server (generally via a web service) you may ask why this wasn’t used for the solution.  Well, we wanted to do more stuff with the captured data in SharePoint, using workflows and task assignments before the data finally got approved and loaded into SQL Server.

In summary, the data was going to be submitted using InfoPath and Forms Services as promoted columns in a document library.  Once the appropriate people had reviewed, actioned and submitted the data it would be moved to a separate list, and any new items then needed to be loaded into SQL Server.  From there, Reporting Services would be used to generate and distribute nice looking reports.

So I took a look around to see what tools I might use to get the data into SQL Server with minimum coding effort, and ideally without installing any custom components on the SharePoint server.  I was aware of a set of SharePoint Designer Actions on CodePlex which included an Execute SQL Action.  For me the problem with this was that I was fairly sure I wouldn’t be allowed to install these on the production SharePoint server.

Also on CodePlex is a set of SQL Server Integration Services Community Samples, including SharePoint List Source and Destination Components.  For this project these components seemed more suitable, particularly as we would only need to import new items into SQL Server at the end of each day, and Integration Services would let us schedule a daily import.  And there’s even a MSDN article showing you how to do it.

To show how it works, I created a test SharePoint list called Responses which looks like this (let’s assume this data is coming in via InfoPath)

spResponse

And on SQL Server, I created a table which looks like this:

SQLResponse

Once the List Source and Destination components have been installed into Integration Services, I can create a Data Flow Task to read from SharePoint and write to SQL Server:

Dataflow

And map columns like so -

Mappings

Of course when I run this I only want to import new rows.  The way I’ve handled this is to store the ID of the SharePoint list items in the SQL Server table and execute a SQL task to get the Max ID currently in the table like so –

SELECT ISNULL(MAX(ResponseID),0) AS LastResponseID  FROM Responses

As the SharePoint List Source component has a CAMLQuery property which I can update with a property expression at runtime, I can set it to something like this –

<Query><Where><Gt><FieldRef Name="ID" /><Value Type="Counter">1</Value></Gt></Where></Query>

In fact in the Integration Services Property Expression Editor I created an expression like this (LastResponseID is the output from my SQL Query):

"<Query><Where><Gt><FieldRef Name=’ID’ /><Value Type=’Counter’>" + (DT_STR, 4, 1252)  @[User::LastResponseID] +"</Value></Gt></Where></Query>"

So now if I put all of this together, I have an Integration Services Package which -

- Gets the last imported ID from the SQL Server table

- Queries the SharePoint list for items with an ID greater than the last one imported

- Loads any new list items into the table

Making it easy to get my list items -

ResponseList

Into my SQL Server table -

ResponseData

And I can schedule this to run each night, all without installing any new stuff on the SharePoint server or spending any money on new components!

6 Responses to “Use Integration Services to Import SharePoint List Items to SQL Server”

  1. Best Practices for Developing Web Parts; A Fresh Start for Windows; The End of the E-mail Age? - SharePoint Daily - Bamboo Nation Says:

    [...] Use Integration Services to Import SharePoint List Items to SQL Server (Worker Thread Blog)A recent project I worked on called for data captured into a SharePoint list using InfoPath Forms Services to be ultimately imported into a SQL Server database for use with a set of performance reports. [...]

  2. SharePoint Development Blog by Jeremy Thake in Perth, Australia Says:

    SharePoint Development Weekly Roundup (01Sep)…

    TOP THIS WEEK Deployment Survey
    If you haven’t alr ……

  3. Gil Says:

    Regarding the “Use Integration Services to Import SharePoint List Items to SQL Server”, how might you handle updates to the list items so that they reflect in the SQL DB? Also, what about deletes? This sounds great for end of life list items to go into sql, but if I can’t see the list items as they update in sharepoint also update in SQL then it’s not so good for me. Might you have a solution for this?

    • workerthread Says:

      Hi Gil

      As you say my requirement was for SQL Server to be a repository for completed SharePoint items. But that doesn’t mean you couldn’t use other transformation components of Integration Services to update changed records or capture deletions. Best bet would be to take a look at some of these components, such as LOOKUP and CONDITIONAL SPLIT. There are lots of blog articles covering these, here are a couple of examples -

      Get all from Table A that isn’t in Table B

      Checking if a Row Exists and if it does Has it Changed

      I would think carefully though about data which belongs in SharePoint and data which is obviously relational and should be in the SQL Server DB in the first place.

      Derek

  4. jagan Says:

    hi..gril,
    nice stuff.but here my requirement is i need to run this periodically .means i need to be run end of the day .can u suggest me how to do it.its very urgent.

    Thanks for your efford…
    jagan

    • workerthread Says:

      Integration Services packages can be deployed and then run as scheduled jobs with SQL Agent. Do a Google or Bing search for SSIS deployment or SSIS schedule for lots of articles on how to do this.

      Derek

Leave a Reply