lunedì 22 agosto 2011

Linq To Object in sharepoint 2007

Il codice sarà sicuramente molto semplificato in Sharepoint 2010.
Un esempio di come usare Linq to Object per manipolare i dati di una lista:

// Get all the Site Collections (Urls) in the "departments" Managed Path
var sites = from site in SPContext.Current.Site.WebApplication.Sites.Names

where site.ToLower().StartsWith("departments")

orderby site

select site;



// A List that will contain all the non-expired rolled up announcements
List announcementsRollup = new List();



foreach (string siteName in sites)

{

using (SPSite spSite = new SPSite("http://intranet/" + siteName))

{

using (SPWeb web = spSite.RootWeb)

{
// Select the "top 2" announcements in each web
// where the announcement is not expired

var top2 =
(from all in
web.Lists["Announcements"].Items.GetDataTable().AsEnumerable()

orderby all.Field("Created") descending
where all.IsNull("Expires") == true ||
all.Field("Expires") > DateTime.Today

select all).Take(2);


// Add those 2 announcements to multi Site Collection rollup

foreach (DataRow announcement in top2)

announcementsRollup.Add(announcement);

}

}

}



// Sort all the announcments across all the sites by created date

var sorted = from s in announcementsRollup

orderby s.Field("Created") descending

select s;



// Print all the announcements

foreach (DataRow announcement in sorted)

this.Controls.Add(new LiteralControl(announcement["Title"].ToString() + "
"));


End.

Nessun commento:

Posta un commento