Freigeben über


After Properties go blank in ItemUpdated event for Office 2007 Documents..

Event Handlers in MOSS have come a long way since their inception in SharePoint 2003. In MOSS, event handlers expose a pair of properties which can be used for easing quite complex functionality. Yes, I am talking about the pair – AfterProperties and BeforeProperties. I am sure you would be knowing the basic functionality of these properties. You can use the AfterProperties to get values which are set after the evnt executes and BeforeProperties to get the values before the event executes. And I also know that the web is full of articles which talk about bugs related to these pairs.

I am not going to go into the known issues with the Pair of properties. (I am saving it for another blog post)

So lets come to the main issue. I had a requirement where we wanted to set default values of custom columns of a list. So the normal practice to achieve such functionality is to set the default values in itemAdding event of the fields using the afterProperties property. (Pretty Normal stuff, Right) This works like a breeze for all type of documents, but strangely the default values were not being set for the Office 2007 documents. On debugging I found that AfterProperties were getting blank between the ItemUpdating and ItemUpdated event. In case you change the values of the fields from the UI, it worked. But for the default values these AfterProperties were getting blank and hence no value was being saved.

On deeper analysis, I found that the Office 2007 document parser is to be blamed here. The document parser for Office 2007 allows the default value to be set, only if you declare the default value at the time of column creation. (Either through UI or OM code)

So the possible workarounds for this problem are:-

  1. Set the default value at the time of field creation:

This can be achieved from UI or through code. I am writing a sample code for a multichoice field:

<Code>

 

SPFieldMultiChoice choice = lib.Fields[choiceField] as SPFieldMultiChoice;

 

            choice.Choices.Add("North");

            choice.Choices.Add("South");

            choice.Choices.Add("East");

            choice.Choices.Add("West");

            choice.DefaultValue = ";#North;#West;#";

            choice.Update();

 

</Code>

Pros

Cleaner approach, Works for all types of docs

Cons

If you already have a list with a lot of documents, this can be used only for future items

  1. Set the default value from the ItemUpdated event.

As I specified above the AfterValues are being cleared just before the itemUpdated event, so why not reset the values in ItemUpdated event. The following code can be used for reference.

<Code>

public override void ItemUpdated(SPItemEventProperties properties)

            {

                  base.ItemUpdated(properties);

 

                  if (properties.ListItem["Region"] == null)

                  {

                        properties.ListItem["Region"] = ";#North;#West;#";

                        this.DisableEventFiring();

                        properties.ListItem.Update();

                        this.EnableEventFiring();

                  }

 

            }

 

     </Code>

Pros:

Works well for all situations

Cons:

Works erratically for lists with checkIn and checkout facility.