Your SlideShare is downloading. ×
CAML, REST and LINQ -Data Access Options inSharePoint 2010Rob Windsorrwindsor@allin.com@robwindsor       #spsvb
Sponsors
About Me•   Senior SharePoint Architect with Allin Consulting•   Technical Contributor to the Pluralsight On-Demand Librar...
Lists• The data storage mechanism in SharePoint    Virtually all content stored in lists• Provisioning engine handles lis...
Accessing and Updating List Items• Field values accessed via SPListItem indexer• Generally accessed by internal name• Weak...
Field Names• Fields have three names   Display name      The name displayed to the end user      Can be changed   Inte...
CAML Queries• Query list items using XML syntax• SPQuery   Query single list   Returns SPLitsItemCollection• SPSiteDataQ...
CAML Queriesvar query = new SPQuery();query.ViewFields =    "<FieldRef Name=Title />" +    "<FieldRef Name=UnitPrice />";q...
“All we can do is decide what to do with   the time tools that is are given us”  Image from The Lord of the Rings: The Fel...
Large List Throttling• Lists can store millions of items• Retrieval of items is throttled    Max 5,000 items    Max 20,0...
List Relationships
Joins in CAML Queries• SPQuery supports joins    SPSiteDataQuery does not• Joins can only be done on lookup columns• Key ...
SPQuery with Join Examplevar query = new SPQuery { RowLimit = 5 };query.ProjectedFields =    "<Field Name=CategoryTitle Ty...
Querying Multiple Lists with SPSiteDataQuery• Core properties the same as SPQuery• Cannot query lists outside site collect...
SPSiteDataQuery Examplevar query = new SPSiteDataQuery { RowLimit = 50 };query.ViewFields =    "<FieldRef Name=Title />" +...
DEMOList access using ServerObject Model
REST APIs• Generally used by remote applications• Uses Open Data (oData) Protocol    Also known as: Astoria, ADO.NET Data...
REST APIs• Urls map to SharePoint resources    Example: ListData.svc/Products(2)/Category maps to the category     for th...
REST APIs (oData)
Consuming Data via REST APIs
Consuming Data via REST APIsvar context = new DemoDataContext(new Uri(    SiteUrl + @"_vti_bin/ListData.svc"));context.Cre...
Consuming Data via REST APIsvar context = new DemoDataContext(new Uri(    "http://localhost/sites/demo/_vti_bin/ListData.s...
Consuming Data via REST APIsvar url = "http://localhost/sites/demo/_vti_bin/ListData.svc";var context = new DemoProxy.Demo...
DEMOUsing the REST APIs
LINQ to SharePoint• LINQ provider generates CAML for query    Requires reference to Microsoft.SharePoint.Linq.dll• Only a...
Consuming Data with LINQ to SharePointvar url = "http://localhost/sites/demo";var context = new SPNorthwind.SPNorthwindDat...
DEMOLINQ to SharePoint
Thank You• Big thanks to the organizers, sponsors and you for making  this event possible• Please fill out your evaluation...
Upcoming SlideShare
Loading in...5
×

Data Access Options in SharePoint 2010

1,331

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,331
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Data Access Options in SharePoint 2010"

  1. 1. CAML, REST and LINQ -Data Access Options inSharePoint 2010Rob Windsorrwindsor@allin.com@robwindsor #spsvb
  2. 2. Sponsors
  3. 3. About Me• Senior SharePoint Architect with Allin Consulting• Technical Contributor to the Pluralsight On-Demand Library• Microsoft MVP, MCPD, MCT• Founder and Past-President of the North Toronto .NET UG• Co-author of Prof. Visual Basic 2010 and .NET 4 (Wrox)
  4. 4. Lists• The data storage mechanism in SharePoint  Virtually all content stored in lists• Provisioning engine handles list creation and schema management• SharePoint provides interface to add, edit, delete and view items• Lists support:  Multiple views (for sorting, filtering, grouping)  Simple validation  Content approval  Item versioning• Data stored in rows and columns  More like Excel worksheets than database tables
  5. 5. Accessing and Updating List Items• Field values accessed via SPListItem indexer• Generally accessed by internal name• Weakly typedvar web = SPContext.Current.Web; var web = SPContext.Current.Web;var list = web.Lists.TryGetList("Products"); var list = web.Lists.TryGetList("Products");if (list == null) return; if (list == null) return;foreach (SPListItem item in list.Items) foreach (SPListItem item in list.Items){ { Console.WriteLine("{0} ({1})", var price = Convert.ToDouble( item["Title"], item["UnitPrice"]); item["UnitPrice"]); price *= 1.1;} item["UnitPrice"] = price; item.Update(); }
  6. 6. Field Names• Fields have three names  Display name  The name displayed to the end user  Can be changed  Internal name  Set when field is created  Does not change  Non-alphanumeric chars escaped (e.g. Unit_x0020_Price)  Find using Server Explorer  Static name  Used by custom field types
  7. 7. CAML Queries• Query list items using XML syntax• SPQuery  Query single list  Returns SPLitsItemCollection• SPSiteDataQuery  Query multiple lists across site collection  Returns DataSet
  8. 8. CAML Queriesvar query = new SPQuery();query.ViewFields = "<FieldRef Name=Title />" + "<FieldRef Name=UnitPrice />";query.Query = "<OrderBy>" + " <FieldRef Name=Title />" + "</OrderBy>" + "<Where>" + " <And>" + " <Gt>" + " <FieldRef Name=UnitsInStock />" + " <Value Type=Integer>0</Value>" + " </Gt>" + " <Eq>" + " <FieldRef Name=Category LookupId=True />" + " <Value Type=Lookup>1</Value>" + " </Eq>" + " </And>" + "</Where>";var items = list.GetItems(query);
  9. 9. “All we can do is decide what to do with the time tools that is are given us” Image from The Lord of the Rings: The Fellowship of the Ring by New Line Cinema
  10. 10. Large List Throttling• Lists can store millions of items• Retrieval of items is throttled  Max 5,000 items  Max 20,000 for Owners  No max for local admin• Can override limits in code  QueryThrottleMode• Can configure time window where limits are ignored  For reporting, list aggregation, etc  Done during off-peak hours
  11. 11. List Relationships
  12. 12. Joins in CAML Queries• SPQuery supports joins  SPSiteDataQuery does not• Joins can only be done on lookup columns• Key properties  Joins: Define joins to other lists  ProjectedFields: Fields being projected from the foreign list• Only inner and left outer joins are supported
  13. 13. SPQuery with Join Examplevar query = new SPQuery { RowLimit = 5 };query.ProjectedFields = "<Field Name=CategoryTitle Type=Lookup List=productCategory ShowField=Title />";query.ViewFields = "<FieldRef Name=Title />" + "<FieldRef Name=UnitPrice />" + "<FieldRef Name=UnitsInStock />" + "<FieldRef Name=CategoryTitle />";query.Query = "<Where>" + " <And>" + " <Gt>" + " <FieldRef Name=UnitsInStock />" + " <Value Type=Integer>0</Value>" + " </Gt>" + " <Eq>" + " <FieldRef Name=CategoryTitle />" + " <Value Type=Text>Condiments</Value>" + " </Eq>" + " </And>" + "</Where>";query.Joins = "<Join Type=INNER ListAlias=productCategory>" + " <Eq>" + " <FieldRef Name=Category RefType=ID />" + " <FieldRef List=productCategory Name=ID />" + " </Eq>" + "</Join>";return list.GetItems(query);
  14. 14. Querying Multiple Lists with SPSiteDataQuery• Core properties the same as SPQuery• Cannot query lists outside site collection• Key additional properties  Lists  Defines which lists will be included in query  Use ServerTemplate for OOB content types  Use BaseType plus filter for ID in where clause for custom content types  Webs  Defines the scope of the query  SiteCollection – Query all matching lists in site collection  Recursive: – Query all matching lists in current Web and it’s children
  15. 15. SPSiteDataQuery Examplevar query = new SPSiteDataQuery { RowLimit = 50 };query.ViewFields = "<FieldRef Name=Title />" + "<FieldRef Name=DueDate />" + "<FieldRef Name=AssignedTo />";query.Query = "<Where>" + " <And>" + " <Geq>" + " <FieldRef Name=DueDate />" + " <Value Type=DateTime><Today /></Value>" + " </Geq>" + " <Leq>" + " <FieldRef Name=DueDate />" + " <Value Type=DateTime><Today OffsetDays=5 /></Value>" + " </Leq>" + " </And>" + "</Where>";query.Lists = "<Lists ServerTemplate=107 />"; <!– Task Lists -->query.Webs = "<Webs Scope=SiteCollection />";return web.GetSiteData(query);
  16. 16. DEMOList access using ServerObject Model
  17. 17. REST APIs• Generally used by remote applications• Uses Open Data (oData) Protocol  Also known as: Astoria, ADO.NET Data Services, WCF Data Services  Data centric (REST) Web service  Data returned in AtomPub or JSON format  Metadata is available  Allows creation of service proxy  Strongly-typed access to data  Requests for collections may not return all items  Max 1,000 items returned per request  Need to check for continuation
  18. 18. REST APIs• Urls map to SharePoint resources  Example: ListData.svc/Products(2)/Category maps to the category for the product with id == 2• Protocol commands  $filter={simple predicate}  $expand={Entity}  $orderby={property}  $skip=n  $top=n  $metadata
  19. 19. REST APIs (oData)
  20. 20. Consuming Data via REST APIs
  21. 21. Consuming Data via REST APIsvar context = new DemoDataContext(new Uri( SiteUrl + @"_vti_bin/ListData.svc"));context.Credentials = CredentialCache.DefaultCredentials;var query = from detail in context.OrderDetails where detail.UnitPrice < 100 select detail;
  22. 22. Consuming Data via REST APIsvar context = new DemoDataContext(new Uri( "http://localhost/sites/demo/_vti_bin/ListData.svc"));context.Credentials = CredentialCache.DefaultCredentials;var query = from detail in context.OrderDetails where detail.UnitPrice < 100 select detail;var queryCount = query.Count();var details = new List<OrderDetailsItem>();var dsquery = query as DataServiceQuery<OrderDetailsItem>;var response = dsquery.Execute() as QueryOperationResponse<OrderDetailsItem>;details.AddRange(response);var token = response.GetContinuation();while (token != null){ response = context.Execute(token); details.AddRange(response); token = response.GetContinuation();}
  23. 23. Consuming Data via REST APIsvar url = "http://localhost/sites/demo/_vti_bin/ListData.svc";var context = new DemoProxy.DemoDataContext(new Uri(url));context.Credentials = System.Net.CredentialCache.DefaultCredentials;var products = from product in context.Products where product.Category.Title == "Condiments" && product.UnitsInStock > 0 select product;foreach (var product in products){ product.UnitsInStock += 1; context.UpdateObject(product); // don’t forget this}context.SaveChanges();
  24. 24. DEMOUsing the REST APIs
  25. 25. LINQ to SharePoint• LINQ provider generates CAML for query  Requires reference to Microsoft.SharePoint.Linq.dll• Only available to server object model• Queries are strongly-typed• Entities created using SPMetal command-line utility  Similar to SqlMetal in LINQ to SQL  Located in <System root>bin  Basic usage  spmetal /web:<site url> /code:<file name>  Code generation can be customized via parameters XML file  http://msdn.microsoft.com/en-us/library/ee535056.aspx
  26. 26. Consuming Data with LINQ to SharePointvar url = "http://localhost/sites/demo";var context = new SPNorthwind.SPNorthwindDataContext(url);// context.Log = Console.Out;var products = from product in context.Products where product.Category.Title == "Condiments" && product.UnitsInStock > 0 select product;foreach (var product in products){ product.UnitsInStock += 1;}context.SubmitChanges();
  27. 27. DEMOLINQ to SharePoint
  28. 28. Thank You• Big thanks to the organizers, sponsors and you for making this event possible• Please fill out your evaluation• Please keep in touch rwindsor@allin.com @robwindsor msmvps.com/blogs/windsor

×