none
Detect an item published in ItemUpdating eventReceiver

    Question

  • Hi,
    wondering on the web on this subject, I haven't found a lot of reference on detecting when ItemUpdating is called.

    Finally I found a solution, even I would hardly call it a "solution", to understand when the Event Receiver ItemUpdating is called due to the publication of an item.

    properties.AfterProperties["vti_draftOwnerId"] != null &&
    properties.AfterProperties["vti_cachedcustomprops"] != null &&
    properties.AfterProperties["vti_replid"] == null &&
    properties.AfterProperties["vti_doclibmodstat"] != null)

    If the condition is satisfied, it means that the update is a publish.

    Now the question is: is there a better way to do it?

    Please help!!!

    Thanks,
    Mauro
    Tuesday, May 06, 2008 1:58 PM

Answers

  • you can check ModerationInformation in event handler to see if it is in published version or not. Approved is published. please check this as well. i have used it in ItemUpdated event to insert data to a Line of business application on publishing of a page. You can check it in ItemUpdating if it has the correct value as per your need otherwise use ItemUpdated in this case.

     

    properties.ListItem.ModerationInformation==SPModerationStatusType.Approved

    Monday, May 12, 2008 7:12 PM
  • Hi all,
    Testing the solution once more, I figured out that it was still faulty in case the user rejects the submit for approval.
    Here's the snippet that should do the trick

    properties.AfterProperties["vti_draftOwnerId"] != null
    && properties.AfterProperties["vti_cachedcustomprops"] != null
    && properties.AfterProperties["vti_replid"] != null
    && properties.AfterProperties["vti_doclibmodstat"] != null
    && properties.AfterProperties["vti_doclibmodstat"] == 0
    && properties.AfterProperties["vti_sourcecontrolcheckedoutby"] == null
    && properties.BeforeProperties["vti_sourcecontrolcheckedoutby"] == null

    If you find any mistake\you gout a better idea, please tell...

    Mauro
    Wednesday, May 21, 2008 4:38 PM

All replies

  • Please help Sad

    Mauro
    Monday, May 12, 2008 2:50 PM
  • you can check ModerationInformation in event handler to see if it is in published version or not. Approved is published. please check this as well. i have used it in ItemUpdated event to insert data to a Line of business application on publishing of a page. You can check it in ItemUpdating if it has the correct value as per your need otherwise use ItemUpdated in this case.

     

    properties.ListItem.ModerationInformation==SPModerationStatusType.Approved

    Monday, May 12, 2008 7:12 PM
  • Hi,
    in fact ModerationInformation should be the right thing to check but unfortunately it is meaningless in ItemUpdatING handler.
    Checking it in Updated will not be able to cancel the pubishing event in case the logic behind triggers it.
    Do you think there is another solution to this problem?

    Mauro
    Thursday, May 15, 2008 4:46 PM
  • Usually the requirement is if an item is published then send certain things to other system or sharepoint site etc. so you can acheive it in ItemUpdated event. What you get in ItemUpdating in ModerationInformation?

     

    Thursday, May 15, 2008 7:23 PM
  • Moderation Status is set to 'Pending'
    Ther requirement in this workflow is to cancel the publish event  if certains conditions (presence of a folder, files, mandatory fields) are not fulfilled.
    Friday, May 16, 2008 7:10 AM
  • Hi all,
    Testing the solution once more, I figured out that it was still faulty in case the user rejects the submit for approval.
    Here's the snippet that should do the trick

    properties.AfterProperties["vti_draftOwnerId"] != null
    && properties.AfterProperties["vti_cachedcustomprops"] != null
    && properties.AfterProperties["vti_replid"] != null
    && properties.AfterProperties["vti_doclibmodstat"] != null
    && properties.AfterProperties["vti_doclibmodstat"] == 0
    && properties.AfterProperties["vti_sourcecontrolcheckedoutby"] == null
    && properties.BeforeProperties["vti_sourcecontrolcheckedoutby"] == null

    If you find any mistake\you gout a better idea, please tell...

    Mauro
    Wednesday, May 21, 2008 4:38 PM
  • Hi,
    Have you found a solution ?

    Regards,
    eric
    • Proposed as answer by behin_thierry Thursday, March 11, 2010 10:31 AM
    Wednesday, August 19, 2009 11:25 AM
  • ok

    I have to convert a docx to pdf when the item is approved, but if the conversion failed the approved must fail too,
    and with your solution that works, I just change it a little :

    public override void ItemUpdating(SPItemEventProperties properties)
            {
                base.ItemUpdating(properties);
                int resu = -1;
                if (properties.AfterProperties["vti_draftOwnerId"] != null
                    && properties.AfterProperties["vti_replid"] != null
                    && properties.AfterProperties["vti_doclibmodstat"] != null
                    && Int32.TryParse(properties.AfterProperties["vti_doclibmodstat"].ToString(), out resu)
                    && resu == 0
                    && properties.AfterProperties["vti_sourcecontrolcheckedoutby"] == null
                    && properties.BeforeProperties["vti_sourcecontrolcheckedoutby"] == null)
                {
                    properties.ErrorMessage = "You approved the item";
                    properties.Cancel = true;
                }
                else
                {
                    properties.ErrorMessage = "You didn't approve the item";
                    properties.Cancel = true;
                }
            }
    • Proposed as answer by behin_thierry Thursday, March 11, 2010 10:39 AM
    Thursday, March 11, 2010 10:38 AM
  • If you are trying to detect an approval event in ItemUpdating I would do something like :

    var moderationStatus = (SPModerationStatusType) properties.AfterProperties["vti_doclibmodstat"];

    if (moderationStatus == SPModerationStatusType.Approved)
    {
        properties.Cancel = true;
        properties.ErrorMessage = "STOP!!!";
    }

    If you are tring to detect if an item has been approved. I would move the check to ItemUpdated and do a :

    if (properties.ListItem.ModerationInformation.Status == SPModerationStatusType.Approved)


    Industrial inspection cameras | Speed enforcement | Line scan cameras | Machine vision cameras

    Friday, March 23, 2012 10:29 AM
  • Instead of using ItemUpdating, try using ItemUpdated. In that slightly later event you can just check if:

    properties.ListItem.ModerationInformation==SPModerationStatusType.Approved

    This thread is a little old but led me to a solution to a similar issue in SP2010.

    • Proposed as answer by James Hodges Friday, July 20, 2012 5:07 PM
    Friday, July 20, 2012 5:07 PM