none
event does not fire when item checkout

    Question

  • hi all,

    i have created event handlers for check out item, actually i have a field document Code. based on document code when user checkout one item then check out all associated item based on document code. 

    problem is that when user check out item its not fire the event , when i refresh the page then its checking out the all associated item.

    how can i set the issue in event handler

    Thanks


    BestRegards|SharePoint Techies

    Friday, February 24, 2012 11:08 AM

Answers

  •  

    Hi Wasi-Khan-SP,

    Through debugging the code, we can see the code will execute an end-less loop of checking out event, as when checking out one file, it will execute the code to check out related files, then the checking out event will be fired again and again.

    In this situation, we need to disable the event to fire when check out the related files in event receiver, this can be done through SPEventReceiver.EventFiringEnabled = false;

    The code is like below:
    base.ItemCheckingOut(properties);
    string docCode = properties.ListItem["RevisionNo"].ToString();
    using (SPSite site = properties.OpenSite())
    {
    using (SPWeb web = site.OpenWeb())
    {
    SPList list = web.Lists["Technical"];
    SPQuery query = new SPQuery();
    query.Query = "<Where><And>”+
    ”<Eq><FieldRef Name='RevisionNo'/><Value Type='Text'>" + docCode + "</Value></Eq>”+
    ”<Neq><FieldRef Name='ID'/><Value Type='Counter'>" + properties.ListItem.ID + "</Value></Neq>”+
    ”</And></where>";

    query.ViewAttributes = "Scope=\"Recursive\"";
    SPListItemCollection document = list.GetItems(query);
    foreach (SPListItem curItem in document)
    {

    this.EventFiringEnabled = false;
    curItem.File.CheckOut();
    this.EventFiringEnabled = false;
    }
    web.Dispose();
    }
    }

    Thanks,
    Qiao


    Qiao Wei

    TechNet Community Support


    Tuesday, February 28, 2012 9:08 AM
    Moderator

