Spug pt linqtosharepoint
-
Upload
comunidade-portuguesa-de-sharepoiint -
Category
Technology
-
view
494 -
download
0
description
Transcript of Spug pt linqtosharepoint
![Page 1: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/1.jpg)
Comunidade Portuguesa de SharePoint
10ª Reunião Presencial
16/04/2011
![Page 2: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/2.jpg)
Thank you for sponsoring!
![Page 3: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/3.jpg)
Agenda
09:30 Recepção10:00 Abertura10:30 Linq to SharePoint com André Vala 11:30 Coffee-break 11:45 Painel de Discussão 13:00 Final da Reunião & Almoço
![Page 4: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/4.jpg)
LINQ to SharePoint
![Page 5: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/5.jpg)
LINQ to SharePoint
LINQ?
LINQ to SharePoint?
Desenvolvimento com LINQ
Common Pitfalls
Performance
Cenários de uso
DEMO
DEMO
DEMODEMO
DEMO
![Page 6: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/6.jpg)
LINQ?
LINQ = Language Integrated Query• LINQ to SQL• LINQ to Objects• LINQ to XML• LINQ to ?
DadosLINQ Provider
APP
.NET Query
.NET Dados
![Page 7: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/7.jpg)
LINQ Data Context• Ponto de entrada para o LINQ• Permite obter e actualizar informação na fonte de
dados• Traduz queries sobre objectos em queries na
linguage nativa da fonte de dados, e constrói objectos a partir dos resultados provenientes da fonte de dados
DadosLINQ Provider
APP
.NET Query
.NET DadosDat
a Co
ntex
t
![Page 8: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/8.jpg)
LINQ to SQLusing System.Data.Linq;DataContext db = new DataContext(@”mydatabase.mdf”);
Table<BankAccount> BankAccounts = db.GetTable<BankAccount>();
var query =from account in BankAccountswhere account.Bank == “Millennium BCP”select account;
foreach (BankAccount account in query){
Console.WriteLine(“Number: {0}”, account.Number);}
![Page 9: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/9.jpg)
LINQ to SharePoint?
• Utiliza um LINQ to SharePoint provider– Microsoft.SharePoint.Linq.dll– Transforma .NET em CAML– Transforma List Items em .NET
SharePointLINQ to SharePoint
APP
.NET CAML
.NET List Items
![Page 10: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/10.jpg)
LINQ to SharePointusing Microsoft.SharePoint.Linq;DataContext data = new DataContext(SPContext.Current.Web.Url);
EntityList<BankAccount> BankAccounts = data.GetList<BankAccount>(“BankAccounts”);
var query =from account in BankAccountswhere account.Bank == “Millennium BCP”select account;
foreach (BankAccount account in query){
Console.WriteLine(“Number: {0}”, account.Number);}
![Page 11: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/11.jpg)
DEMO: Hello LINQ!
![Page 12: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/12.jpg)
Desenvolvimento com LINQ
• Entidades• Adicionar Itens• Eliminar Itens• Reciclar Itens• Actualizar Itens• Logging
![Page 13: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/13.jpg)
Entidades
• Content Type Entidade Classe• Coluna Atributo Membro
BankAccount account = new BankAccount(){
Number = “99100048169”,Bank = “Millennium BCP”
};
![Page 14: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/14.jpg)
Entidades Definição Manual[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; }
}
![Page 15: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/15.jpg)
Entidades Geração Automática
SPMetalFerramenta para gerar as entidades para um conjunto de content types / listas
Onde está?C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN
Como se usa?SPMetal
/web:http://createbox /code:BankAccounts.cs/namespace:MyApp.SharePoint.Data
![Page 16: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/16.jpg)
Adicionar Itensusing Microsoft.SharePoint.Linq;DataContext data = new DataContext(SPContext.Current.Web.Url);
BankAccount account = new BankAccount(){
Number = “99100048169”,Bank = “Millennium BCP”
};
data.BankAccounts.InsertOnSubmit(account);
data.SubmitChanges();
![Page 17: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/17.jpg)
Eliminar Itensusing Microsoft.SharePoint.Linq;DataContext data = new DataContext(SPContext.Current.Web.Url);
foreach (BankAccount account in data.BankAccounts){
if (account.Bank == “Millennium BCP”)data.BankAccounts.DeleteOnSubmit(account);
}
data.SubmitChanges();
![Page 18: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/18.jpg)
Reciclar Itensusing Microsoft.SharePoint.Linq;DataContext data = new DataContext(SPContext.Current.Web.Url);
foreach (BankAccount account in data.BankAccounts){
if (account.Bank == “Millennium BCP”)data.BankAccounts.RecycleOnSubmit(account);
}
data.SubmitChanges();
![Page 19: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/19.jpg)
Actualizar Itensusing Microsoft.SharePoint.Linq;DataContext data = new DataContext(SPContext.Current.Web.Url);
foreach (BankAccount account in data.BankAccounts){
if (account.Bank == “Millennium BCP”)account.Bank = “BES”;
}
data.SubmitChanges();
![Page 20: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/20.jpg)
Loggingusing Microsoft.SharePoint.Linq;DataContext data = new DataContext(SPContext.Current.Web.Url);
StringWriter writer = new StringWriter();data.Log = writer;
// queries LINQ(...)
string caml = writer.ToString();
![Page 21: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/21.jpg)
DEMO: LINQing
![Page 22: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/22.jpg)
Common Pitfalls
• To Dispose or Not to Dispose• Acesso anónimo• Display Names
![Page 23: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/23.jpg)
Pitfall To Dispose or Not to DisposeDataContext data = new DataContext(“http://createbox”);
Dispose? SIM
DataContext data = new DataContext(SPContext.Current.Web.Url);
Dispose? NÃO
string webUrl = SPContext.Current.Web.Site.RootWeb.Url;DataContext data = new DataContext(webUrl);
Dispose? SIM
1
2
3
![Page 24: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/24.jpg)
Pitfall To Dispose or Not to Disposeusing (DataContext data = new DataContext(“http://createbox”)){
(...)}
DataContext data = new DataContext(SPContext.Current.Web.Url);(...)
string webUrl = SPContext.Current.Web.Site.RootWeb.Url;using (DataContext data = new DataContext(webUrl)){
(...)}
1
2
3
![Page 25: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/25.jpg)
Pitfall Acesso Anónimo
• LINQ to SharePoint não suporta acesso anónimo
• Há um workaround mas tem desvantagens:– Grande impacto no desempenho– Utiliza RunWithElevatedPrivileges
• Solução: regressar ao CAML...
![Page 26: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/26.jpg)
DEMO: Acesso Anónimo
![Page 27: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/27.jpg)
Pitfall Display Names
• SPMetal utiliza o Display Name das colunas para gerar os membros das classes
• O que acontece quando os nomes das colunas têm caracteres especiais? POIS...
• E como é que isso se resolve?...
![Page 28: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/28.jpg)
DEMO: Display Names
![Page 29: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/29.jpg)
Performance
• Object Tracking Enabled• LINQ vs CAML
![Page 30: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/30.jpg)
Performance Object Tracking Enabled
Permite optimizar o desempenho em acessos read-only...
DataContext data = new DataContext(SPContext.Current.Web.Url);data.ObjectTrackingEnabled = false;
![Page 31: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/31.jpg)
Performance LINQ vs CAML
• A tradução LINQ para CAML é feita em tempo real
• Existe uma penalização no desempenho• A query gerada pode ser ineficiente• Nem todas operações definidas em LINQ são
suportadas em CAML...
![Page 32: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/32.jpg)
DEMO: Performance
![Page 33: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/33.jpg)
Cenários de Uso
• Muito simples de usar mas não é a solução quando...– Código tem que correr no cliente– A solução tem que suportar acesso anónimo– O desempenho é crítico– Existem colunas lookup que se referem a listas
noutros sites da mesma site collection
![Page 34: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/34.jpg)
Referências
MSDN: Using LINQ to SharePointhttp://msdn.microsoft.com/en-us/library/ee535491.aspx
Vídeos no Channel 9http://channel9.msdn.com/Learn/Courses/SharePoint2010Developer/LINQtoSharePointUnit
![Page 35: Spug pt linqtosharepoint](https://reader036.fdocumentos.tips/reader036/viewer/2022081519/554f78f9b4c9052a518b4920/html5/thumbnails/35.jpg)
Comunidade Portuguesa de SharePoint
10ª Reunião Presencial
16/04/2011