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. 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. 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
9. Entities
BankAccount account = new BankAccount()
{
Number = “99100048169”,
Bank = “Deutsche Bank”
};
9
Content Type Entity Class
Column Attribute Member
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. 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. 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. 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. 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. 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
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
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
24. Performance Object Tracking Enabled
Optimizes the performancein read-only scenarios...
DataContext data = new DataContext(SPContext.Current.Web.Url);
data.ObjectTrackingEnabled = false;
24
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
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