5

In an event receiver, Item Was Updated. I want to Compare the Moderation status on ListItems to perform some functionality.This is just some of the code.

SPList list = web.Lists["DocLib"];
SPListItem item = list.GetItemById(listItemID);

var moderationStatus = item["_ModerationStatus"] as string;

if(moderationStatus == SPModerationStatusType.Approve.ToString())
   //Do stuff

So I should be able to get the values from this to make the comparison in a later stage:

string beforeStatus = properties.BeforeProperties["_ModerationStatus"] as string;
string afterStatus = properties.AfterProperties["_ModerationStatus"] as string;

In Document Libraries we know that:

  • BeforeProperties = Original Value.
  • AfterProperties = Changed Value.

But beforeStatus and afterStatus is null.

Does someone have any tips of other ways to Compare the values? I have even tried to take get the value form "ItemUptading" to get the value before it updates.

4 Answers 4

4

Not sure if this is the cause of your problem but...

For documents, Before and After properties are guaranteed for post events, such as ItemUpdated, but Before properties are not available for post events on list items

Reference:http://msdn.microsoft.com/en-us/library/ms463479.aspx

If this is your scenario you may want to use properties.ListItem and use that to compare the values. This article may help you further... http://www.synergyonline.com/Blog/Lists/Posts/Post.aspx?ID=122

1
  • So the problem lies with the accessing of the parameter. I have to use the "vti_" named parameters for it to work for title, but i can't use the vti_doclibmodstat to access the ModerationStatus. Have any toughts? - title --> vti_title - _ModerationStatus --> vti_doclibmodstat Commented Oct 14, 2013 at 10:55
2

To get the functionality as with before and after properties and if() statments with the same result as the code does down bellow.

So to answer my question, one of the problems with Before and After properties with versioning is that it's not 100% complete. In some cases the before value (of moderationStatus) is null, when it before value is 2 or pending it's OK, but when it's 3 or draft the before value is null.. just one example.

This might help someone who's struggling with the same issue i had.

The Code:

if (properties.AfterProperties["vti_doclibmodstat"] != null)
        {
            var after = (SPModerationStatusType)int.Parse(properties.AfterProperties["vti_doclibmodstat"].ToString());
            switch (properties.ListItem.ModerationInformation.Status)
            {
                case SPModerationStatusType.Approved:
                    switch (after)
                    {
                        case SPModerationStatusType.Draft:
                            break;
                        case SPModerationStatusType.Pending:
                            break;
                        default:
                            break;
                    }
                    break;
                case SPModerationStatusType.Denied:
                    switch (after)
                    {
                        case SPModerationStatusType.Draft:
                            break;
                        case SPModerationStatusType.Pending:
                            break;
                        default:
                            break;
                    }
                    break;
                case SPModerationStatusType.Draft:
                    switch (after)
                    {
                        case SPModerationStatusType.Draft:
                            break;
                        case SPModerationStatusType.Pending:
                            break;
                        default:
                            break;
                    }
                    break;
                case SPModerationStatusType.Pending:
                    switch (after)
                    {
                        case SPModerationStatusType.Approved:
                            break;
                        case SPModerationStatusType.Denied:
                            break;
                        case SPModerationStatusType.Draft:
                            break;
                        case SPModerationStatusType.Pending:
                            //hej
                            break;
                        default:
                            break;
                    }
                    break;
                default:
                    break;
            }
        }
        base.ItemUpdating(properties);
1

I had similar but more specific case. Needed to do some stuff for the first (and only the first) time document was approved. Here is what I did. Hope it helps someone.

public override void ItemUpdating(SPItemEventProperties properties)
    {
        if (properties.AfterProperties["vti_doclibmodstat"] != null)
        {
            SPModerationStatusType after = (SPModerationStatusType)int.Parse(properties.AfterProperties["vti_doclibmodstat"].ToString());

            if (!properties.ListItem.HasPublishedVersion
                 && after == SPModerationStatusType.Approved)
            {
             ...
-1
  1. use internal names for columns to get the after properties in item updating event.
  2. to get before properties use properties.listitem

http://www.sharepoint-tips.com/2006/09/synchronous-add-list-event-itemadding.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.