Skip Ribbon Commands Skip to main content

SharePoint Happenings

Help (new window)
Sign In
Navigate Up
Get Microsoft Silverlight
Install Silverlight plugin for a richer experience...
Blog Home |  Freeware |  Speaking |  About me

Simple LINQ to SharePoint Example that Rolls Up Announcements



Bookmark and Share

Top Tech Links










Top SharePoint Administrator Links









Top SharePoint Developer Links













NOTE: This blog has been moved to http://blog.philwicklund.com. If you'd like assistance, leave a comment on the copied post in the new location (hint, use search on the right to find the post).


LINQ to SharePoint is officially being supported with the 2010 release, but that doesn't mean a lot of people haven't already been using LINQ to access SharePoint data on the 2007 platform. In fact there's even a codeplex project out there: http://www.codeplex.com/LINQtoSharePoint. To be honest, I haven't tried that project out, but it looks promising… J

 

Even with straight LINQ to objects, you can do quite a bit against the SharePoint API. My little example demonstrates how to search a Web Application's Site Collections for all the Site Collections within a given managed path, and then roll-up the most recent two announcements in each of the root webs in each site collection. Thereafter, print those announcements on the page:

 

 

// 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
<DataRow> announcementsRollup = new List<DataRow>();

 

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<DateTime>("Created") descending
                where
all.IsNull("Expires") == true ||
                    all.Field<DateTime>("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<DateTime>("Created") descending

    select s;

 

// Print all the announcements

foreach (DataRow announcement in sorted)

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

 

 

I'm sure this code will be greatly simplified with the release of SharePoint 2010, but it is useful nevertheless for today's 2007 platform. What I'm hoping to see with 2010 is a where clause on the SPList object that you could use to test if the TemplateFeatureID is equal to the GUID of the list type you want to go after. Then, you wouldn't have to hard code "Announcements", but rather you'd just type the GUID of the feature/list you want to roll up. That would be handy…

 

Cheers!

 

Phil