Paolo Pialorsi - Bridge The Gap!

Living in a Service Oriented World

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 :-) !

Posted: Jan 30 2005, 10:08 AM by paolopia | with 11 comment(s)
Filed under: ,

Comments

TrackBack said:

# January 29, 2005 10:10 PM

TrackBack said:

^_^,Pretty Good!
# April 10, 2005 12:02 AM

jat said:

thank you very much. This article very pretty.

# September 10, 2007 6:51 AM

Chris said:

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?)

# October 8, 2007 10:39 PM

Chris said:

Please answer me paolo?!

# October 9, 2007 2:18 AM

Lennin said:

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.

# February 15, 2008 6:13 AM

paolopia said:

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.

# February 20, 2008 2:45 AM

天使の泪 said:

weblogs.asp.net/.../363312.aspx

HandlingtransactionsworkingonWin...

# April 23, 2008 10:28 AM

Brajendu Kumar Das said:

The Roll back Transaction is not working for me.? IS there any other way...?Pls Suggest...

# May 7, 2008 4:52 AM

Sharepoint solutions » Transactions in Sharepoint 2007 said:

Pingback from  Sharepoint solutions » Transactions in Sharepoint 2007

# May 12, 2008 5:59 PM

Vikash said:

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

# February 5, 2010 7:08 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)