I’ve been working on a “start up” which does product development, trying to implement what SharePoint does not offer in our country regarding no-paper policies.

Most of our operations involve a lot lists and libraries, the usual sharepoint approach did not work for us since it was impossible to undo all previous operations given an unhandled (or handled) exception occurred. So we decided to try a transactional approach.

Let’s do a bit of abstraction on a transactional operation, we find that there are two basic operations: do something and undo that which we did.

Lets be a little more elegant: lets call those two operations Execute() and Undo(). Let’s start then, with a simple interface:

image

With a little bit more on thought on the matter, we can separate transactions on two groups: a single operation or a set of operations. Since we are doing transactions on SharePoint, I propose atomic transactions to have and SPListItem and an SPWeb, and those which are a collection of operations to have additionally a collection of transactions.

The atomic transaction is pretty simple:

image

We have a couple of constructors and a couple of abstract operations to be implemented on the child classes, and the SPListItem and SPWeb objects to work with.

The transaction collection, on the other hand, is a little bit more complex:

image

Yes, there is a Stack object there to store the operations. Let suppose for a moment that you are executing a large batch of operations and something fails. You need to start undoing stuff beginning with the last operation and ending with the first operation. Yes we need a LIFO data structure, and the Stack provides this kind of behavior.

So now that we have our base classes, we can begin writing some transaction implementation for our base Sharepoint operations: create, update and delete.

Since there is NO WAY in this world to recover an Item after doing a SPListItem.Delete(), so instead I’m going to use Recycle(), which allows us to recover stuff from the SharePoint Recycle Bin.

After digging up a little bit how the recycle works we find that after recycling an element we get a Guid which identifies the element on the bin. So our delete command will have a Guid as a field so we can recover the element from the bin in case something fails…

image

The SPUpdate, and SPCreate commands are on the visual studio solution, which can be grabbed from here. The SPCreate is a nice sample of a CommandCollection, so remember to give a look to it.

Happy coding

About the author 

Jaime Alberto Jaramillo Zapata