RSS Feed Feed your read!

Bookmark and Share







Tag Cloud

ASP.NET Generic, Best Practices, Business Intelligence, Freeware Releases, InfoPath, Infrastructure, jQuery, Lunch & Learn Events, Project Server, Random, Reporting Services, Search, SharePoint Administration, SharePoint Business Analysis and Project Management, SharePoint Development, Silverlight, Social Networking, Speaking Events, White Paper Releases, Workflow Foundation,

Archives

June 2007 (3)
August 2007 (1)
November 2007 (2)
February 2008 (2)
April 2008 (5)
May 2008 (7)
June 2008 (8)
July 2008 (7)
August 2008 (3)
September 2008 (7)
October 2008 (1)
November 2008 (3)
December 2008 (3)
January 2009 (7)
February 2009 (5)
March 2009 (10)
April 2009 (2)
May 2009 (6)
June 2009 (3)
July 2009 (4)
August 2009 (6)
September 2009 (3)
October 2009 (9)
November 2009 (10)
December 2009 (1)
January 2010 (1)
February 2010 (3)
March 2010 (6)
April 2010 (2)
May 2010 (3)
June 2010 (4)
July 2010 (3)

Simple LINQ to SharePoint Example that Rolls Up Announcements 

Tags:

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

 
Posted by BENDER\pwicklund on 21-Jul-09
0  Comment  |  Trackback Url  | 0  Link to this post | Bookmark this post with:        
 
Failed to render control: Value does not fall within the expected range.

Comments

Bookmark and Share

Note: Facebook no longer sends notifications for comments, so it may be a number of days before I see your post. For urgent matters, click "Contact Me" on the top nav. More info: Click Here.