0

I have an event receiver where I change the name of the file,it works right but If I click two times the record button (this is used in an infopath form) a new item is created and an itemAdded event throws and I don't want this, I only want to change the name of the file. If I click on save anc close it renames the file like I want, how can I prevent the second change, maybe I can know if the item has been saved formerly?

I put below my code:

   using (SPSite site = new SPSite(properties.Web.Site.ID))
                        {
                            using (SPWeb web = site.OpenWeb(properties.Web.ID))
                            {
                                //Grab a new copy of our list and listitem under the new security context
                                SPList list = web.Lists[properties.List.ID];
                                SPListItem item2 = list.Items.GetItemById(properties.ListItemId);

                                // //Get a fresh copy of the object to ensure we don't end up with a conflict
                                item2 = list.Items.GetItemById(properties.ListItemId);


                            // I tested this way
                                //item2.File.MoveTo(item2.ParentList.RootFolder.Url + "/" + newFileName + ".xml", false);
                                //item2.File.Update(); 
                                  //item2.File.Update();


                                //// this is what I use ...

                                item2["Name"] = newFileName;
                                item2.SystemUpdate(false);

                            }
                        }

                    }
                    catch (Exception ex)
                    {

                    }
                    finally
                    {
                        properties.Web.AllowUnsafeUpdates = false;
                        //Turn event firing back on
                        base.EventFiringEnabled = true;
                    }
  • What event fires at button record click? n in which event are you modifying this item name? Item added or item updating? – Aanchal Sep 6 '14 at 9:08
  • when the user click on Save, it fires the itemupdated event and there is where I change the name, but if the user clicks another time the button save that's when the itemadded event throws. – Luis Molina Sep 6 '14 at 9:14
  • So where are you using File.Move in your code? – Aanchal Sep 6 '14 at 11:56
  • Could you please clarify record button? I only have save that is also submits the form – XristosK Sep 6 '14 at 23:41
  • excuse me Xristosk, it is the save button. I'm using this code in the itemupdated event receiver Aanchal. – Luis Molina Sep 7 '14 at 15:48

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.