About      |       Articles      |      Services      |      Software      |      Contact

Latest Free SharePoint Software

ARB Security Solutions regularly releases free SharePoint software, including WebParts, Client Applications, Framework Extensions, and other Miscellaneous Components.
The most recent freeware is:

Title: Simple SharePoint Rollup WebPart
Date Published: 10/22/2009

Previous Two Free WebPart Releases:

SecureCenter For SharePoint

By SharePoint security integrators, for SharePoint security integrators.

SharePoint Security Assurance Program™

For externally facing SharePoint deployments, security is an acutely important deployment concern. Learn how through daily security scanning, you can ensure external business users and partners that they can collaborate in confidence!

Security Assurance WebPart:



Take Advantage of Lambdas with RunWithElevatedPrivileges

375px Lambda lc.svg Take Advantage of Lambdas with RunWithElevatedPrivileges

One of the compelling features in C# 3.0 is the presentation of lambda expressions, cultivated out of the anonymous delegate (type-safe function pointers) support (don’t need to define a method somewhere else) that was present in C# 2.0. A lambda expression organizes with a left side composed of the arguments and a right expression which is the body of the method, split with a “=>”.

In the context of SPSecurity.RunWithElevatedPrivileges to encourage execution of a method with Full Control rights, it is common to see it as an anonymous method. For example, when using the SPUtility.SendEmail method it is ordinary to elevate rights. Using pre C# 3.0 mechanisms this would normally take on the form:

C#:
  1. SPSecurity.RunWithElevatedPrivileges(delegate
  2. {
  3. SPUtility.SendEmail(SPContext.Current.Site.OpenWeb(), false, false, "no_reply@sharepointsecurity.com", "this is my subject", "this is my body");
  4. });

This is a fair amount of real estate on the code surface. Wouldn’t it be nicer if we introduced a little bit of syntax sugar to tidy it up? This is where lambda expressions are accommodating. Taking the above method, this instead takes on the form:

C#:
  1. SPSecurity.RunWithElevatedPrivileges(() => SPUtility.SendEmail(SPContext.Current.Site.OpenWeb(), false, false, "no_reply@sharepointsecurity.com", "this is my subject", "this is my body"));

This is much cleaner, and required less typing!

  • Share/Bookmark

2 Comments »

  1. [...] Take Advantage of Lambdas with RunWithElevatedPrivileges [...]

    Pingback by Links (12/4/2008) « Steve Pietrek - Everything SharePoint — December 4, 2008 @ 8:02 pm

  2. And if your function doesn’t have parameters, then just:
    SPSecurity.RunWithElevatedPrivileges(SendEmail);

    Comment by bungle — December 5, 2008 @ 2:05 am

RSS feed for comments on this post. TrackBack URL

Leave a comment