Ideas for Free

Ideas for Free

only freedom gives SharePoint real power to grow

Ideas for Free RSS Feed
 

Posts tagged Lambda

Howto: Use lambda expression in SharePoint – Working with SPWeb

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 any Event list in site.

SPWeb spWeb = SPContext.Current.Web

var lists = spWeb.Lists.OfType<SPList>().Where(
             l => l.BaseTemplate == SPListTemplateType.Events);
  foreach (SPList list in lists)
{
       Console.WriteLine("{0} {1}", list.BaseTemplate.ToString(), list.Title);

  }

B.  Find any list based on custom list definition with Type=20080

SPWeb spWeb = SPContext.Current.Web

var lists = spWeb.Lists.OfType<SPList>().Where(
             l => (int) l.BaseTemplate == 20080);
  foreach (SPList list in lists)
{
       Console.WriteLine("{0} {1}", list.BaseTemplate.ToString(), list.Title);

  }

C. Find any “Daily” alert in the site

SPWeb spWeb = SPContext.Current.Web

var alerts = spWeb.Alerts.OfType<SPAlert>().Where(
                                  a => a.AlertFrequency == SPAlertFrequency.Daily);

  foreach (SPAler alert in alerts)
{
      // Do something with alert

  }

D.  Find any user which are Domain group

SPWeb spWeb = SPContext.Current.Web

var users = spWeb.SiteUsers.OfType<SPUser>().Where(
                              u => u.IsDomainGroup );

  foreach (SPUser user in users)
{
      // Do something with alert

  }

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
}

Howto: Use lambda expression in SharePoint Object model

English

Lambda expression has been introduced since .NET framework 3.5, it is an anonymous function that can contain statement and expression. For more understanding on lambda expression you can read directly in MSDN page here. I will assume that you have read the topic and you can remember the lambda simply as:

(input parameters ) => operation

We will start with very basic operation of using lambda expression in SOM, and I hope you’ll find your path for more complex one. (note some code may not be efficient, for the clarity purpose)

A.  Start with OfType<TResult>() function to get IEnumerable that implement query pattern.

Example:

Originally to browse to all Site collection in WebApplication we will write,

SPWebApplication webApplication = SPContext.Current.Site.WebApplication;

SPSiteCollection allSPSite = webApplication.Sites;

foreach(SPSite spSite in allSPSite)

{

      // operation in the SPSite

}

the equivalent for our lambda starter is,

SPWebApplication webApplication = SPContext.Current.Site.WebApplication;

var allSPSite = webApplication.Sites.OfType<SPSite>();

foreach(SPSite spSite in allSPSite)

{

      // operation in the SPSite

}

B. Use lambda expression in the IEnumerable

Example :

You need to list all Site collection which uses STS site template.

Originally you will write,

SPWebApplication webApplication = SPContext.Current.Site.WebApplication;

SPSiteCollection allSPSite = webApplication.Sites;

foreach(SPSite spSite in allSPSite)

{

      if(spSite.RootWeb.WebTemplate.Equals(“STS”,StringComparison.InvariantCultureIgnoreCase))

     {

           // Do operation in selected site

     }

}

the equivalent using lambda is,

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

}

Ok, now I believe you will get the idea of how to use lambda expression in SOM collection. In next posting, I’ll cover directly to the sample usage of the lambda.

Computers Business Directory - BTS Local