Howto: Use lambda expression in SharePoint Object Model – Working with WebApplication

English

Before you continue reading this post, I hope you have read the basic task to start using lambda expression in SOM here.

A. Find existing job definition, named “Change Log”

SPWebApplication spWebApplication = SPContext.Current.Site.WebApplication;
var jobs = spWebApplication.JobDefinitions.Where(x => x.Title == "Change Log");
foreach(SPJobDefinition job in jobs)
               Console.WriteLine(job.Id);

B. Find existing custom job definition MyCustomJobDefinition, named “My Custom Job”

SPWebApplication spWebApplication = SPContext.Current.Site.WebApplication;
var jobs = spWebApplication.JobDefinitions.OfType<MyCustomJobDefinition>().Where(x => x.Title == "My Custom Job");
foreach(SPJobDefinition job in jobs)
               Console.WriteLine(job.Id);

C. Find site collection from “STS” template

SPWebApplication webApplication = SPContext.Current.Site.WebApplication;

var allSPSite = webApplication.Sites.OfType<SPSite>().Where(s => s.RootWeb.WebTemplate.Equals("STS", StringComparison.InvariantCultureIgnoreCase));

foreach(SPSite spSite in allSPSite)

{

      // operation in the SPSite

}

D. Find All RootWeb (of Site collection)

SPWebApplication spWebApplication = SPContext.Current.Site.webApplication;
var rootWebs= spWebApplication.Sites.OfType<SPSite>().Select ( s => s.RootWeb );
foreach (SPWeb spWeb in rootWebs)
{
   // Do your task here
}

E. Find if Site Collection with RootWeb contains list named “Sample List”

SPWebApplication spWebApplication = SPContext.Current.Site.webApplication;
var sites = spWebApplication.Sites.OfType<SPSite>().Where(
                    s => s.RootWeb.Lists.OfType<SPList>().Where(
                         l => l.Title.Equals("Sample List",StringComparison.InvariantCultureIgnoreCase)).Count() > 0);
foreach (SPSite spSite in sites)
{
   // Do your task here
}

F. Find if RootWeb which contains list named “Sample List”

SPWebApplication spWebApplication = SPContext.Current.Site.webApplication;

var rootWebs = spWebApplication.Sites.OfType<SPSite>().Select(s => s.RootWeb).Where(
                                 r => r.Lists.OfType<SPList>().Where(
                                     l => l.Title.Equals("Sample List",StringComparison.InvariantCultureIgnoreCase)).Count() > 0);

foreach (SPWeb spWeb in rootWebs)
{
   // Do your task here
}

Leave a Reply