Skip to main content
Go Search

Ayman El-Hattab's Blog [MVP]

Home
Ayman El-Hattab's Blog
Marwan Tarek's Blog
Mohamed Yehia's Blog
  

SharePoint 4 Arabs - Online SharePoint Training, Video Tutorials and Webcasts in Arabic > Ayman El-Hattab's Blog [MVP] > Posts > Using Lambda Expressions with RunWithElevatedPrivileges
Using Lambda Expressions with RunWithElevatedPrivileges

Elevation of privilege, a feature that was added in WSS 3.0 & MOSS 2007, enables you to programmatically perform actions in code by using an increased level of privilege. The SPSecurity.RunWithElevatedPrivileges method enables you to supply a delegate that runs a subset of code in the context of an account with higher privileges than the current user.

For some reason, I have never seen a code snippet that uses a lambda expression instead of the anonymous method syntax so I thought of showing you how to save some typing by using Lambda.

Commonly used syntax:

SPSecurity.RunWithElevatedPrivileges(delegate()
{     // Do things by assuming the permission of the "system account".
});

 

Much easier syntax:

SPSecurity.RunWithElevatedPrivileges(()=>
{     // Do things by assuming the permission of the "system account".
});

When I asked the following question on Twitter : “Why do people still use the anonymous method syntax rather than Lambda Expressions with SPSecurity.RunWithElevatedPrivileges() ? #SharePoint”

I got the following answer from Todd Bleeker : “Delegate syntax has been around a lot longer. Lambda is still confusing and even scary for most. <Todd />”

I totally agree with Todd, but guys if you are not comfortable with LINQ and lambda expressions, you should familiarize yourself with them before diving deep into developing SharePoint 2010 solutions.

As you all might know, Now in SharePoint 2010 you can use LINQ syntax to retrieve items from your lists instead of using CAML queries. Microsoft has included a LINQ provider for SharePoint in the Microsoft.SharePoint.Linq namespace, some classes in this namespace are responsible for translating the LINQ Queries to CAML ones which are eventually translated into SQL. You can still write CAML Queries and fetch items in the old ways but LINQ can really make your life easier specially when it comes to joins.

 

Comments

Thanks

This is a good tip.

-- Mostafa Elzoghbi
at 5/19/2010 5:35 PM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Body *


Attachments