Try MySite.GetContent() Catch(Useless Info) Finally Site.Close() End Try

The guide to programming and analyzing SQL, .NET, and SAP
posts - 17, comments - 79, trackbacks - 0

My Links

SQLTeam.com Links

News

Hello World. I'm Jon Hermiz and am a software engineer here in Michigan. Feel free to look around and leave feedback!

Archives

Post Categories

How to pull Sharepoint list data from SQL Server

If anyone has dealt with Sharepoint they know how difficult it can be to get custom web controls, data extraction, and general programming completed in this monster.  There is a great thing about Sharepoint in that its back end is SQL Server.  But along with the pro comes a con, obtaining that data is very painful.  Why?  Well to be quite honest I don't think the job that Microsoft did with creating the Sharepoint database was all that great.  Reasons being:

  • foreign keys (if any…) are never given the same column name as the primary key
  • column names being cryptic
  • for instance tpID in one table (as a pk) can be tpListID in another table (as the foreign key)
  • referential integrity is used in some areas and forgotten??? in other tables
  • the use of NULLS which can return disastorous results if you are not careful is misused beyond belief
  • and whatever happened to 3NF (check out the table UserData for instance)?

Ok after reading that list most of you are thinking why on earth would I ever use such a tool, the answer is I have to, it’s Microsoft.  Anyhow as I was saying the nice thing is it’s got that SQL backend which means I can use that data to report on in Reporting Services.  Sharepoint is nice as a basic package, but companies always have specific business requirements which cannot all be embedded by MS in Sharepoint.  So one must program or at least change a business process to get that functionality in Sharepoint.

One important object used in Sharepoint is the List object.  Lists are much like your .NET data grids allowing users to enter data and fill a grid with relevant information.  The information in a list is stored in the UserData table (which is an odd name for the table), and the lists themselves are stored in the Lists table.  A list in the database has a GUID representation.  So say you need to create a report with the data inside your list one can query these two tables to get this data.  So here is a nice little example for you:

SELECT      dbo.UserData.tp_ID,
  dbo.UserData.tp_ListId,
  dbo.UserData.tp_Author,
  dbo.UserData.nvarchar1,
                dbo.UserData.nvarchar2,
  dbo.UserData.nvarchar3,
  dbo.UserData.nvarchar4,
  dbo.UserData.nvarchar5,
                dbo.UserData.nvarchar6,
  dbo.UserData.nvarchar7,
  dbo.UserData.nvarchar8,
  dbo.UserData.nvarchar9 ,
                dbo.UserData.nvarchar10,
  dbo.UserData.nvarchar11,
  dbo.UserData.nvarchar12,
  dbo.UserData.*                  –dont forget to modify this to snatch only the columns you need
FROM            dbo.Lists
INNER JOIN
                dbo.UserData ON dbo.Lists.tp_ID = dbo.UserData.tp_ListId
WHERE    
(dbo.UserData.tp_ListId = ‘{B44327F5-95E9-4504-A3BF-1E6751C452D6}’) 

–optional WHERE clause, this pulls data for ONE list

Notice my WHERE condition is simply pulling out one list’s data.  You can eliminiate this or use a parameter (with the actual list name as the selection to the end user).  You can also perform any SQL operation on this query since the data is simply sitting on SQL.  Then simply use this as your reports dataset, add some parameters and voila a Sharepoint report!

Print | posted on Wednesday, August 15, 2007 11:25 AM

Feedback

# re: How to pull Sharepoint list data from SQL Server

nbbbbbbbbbbbb
8/23/2007 5:13 AM | Yogesh Pande

# re: How to pull Sharepoint list data from SQL Server

I am new to this work area.
Here i want ex. like how can i fetch all Database table data into share Point list.
Anyone knows pls let me know.
Thanke in advance :)
Yogesh Pande.
8/24/2007 3:07 AM | Yogesh Pande

# re: How to pull Sharepoint list data from SQL Server

Could you please explain something for me?

====
SELECT dbo.UserData.tp_ID,
dbo.UserData.tp_ListId,
dbo.UserData.tp_Author,
dbo.UserData.nvarchar1,
====

does tp_ListId, tp_Author, etc get changed to field and list names or do you have to fiddle with these fields until they match your list? I've noticed nvarchar goes from 1-64.......if you have to fiddle with these fields, how do I know which goes where?

12/24/2007 5:10 AM | Michael

# re: How to pull Sharepoint list data from SQL Server

This is a great article thanks.

However, I do have another issue as well. I have a list that has lookups to other lists in it. Meaning I have created a list to provide a drop down for attribute "promotion type". This list of "promotion type" is a lookup in list "Promotions" When a user creates a new row in the Promotions list they must choose a drop down of lookup list "promotion type". I am using this like a lookup table or referencial key. I need to query this data for reports etc. These columns do not show up in a query such as this. Do you know how to reference this data?

Thanks.

bowman
2/5/2008 3:34 PM | bowmandba

# re: How to pull Sharepoint list data from SQL Server

This is great. But, if some of my fields are lookups, it looks like that data is stored in a different table (can be viewed in userdatajunctions). Any idea how it references back to userdata? It would appear to be the tp_fieldId column, but I can't see that referenced anywhere else.

Thanks for any help you can provide.

jay
3/17/2008 12:29 PM | userjay

# re: How to pull Sharepoint list data from SQL Server

I was just wondering if any of the comments got answers. Maybe I do not have the idea of these type sites yet. I often see an article followed by comments and no answers.

How do you get the list id and even the database name for the list?

I actually went thru the exercise once of use SQL Server Manager to find out these details for a survey.
This was easy, my sharepoint had little else. was a lot of work and trying to match up the rows was not easy.
Someone at work found an answer (that he has not shared yet) involving some query that seemed to give the answe.
well, just looking around myself.
4/4/2008 5:04 PM | Forums blogs

# re: How to pull Sharepoint list data from SQL Server

You get the list id by querying the lists table using a LIKE query and searching for the NAME of your site.
It really is that simple.

4/7/2008 1:05 PM | Jon H

# re: How to pull Sharepoint list data from SQL Server

Actually to get the list id you need to look at the Webs table and query the field Title using a like query.
For instance, if you have a site called "Jons Tests" and you need the list id you do something to this effect:
SELECT *
FROM Webs
WHERE (Title LIKE '%Test%')

That will give you the id you need to go into the lists table and query the data for this site using this list id.
4/8/2008 11:37 AM | Jon H

# re: How to pull Sharepoint list data from SQL Server

OK, I am intrigued but I was wondering more about the way Excel connects back to SharePoint after an export of a list. There is an IQY file that can be saved that seems to contain a call to the owssrv.dll to extract XMLDATA.

Would there be a way to leverage that mechanism to pull XML data into SQL Server through SSIS on a regular basis? This direction would seem to be more in line with a supported means to pull the data versus a back-end way.

My problem is that I am on a corporate SharePoint server withno access to the back-end SQL Server database.
4/21/2008 11:20 PM | Dave Jackson

# re: How to pull Sharepoint list data from SQL Server

I have a custom list that has lookups to other lists in it. If I select all field of my custom list I cannot find the fields with the lookup multi values. Can you tell me how to make a link between the lookup list and my list to find the selected values or better to show the selected items in the custom list?
6/12/2008 9:46 AM | Rien

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 5 and 1 and type the answer here:

Powered by: