0

I have the below code for a custom event receiver to fire when an item is either updating, updated and checked in. But when the item is published to a major version and the approval status changes to 'Approved' the receiver doesn't update the field 'Current Version'

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using System.Collections;

namespace FFVersionNumberEventReceiver.EventReceiver1
{
/// <summary>
/// List Item Events
/// </summary>
public class EventReceiver1 : SPItemEventReceiver
{
    /// <summary>
    /// An item is being checked in.
    /// </summary>
    /// 

    //public override void ItemCheckingIn(SPItemEventProperties properties)
    //{
    //    base.ItemCheckingIn(properties);
    //    // set version number

    //    properties.ListItem["Current Version"] = properties.AfterProperties["vti_sourcecontrolversion"].ToString().Replace("V", "");
    //    properties.ListItem.SystemUpdate();
    //}


    public override void ItemUpdating(SPItemEventProperties properties)
    {

        // code to set old status
        var status = properties.ListItem["Approval Status"];
        properties.AfterProperties["OldStatus"] = status;

        base.ItemUpdating(properties);

    }

    public override void ItemUpdated(SPItemEventProperties properties)
    {
        base.ItemUpdated(properties);

        var currentstatus = properties.ListItem["Approval Status"];
        var oldstatus = properties.ListItem["OldStatus"];

        using (DisabledEventsScope scope = new DisabledEventsScope())
        {
            SPListItem listItem = properties.ListItem;

            // only set approval date if it is newly set to approved status
            if (currentstatus != oldstatus && Int32.Parse(currentstatus.ToString()) == 0)
            {
                listItem["Approval Date"] = DateTime.Now.ToShortDateString();
            }

            // set version number
            properties.ListItem["Current Version"] = properties.AfterProperties["vti_sourcecontrolversion"].ToString().Replace("V", "");

            // update database without changing info
            listItem.SystemUpdate();

        }

    }
}
}
  • Please Check also the associated listtype for the eventreceiver. Macbeth You use a different listtype? – Sascha Jun 9 '14 at 18:26
  • The list type is correct as the other fields update when a item is edited or added. Only when the approval status changes that the Current Version number doesn't update. – Mike Blair Jun 10 '14 at 7:21
  • You know that when you make an Item.Update() in ItemUpdated, you call Item.Updating() again...and the loop continues – Gwny Jun 11 '14 at 12:49
  • No I didn't how do I stop the loop? – Mike Blair Jun 12 '14 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.