All replies

  • hi,

    please check the below link to debugg the event handlers.

    http://blogs.technet.com/b/speschka/archive/2009/12/25/debugging-event-receivers-in-sharepoint-2010.aspx

    still you could not able to debugg it and solve it .

    please post the code which you are trying so we can help you.


    MCTS,MCPD Sharepoint 2010. My Blog- http://sharepoint-journey.com


    If a post answers your question, please click "Propose As Answer" on that post and "Vote as Helpful


    Friday, February 24, 2012 1:44 PM
  • hi 

    thank you very much for your reply, i have tried to debug.  here is my code.

    public override void ItemCheckingOut(SPItemEventProperties properties)
           {
               base.ItemCheckingOut(properties);
               //base.ItemCheckingOut(properties);
               
               string docCode = properties.ListItem["RevisionNo"].ToString();






               using (SPSite site = properties.OpenSite())
               {
                   using (SPWeb web = site.OpenWeb())
                   {


                       SPList list = web.Lists["Technical"];
                       SPQuery query = new SPQuery();
                       query.Query = "<Where><Eq><FieldRef Name='RevisionNo'/><Value Type='Text'>" + docCode + "</Value></Eq></where>";
                       query.ViewAttributes = "Scope=\"Recursive\"";


                       


                       SPListItemCollection document = list.GetItems(query);


                       //SPListItemCollection document = list.GetItems(query);


                       foreach (SPListItem curItem in document)
                       {


                           curItem.File.CheckOut();


                       }


                       web.Dispose();
                       //w.Dispose();
                   }


               }
           }
           public override void ItemCheckedIn(SPItemEventProperties properties)
           {
               base.ItemCheckedIn(properties);
               string docCode = properties.ListItem["RevisionNo"].ToString();
               


               using (SPSite site = properties.OpenSite())
               {
                   using (SPWeb web = site.OpenWeb())
                   {
                       SPFolder oFolder = web.Folders["Technical"];
                       SPQuery query = new SPQuery();
                       query.Query = "<Where><Eq><FieldRef Name='RevisionNo'/><Value Type='Text'>" + docCode + "</Value></Eq></where>";
                       query.ViewAttributes = "Scope=\"Recursive\"";
                       //    query.RowLimit = 100;


                       SPList list = web.Lists["Technical"];
                       SPListItemCollection document = list.GetItems(query);


                       foreach (SPListItem curItem in document)
                       {
                           SPFile fl = curItem.File;
                           if (fl.CheckOutType != SPFile.SPCheckOutType.None)
                           {
                               curItem.File.CheckIn("a");
                           }


                       }


                       //SPFileCollection ofileColelction = oFolder.Files;


                       //for (int i = 0; i < ofileColelction.Count; i++)


                       //    ofileColelction[i].CheckOut();
                       //}
                       web.Dispose();
                   }
                   site.Dispose();
               }
           }


    BestRegards|SharePoint Techies

    Friday, February 24, 2012 5:36 PM
  • It's possible that an exception is being thrown in your event receiver that is preventing it from completing; this can often appear as if it's not firing at all.  A debugger, as suggested earlier, will tell you if this is the case.
    Friday, February 24, 2012 6:10 PM
  • thanks servy for reply,

    i have debugged but i unable to find any exception, please tell me what should i do

    Thanks


    BestRegards|SharePoint Techies

    Saturday, February 25, 2012 9:26 AM
  •  

    Hi Wasi-Khan-SP,

    I have a test in my local about this event receiver, it seems that you have some relative documents in the same document library, when you check out one file, then you want to check out all the associated files, which have same value in “RevisionNo” column, if I have any misunderstanding, please feel free to let me know.

    If so, the event receiver in this situation is fired actually, when checking out the file, it will shows
    the file “filepath” is checked out for editing by SHAREPOINT/system.
    this is because that in your caml query, you select the current item again to being checking out, as it is in the result of the query collection. This may cause the event need to refresh the page.

    As to this scenario, you may try to set the query to query expected result exclude the current item, this will make the event work as normal.

    Thanks,
    Qiao


    Qiao Wei

    TechNet Community Support

    Monday, February 27, 2012 6:36 AM
    Moderator
  •  thank you very much for your reply, can you please elaborate the you point i didnt understand. how can i set this issue 

    Thanks



    BestRegards|SharePoint Techies

    Monday, February 27, 2012 7:52 AM
  • yes dear you are right that is my scenario, which you have understand, now what should do now  

    BestRegards|SharePoint Techies

    Monday, February 27, 2012 4:01 PM
  •  

    Hi Wasi-Khan-SP,

    Through debugging the code, we can see the code will execute an end-less loop of checking out event, as when checking out one file, it will execute the code to check out related files, then the checking out event will be fired again and again.

    In this situation, we need to disable the event to fire when check out the related files in event receiver, this can be done through SPEventReceiver.EventFiringEnabled = false;

    The code is like below:
    base.ItemCheckingOut(properties);
    string docCode = properties.ListItem["RevisionNo"].ToString();
    using (SPSite site = properties.OpenSite())
    {
    using (SPWeb web = site.OpenWeb())
    {
    SPList list = web.Lists["Technical"];
    SPQuery query = new SPQuery();
    query.Query = "<Where><And>”+
    ”<Eq><FieldRef Name='RevisionNo'/><Value Type='Text'>" + docCode + "</Value></Eq>”+
    ”<Neq><FieldRef Name='ID'/><Value Type='Counter'>" + properties.ListItem.ID + "</Value></Neq>”+
    ”</And></where>";

    query.ViewAttributes = "Scope=\"Recursive\"";
    SPListItemCollection document = list.GetItems(query);
    foreach (SPListItem curItem in document)
    {

    this.EventFiringEnabled = false;
    curItem.File.CheckOut();
    this.EventFiringEnabled = false;
    }
    web.Dispose();
    }
    }

    Thanks,
    Qiao


    Qiao Wei

    TechNet Community Support


    Tuesday, February 28, 2012 9:08 AM
    Moderator