locked
Item Update, Document Status. RRS feed

  • Question

  • Hi all

    i am debugging item update event of a file.

    It got fired when i upload a document in the document Library or i change some properties of a document.

    i want to add some custom logic in this item update event and that logic depends upon the file status, if document is uploaded then some piece of code runs and if document is updated then some different piece if code runs.

     

    How can i get that the file is updated or uploaded?

    Tuesday, November 2, 2010 10:09 AM

Answers

  • Yes. try something like

     

     

            private bool NewItemAdded = false;

            private enum DocumentAddStatus {Adding = 0, Editing = 1};

            private DocumentAddStatus CurrentDocStatus ;

            public override void ItemAttachmentAdding(SPItemEventProperties properties)

            {

                base.ItemAttachmentAdding(properties);

                NewItemAdded = true;

                CurrentDocStatus = DocumentAddStatus.Adding;

            }

            public override void ItemUpdating(SPItemEventProperties properties)

            {

                base.ItemUpdating(properties);

                if (NewItemAdded == true)

                {

                    CurrentDocStatus = DocumentAddStatus.Adding;

                    NewItemAdded = false;

                }

                else

                {

                    CurrentDocStatus = DocumentAddStatus.Editing;

                }

            }

            public override void ItemUpdated(SPItemEventProperties properties)

            {

                if (CurrentDocStatus == DocumentAddStatus.Editing)

                {

    //your update code goes here.

                }           

     

            }


     

    • Marked as answer by Shafaqat Tuesday, November 2, 2010 2:01 PM
    • Edited by Mike Walsh FIN Wednesday, November 3, 2010 11:04 AM sig removed - don't ask people to mark your answers
    Tuesday, November 2, 2010 1:57 PM

All replies

  • Use the ItemUpdated Enent in the ItemEventListener Class. That fires every time an event is updated. Catch is it will also fire when the Item is added. to you will have to block the event like the following.

     

            this.DisableEventFiring();

    //--- Your code. Here

            this.EnableEventFiring();

     

     

    • Edited by Mike Walsh FIN Wednesday, November 3, 2010 11:05 AM sig removed - don't ask people to mark your answers
    Tuesday, November 2, 2010 1:06 PM
  • thanks for your help.

    You did not get me i think,

    i have uploaded a file and this will fire item update.

    i have updated a file and this will also fire item update.

    now in item updated event handler how would i get to know that this event was fired due to file upload or file update?

    if document status==uploaded

            then do this

    if document status==updated

            then do this

     

    i want to get document status, whether updated or uploaded.

     

     

    Tuesday, November 2, 2010 1:14 PM
  • ItemAdded Event will fire first and then ItemUpdated will fire for for all adds based on some conditions. in some cases only ItemAdded will fire.

    Only ItemUpdated will fire for all Updates. you can set up a boolean flag to see wether its an add or update. I think the best place to set up your flag will be ItemAdding whicch fires just before an item is added and ItemUpdating which fires before the edit. these are Synchronous. so you can be sure of what is happenning.

    http://msdn.microsoft.com/en-us/library/ms477278(v=office.12).aspx

    has all the event for EventListener. 

     

    • Edited by Nagesh T Tuesday, November 2, 2010 1:41 PM Added more information.
    • Edited by Mike Walsh FIN Wednesday, November 3, 2010 11:05 AM sig removed - don't ask people to mark your answers
    Tuesday, November 2, 2010 1:34 PM
  • but in my case item updated is also fired in file uploading.
    Tuesday, November 2, 2010 1:39 PM
  • Yes. try something like

     

     

            private bool NewItemAdded = false;

            private enum DocumentAddStatus {Adding = 0, Editing = 1};

            private DocumentAddStatus CurrentDocStatus ;

            public override void ItemAttachmentAdding(SPItemEventProperties properties)

            {

                base.ItemAttachmentAdding(properties);

                NewItemAdded = true;

                CurrentDocStatus = DocumentAddStatus.Adding;

            }

            public override void ItemUpdating(SPItemEventProperties properties)

            {

                base.ItemUpdating(properties);

                if (NewItemAdded == true)

                {

                    CurrentDocStatus = DocumentAddStatus.Adding;

                    NewItemAdded = false;

                }

                else

                {

                    CurrentDocStatus = DocumentAddStatus.Editing;

                }

            }

            public override void ItemUpdated(SPItemEventProperties properties)

            {

                if (CurrentDocStatus == DocumentAddStatus.Editing)

                {

    //your update code goes here.

                }           

     

            }


     

    • Marked as answer by Shafaqat Tuesday, November 2, 2010 2:01 PM
    • Edited by Mike Walsh FIN Wednesday, November 3, 2010 11:04 AM sig removed - don't ask people to mark your answers
    Tuesday, November 2, 2010 1:57 PM