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.RunWithElevatedPrivilegesmethod 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.

Advertisements

SharePoint 2010 Community Launch in Photos

Last Wednesday, May 12th was a special day. We had fun celebrating the Egyptian community launch of SharePoint 2010 and Office 2010. I have presented about “Visual Studio Tools for SharePoint 2010”, people were really excited to learn that they no longer need third-party tools for developing SharePoint Solutions. Finally Visual Studio 2010 is enough along with its great extensibility features!

I would like to extend a special thank-you to AvePoint and USPJA for sponsoring the event.

Here are some photos from the event:

13 14  13

16

 

 

USPJ Academy Launch May 17

<Cross Post from USPJA web site>

We are just about ready with the initial parts of the USPJ Academy platform. As such, on May 17, we’re launching the first features of the platform and invite students who have received and accepted a seat in the early access program to join us to begin learning SharePoint.

The first features launched are the library, the forums, and the faculty blog. These features are vital to understanding initial user needs for USPJA and will provide a rich environment in which students can prepare for their upcoming classes.

The official inauguration event happens on May 31, and classes begin on June 1.

</ Cross Post from USPJA Web Site >

For more information about USPJ Academy, see what SharePoint Tech Report wrote about it in April’s newsletter :

11

Drop by “Ask the Experts Panel” at SharePoint 2010 official launch by Microsoft Egypt

Tomorrow May 11th will be an interesting day, Microsoft will be hosting a launch event for Microsoft® Office 2010, Microsoft® SharePoint® 2010, Microsoft® SQL Server® 2008 R2 and Microsoft® Visual Studio® 2010 at Grand Hyatt Hotel – Laylaty Ballroom Time: 09:00 am – 05:00 pm

I will be there in the Ask the experts with my fellow SharePoint MVPs , Marwan Tarek and Shady Khorshied. If you have any SharePoint related questions, please drop by.

SharePoint 2010 Egyptian Community Launch: Transportation Updates

Since the announcement of the Egyptian SharePoint community launch, I have been receiving a lot of emails about the transportation details. Sorry for the delay, but we finally managed to get everything set.

There will be a pickup bus from Lebanon Square.
2:00 to 2:15 > Gathering in-front of Mo’men
2:15 to 3:15 > Moving to Microsoft Egypt
7:45 to 8:00 > gathering in-front of Microsoft
8:00 to 8:45 > back to Lebanon Square

Please if you have any problem regarding transportation, don’t hesitate to call Shady Khorshied on 010 19 11 216 .

I would like also to announce that AvePoint is sponsoring the event, stay tuned to their giveaways 😉

LINQ is Better Than ForEach

5/5/2010

LINQ is Better Than ForEach

Chris Eargle has done a great job comparing LINQ and ForEach loops from the performance and maintainability perspective.

Here is Chris’ conclusion: The vast majority of code I come across that either can be written in LINQ or refactored to LINQ has no noticeable, negative performance impact, but it has a positive impact on maintainability.

Click here to read the full post !