Understand SPSecurity.RunWithElevatedPrivileges

Here is a Nice and simply explained article I’ve found while I was digging the blogs and posts for finding a solution to remove a ListItem from a List using another one’s credentials.


My Scenario

What i was faced was, i wanted to remove a row from the SPList using SPSecurity.RunWithElevatedPrivileges . My WebApplication was Claims Based, and i was able to perfectly remove an item from the list when i perform it using my own credentials. The problem occurred when another person tried to do the same operation using his credentials.

Here the problem was, whenever we take the SPWeb instance using  

SPWeb oWeb = SPContext.Current.Web;, then SPWeb instance still running under anonymous account only , because we are taking it through the current web context in which the current user is running under anonymous account (IUSR_MachineName). That was the reason that we got that “Access Denied” exception. We need to remember this point all time whenever we use RunWithElevatedPrivileges under the web context.

So what we need to that, we have to take the current context outside the SPSecurity.RunWithElevatedPrivileges block and then create a new instance of SPSite and SPWeb inside the that block which will run under application pool identity.

Solution Code:

 SPWeb oWeb1 = SPContext.Current.Web; // taking the current SPWeb context running under the anonymous account
                using (SPSite oSite = new SPSite(oWeb1.Site.Url))
                    // creating a new SPSite running under Application pool idenity
                    using (SPWeb oWeb = oSite.OpenWeb())
                      // Code Lines you need 

Good luck..!!

Why i choose Sharepoint

After several years of academic trainings, finally I started my career as an associate sharepoint engineer. From that moment I’ve just realized its time say goodbye to my loving dream – becoming an UI specialist. Why I selected sharepoint is still a question to me. At the beginning, for sure I did not know why. I realized, knowingly or unknowingly there were two huge influences that inspire me to select this remarkable place as my  playground.

As beginner something is clear in my mind; we shouldn’t reinvent the wheel unless it’s on the requirement document.  Except to that i know that,no one is perfect and nothing is perfect too. According to my understandings,i already defined sharepoint using the simplest possible way I guess.

SharePoint is a huge platform/a product as some people says. It offers software solutions for most of the business cases. And of course, most solutions are far from being perfect. But SharePoint provides basic functionalities that need to be customized (in most cases). It reminds me ‘design patterns’ that I leant during my academics. As a beginner,I am often getting frustrated because I always wanted to start developing from the scratch. It’s fair because I had no idea about how huge software projects were implemented. I hated modifying something and ended up with something. But the real meaning of customizing basic functionalities and delivering the best comes once  we started talking about money and time. At that point by implementing a standard solid platform for web-based applications development, SharePoint gives us the best enterprise content management experience and of course if you consider the perfectness of the product, you should compare SharePoint 07,10 and latest 13 releases, there drawbacks, limitations and features. it improves just like how MS Office did. Some where I saw this “I always compare Microsoft to Madonna. Not the best singer out there, but marketed extremely well.”