Monday, November 26, 2012

Executing code with Full Control in SharePoint 2007

This post I thought to write for my future reference and for other people who are facing issue while executing code and getting issue with access.

My requirement was to implement Cascading drop down functionality in Datasheet mode. So, I thought to write Event Receiver feature in VS2008 for SharePoint 2007. Attach feature with List.

Note: Cascading drop down and attaching Event Receiver feature to list is not scope of this article.

I implemented the code with below methods:

public override void ItemAdded(SPItemEventProperties properties)
{}

public override void ItemUpdated(SPItemEventProperties properties)
{}

Hence, I tried running code with my account (Admin - full control). It ran successfully. It made me think twice when I tried executing this feature on another machine having Contributor rights. They were not able to execute this code at all. After few hrs of research, I got to know that we need to execute this code with full control, so I made below changes to my code.


public override void ItemAdded(SPItemEventProperties properties)
{

      SPSecurity.RunWithElevatedPrivileges(delegate()
       {
                      //Your code over here...
        });
}


public override void ItemUpdated(SPItemEventProperties properties)
{

      SPSecurity.RunWithElevatedPrivileges(delegate()
       {
                      //Your code over here...
        });
}

RunWithElevatedPrivileges() will run below code with full control.

Hope this article have help you out.