Elevated Code In SharePoint Workflows

Lately, I have been immersed in SharePoint WorkFlows, which in itself is not really a bad thing, however I am finding my own quirks as I run through it. And by some I mean a lot. For example, I was looking for an elegant way to run my work flow code with elevated privileges where I could separate my SharePoint work flow code method and call it with the relevant privileges to execute.

In order to do this, run the work flow code as elevated within the overriden ActivityExecutionStatus execute method. I spaced this for a while, and man it was driving me nuts. In order to get to this to work, you need to setup your activity class to run elevated code appropriately with the relevant method.

Firstly, in your execute method you are going to have nothing really besides a reference to the secure code that you are going to be running. In other words, you are going to create a method that is considered the elevated code in a seperate method that is going to be passed into the execution method of the activity.

It will look like this:

  1. protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
  2. {
  3. this.executionContext = executionContext;
  4. SPSecurity.CodeToRunElevated myElevatedCode = new SPSecurity.CodeToRunElevated(this.secureElevatedCode);
  5. SPSecurity.RunWithElevatedPrivileges(myElevatedCode);
  6. return ActivityExecutionStatus.Closed;
  7. }

You can see that here we are using the SPSecurity.CodeToRunElevated Delegate and
SPSecurity.RunWithElevatedPrivileges Method which takes the secure code to be run as a parameter, which in this case equates to our major work flow method. Using this structure, you aren’t limited to building an impersonation.

Following, you build out the method that you want to pass in that equates to your secureElevatedCode method.

  1. private void myElevatedCode()
  2. {
  3. ...
  4. // Your elevated workflow code goes here
  5. ...
  6. }

This can contain an arbitrary set of functions. Within it you could start to establish your SharePoint references to consume within your workflow:

  1. using (SPSite site = new SPSite(this.YourWorkFlowSiteProperty))
  2. {
  3. using (SPWeb web = site.OpenWeb(this.YourWorkFlowWebProperty))
  4. {
  5.  
  6. ....
  7.  
  8. // Now that you are referenced, do your fun stuff here!
  9.  
  10. ....
  11.  
  12. }
  13.  
  14. }

and start to do some fun stuff if you so choose :-)

If you would like to read more about these methods, in the Microsoft Windows SharePoint Services 3.0 SDK there is an example of an event handler which allows users to iterate through sites and groups as their current identity that uses the same tactic. It might be helpful to read through it to solidfy your understanding of the concept if needed.

share save 171 16 Elevated Code In SharePoint Workflows

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>