Working With Share Point Data

Microsoft likes to tout the fact that SharePoint 2007 is built on top of ASP.Net 2.0. While that is true, the fact is that there are a lot of differences, limitations and "gotchas" in working with SharePoint for the ASP.Net developer. One of the most common of these is working with data in the SharePoint environment. In this presentation we will build a SharePoint data structure explorer web part, using the construction process as a basis for getting to know the object model used for working with data stored in SharePoint.

4.
What this talk is not<br />It’s not about how to build web parts<br />However, we will build some<br />It’s not a CAML tutorial<br />But we will discuss it and use it a bit<br />It’s pretty much unavoidable<br />It’s not a solution to all your SharePoint data woes<br />We could spend days talking about this stuff and not cover everything<br />Things are getting better, but any experienced developer will tell you that SharePoint is woefully under-documented for developers<br />Your best bet is just to dig in and start working with it<br />

5.
Why am I giving this talk?<br />SharePoint is built on top of <br />ASP.Net 2.0 <br />SQL Server<br />Easy stuff, Right?<br />Yes…but! <br />There are many differences between working with ASP.Net and SharePoint!<br />One of the biggest differences is working with data stored in SharePoint.<br />There is no ADO.Net Provider for SharePoint<br />There is no (official) LINQ to SharePoint (yet!)<br />

6.
Why am I giving this talk?<br />Until the day Microsoft sees fit to provide us with simpler, easier to use tools, our best bet is to access data in SharePoint via <br />The SharePoint Object Model<br />CAML (more on this later)<br />There’s Web Services too, but that’s another talk<br />

7.
Lists, Lists and more Lists<br />SharePoint stores all of its data in a construct called a List<br />Conceptually, Lists are like Tables in a Database<br />Columns are referred to as Fields<br />Rows are referred to as ListItems<br />However, Lists have a lot more meta data<br />They are less flexible in some ways – some things we take for granted in a database (like joins, for example) are either difficult or impossible<br />

8.
Where do we begin? In Context! <br />You have to reference the SharePoint Libraries<br />To communicate with SharePoint data, you have to have the proper context<br />The SPContext object provides that for you<br />If your code is in a web part you can use the “current” context<br />The context object allows you to get the SPsite object (actually a “site collection”)<br />

9.
Sites have webs<br />The Site Collection has one or more webs, accessible through the AllWebs collection<br />The default web for the collection is accessed with the OpenWeb() method<br />Each web has a collection of lists of varying types (Custom Lists, Document Libraries, Picture Libraries, Task Lists, Announcements etc.)<br />Each List has 1-to-n Views<br />

14.
So, why do we need all this?<br />Second Reason:<br />If you want to write queries to retrieve list items that meet specific criteria, you have to use CAML<br />CAML queries use an XML syntax in place of SQL<br />The field list in your query requires the use of the internal field names and their SharePoint Types<br />There isn’t really a clean, simple, obvious place to find this information<br />

15.
CAML Humps<br />CAML, while useful, has some serious shortcomings:<br />There’s a limited number of conditions and functions<br />There’s a lot of details about fields you MUST know or you’ll spend a lot of time debugging:<br />Internal field names<br />SharePoint data types<br />Data formats for things like dates and lookup values<br />There’s no joins – you can get values from lookup fields but that’s it<br />CAML queries are a real pain to use in code – you can write your own helper classes or you can use some that others have written, but the ultimate result will always be a string<br />

16.
CAML Humps<br />You can write your query as SQL and convert it by hand using the method found here:<br />http://www.codeproject.com/KB/sharepoint/Sharepoint_CAML_query.aspx<br />Or you can save yourself a lot of work by using other tools<br />The U2U CAML Query Builder is VERY helpful<br />

18.
Summary<br />Learn and know the SharePoint data object model<br />You may also want to learn the web services<br />Familiarize yourself with CAML<br />Use the available tools, and when necessary, build your own<br />Learn to search the SharePoint blogs because the official documentation from Microsoft (even the SDK!) leaves much to be desired<br />With apologies to Nike, “Just do it”<br />