Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

LINQ to SharePoint

499 views

Published on

Webinar presented for European SharePoint Community on 14-06-2011, about LINQ to SharePoint, including common pitfalls and performance.

Published in: Software
  • Be the first to comment

  • Be the first to like this

LINQ to SharePoint

  1. 1. LINQ to SharePoint André Vala andre.vala@create.pt
  2. 2. Agenda • LINQ? • LINQ to SharePoint? • Developing with LINQ • Common Pitfalls • Performance • Conclusion 2
  3. 3. LINQ? 3 LINQ PROVIDER DATA SOURCEAPPLICATION B C A .NET .NET QUERY DATA Language Integrated Query
  4. 4. LINQ to SQL using System.Data.Linq; DataContext db = new DataContext(@”mydatabase.mdf”); Table<BankAccount> BankAccounts = db.GetTable<BankAccount>(); var query = from account in BankAccounts where account.Bank == “Deutsche Bank” select account; foreach (BankAccount account in query) { Console.WriteLine(“Number: {0}”, account.Number); } 4
  5. 5. SHAREPOINT LIST ITEMS CAML LINQ to SharePoint? 5 LINQ PROVIDER DATA SOURCEAPPLICATION B C A .NET .NET QUERY DATA SHAREPOINT LINQ PROVIDER Microsoft.SharePoint.Linq.dll
  6. 6. LINQ to SharePoint using Microsoft.SharePoint.Linq; DataContext data = new DataContext(SPContext.Current.Web.Url); EntityList<BankAccount> BankAccounts = data.GetList<BankAccount>(“BankAccounts”); var query = from account in BankAccounts where account.Bank == “Deutsche Bank” select account; foreach (BankAccount account in query) { Console.WriteLine(“Number: {0}”, account.Number); } 6
  7. 7. DEMO HELLO LINQ! 7
  8. 8. Developing with LINQ • Entities • Add Items • Delete Items • Recycle Items • Update Items • Logging 8
  9. 9. Entities BankAccount account = new BankAccount() { Number = “99100048169”, Bank = “Deutsche Bank” }; 9 Content Type Entity Class Column Attribute Member
  10. 10. Entities Manual Definition [ContentType(Name=“BankAccount”, Id=“0x0104”)] public partial class BankAccount { [Column(Name=“Number”, FieldType=“Text”)] public string Number { get; set; } [Column(Name=“Bank”, FieldType=“Text”)] public string Bank { get; set; } } 10
  11. 11. Entities Automatic Generation SPMetal Tool to generate entities for a set of content types / lists Where is it? C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14BIN How is it used? SPMetal /web:http://myserver/myweb /code:BankAccounts.cs /namespace:MyApp.SharePoint.Data 11
  12. 12. Add Items using Microsoft.SharePoint.Linq; DataContext data = new DataContext(SPContext.Current.Web.Url); BankAccount account = new BankAccount() { Number = “99100048169”, Bank = “Deutsche Bank” }; data.BankAccounts.InsertOnSubmit(account); data.SubmitChanges(); 12
  13. 13. Delete Items using Microsoft.SharePoint.Linq; DataContext data = new DataContext(SPContext.Current.Web.Url); foreach (BankAccount account in data.BankAccounts) { if (account.Bank == “Deutsche Bank”) data.BankAccounts.DeleteOnSubmit(account); } data.SubmitChanges(); 13
  14. 14. Recycle Items using Microsoft.SharePoint.Linq; DataContext data = new DataContext(SPContext.Current.Web.Url); foreach (BankAccount account in data.BankAccounts) { if (account.Bank == “Deutsche Bank”) data.BankAccounts.RecycleOnSubmit(account); } data.SubmitChanges(); 14
  15. 15. Update Items using Microsoft.SharePoint.Linq; DataContext data = new DataContext(SPContext.Current.Web.Url); foreach (BankAccount account in data.BankAccounts) { if (account.Bank == “Deutsche Bank”) account.Bank = “Bank of America”; } data.SubmitChanges(); 15
  16. 16. Logging using Microsoft.SharePoint.Linq; DataContext data = new DataContext(SPContext.Current.Web.Url); StringWriter writer = new StringWriter(); data.Log = writer; // LINQ queries (...) string caml = writer.ToString(); 16
  17. 17. DEMO LINQING 17
  18. 18. Common Pitfalls • Anonymous Access • Display Names 18
  19. 19. Pitfall Anonymous Access • LINQ to SharePoint has no support for anonymous access • There is a workaround but has disadvantages: – Large performance impact – Uses RunWithElevatedPrivileges • Solution: go back to CAML... 19
  20. 20. DEMO ANONYMOUS ACCESS 20
  21. 21. Pitfall Display Names • SPMetal uses column Display Names to generate class members • What happens when the column names have special characters? Weird things happen… • How can we solve it? 21
  22. 22. DEMO DISPLAY NAMES 22
  23. 23. Performance • Object Tracking Enabled • LINQ vs CAML 23
  24. 24. Performance Object Tracking Enabled Optimizes the performancein read-only scenarios... DataContext data = new DataContext(SPContext.Current.Web.Url); data.ObjectTrackingEnabled = false; 24
  25. 25. Performance LINQ vs CAML • The LINQ to CAML translation is performed in real time • There is a performance penalty • The generated query can be inefficient • Not all the operations offered with LINQ are supported in CAML... 25
  26. 26. DEMO PERFORMANCE 26
  27. 27. Conclusion LINQ to SharePoint is really simple to use but not the solution when... • The code must run on the client • The solution must support anonymous access • Performance is paramount • There are lookup columns that refer to lists in other web sites of the same site collection 27
  28. 28. Thank You André Vala andre.vala@create.pt

×