Handling transactions working on Windows Sharepoint Services lists

One of the features I miss on WSS and SPS is the capability to change lists contents and configuration transactionally.When you work on complex scenarios often it should be usefull to change data on an "application/service/support database" and on WSS/SPS lists/contents, but the Object Model of Sharepoint doesn't give us a way to handle transactions. So you have to do it manually, with compensating transactions.

By the way WSS and SPS 2003 are based on Windows Server 2003, we know. With Windows Server 2003 we also have COM+ 1.5 that allows us to build Services Without Components. There're many blogs and articles out there, about SWC and Serviced Components, so if you don't know what they are ... just google it.

Here is a code snippet that creates a WSS list covered by a transaction, provided transparently by a Service Without Components:

using System;
using System.EnterpriseServices;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;

namespace WSSTransactional
{
 class ShowWSST
 {
  [STAThread]
  static void Main(string[] args)
  {
   // Prepare the SWC context in COM+ 1.5
   ServiceConfig sc = new ServiceConfig();
   sc.TrackingEnabled = true;
   sc.TrackingAppName = "Demo";
   sc.TrackingComponentName = "WSSTX Demo";
   sc.Binding = BindingOption.BindingToPoolThread;
   sc.Transaction = TransactionOption.RequiresNew;
   sc.IsolationLevel = TransactionIsolationLevel.ReadCommitted;

   // Enter the context
   ServiceDomain.Enter(sc);

   // Check if the transaction is available
   if (!ContextUtil.IsInTransaction)
    throw new Exception("Transaction failed!");

   try
   {
    SPSite site = new SPSite("http://wssdev2k3:8001/");
    SPWeb web = site.OpenWeb();

    Guid folderId = web.Lists.Add("My Agenda", String.Empty, SPListTemplateType.Events);
    SPList folderList = web.Lists[folderId];
    folderList.OnQuickLaunch = true;
    folderList.Title = "My Agenda";

    // Save the list configuration in WSS
    folderList.Update();
   }
   catch (Exception ex)
   {
    // In case of any error, rollback => SetAbort
    if (ContextUtil.IsInTransaction) ContextUtil.SetAbort();
    throw ex;
   }
  
   Console.WriteLine("Would you like to commit the operation (Y/N) ?");
   String result = Console.ReadLine();
   if (result.ToUpper().Trim() == "Y")
    ContextUtil.EnableCommit();
   else
    ContextUtil.SetAbort();
   
   TransactionStatus status = TransactionStatus.NoTransaction;
   if (ContextUtil.IsInTransaction)
    status = ServiceDomain.Leave();

   if ((status != TransactionStatus.Commited) && (status != TransactionStatus.NoTransaction) && (status != TransactionStatus.LocallyOk))
    throw new Exception("Operation failed! Transaction aborted!");
  }
 }
}

As you can see the code that works with WSS/SPS doesn't care of the SWC code. I think this is very usefull! Hope you agree :-) !

6 Comments

  • thank you very much. This article very pretty.

  • Hi,
    This does not work for me? - I'm assuming that i shouldn't actually be able to see a list created within the transaction until you've committed?

    i can?

    even if i abort the transaction the list remains? (shouldn't it get rolled back?)

  • Please answer me paolo?!

  • The rollback doesn't work!. ' Sharepoint OM does not enlist transactions in a coordinator like DTC' according to Wei Lu (Microsoft Support). and DTC is what makes COM+ transactions work.

  • x Chris and Lennin,
    this post is 2 years old and was applied to WSS2 e SharePoint 2003, not to MOSS2007 and WSS3 that are actually available.
    With the new SharePoint editions I rather suggest you both to give a chance to TransactionScope class. However in general it is better to avoid having transactional context wrapping a MOSS/WSS activity because the result is not guaranteed.

  • Sorry Sir, the Code isnt working . please proved some details about perquisite if any , or if u have any idea

Comments have been disabled for this content.