0

I have got a problem how to update Secound list which is made by First list. All must be in Event Receiver. How Can I made difrent Event Receivers update to Diferent Lists Could You Help me with his. I have got a Adding items which look like like this:

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

       if (properties.List.Title == "Wpisy")
       {
           SPSite oSiteCollection = new SPSite("http://wasp03/");
           SPWeb witryna = oSiteCollection.RootWeb;
           SPListItemCollection listaDniSwiatecznych = witryna.Lists["DR2013 Święta"].Items;
           SPListItemCollection listaDniUrlopowych = witryna.Lists["ListaUrlopow2012"].Items;
           SPList lstOtherList = properties.Web.Lists["Szczegoly"];
          // lstOtherList.EnableThrottling = false;

           // Makking two veriables One the day start, and one the day stop
           dzienStart = new DateTime();
           dzienStop = new DateTime();
           // setting two fields from propeties
           dzienStart = Convert.ToDateTime(properties.AfterProperties["DzienStart"]).ToUniversalTime();

           dzienStop = Convert.ToDateTime(properties.AfterProperties["DzienStop"]).ToUniversalTime();

           // Setting a loop while which should be done until the propeties from dzienstart don't equal with dzienstop


           while (dzienStart <= dzienStop)
           {

               if (!RozliczSwieta(oSiteCollection, witryna, listaDniSwiatecznych))
               {
                   continue;
               }
               if(!RozliczUrlopy(oSiteCollection, witryna, listaDniUrlopowych, properties))
               {
                   continue;
               }

                // If are check out that the date are in weekends or not
                       if (dzienStart.DayOfWeek.ToString() == "Saturday" || dzienStart.DayOfWeek.ToString() == "Sunday")
                       {
                           // If a veriable equal Saturday, or sunday they day increase by one 
                           dzienStart = dzienStart.AddDays(1.0);
                           continue;
                       }
                       // In the others hand the object is adding on the list 
                       else
                       {
                           SPListItem item = lstOtherList.Items.Add();
                           item["Kontrakt"] = properties.AfterProperties["Kontrakt1"];
                           item["Pracownik"] = properties.UserDisplayName;
                           item["Dzien"] = dzienStart;
                           item["Procent"] = properties.AfterProperties["Procent"];
                           SPFieldLookup lookup = (SPFieldLookup)properties.List.Fields["Procent"];
                           Int32 singlelookupItemID = Convert.ToInt32(properties.AfterProperties["Procent"]);
                           string lookupval = properties.Web.Lists[new Guid(lookup.LookupList)].GetItemById(singlelookupItemID)[lookup.LookupField].ToString();
                           item["Procent_wartosc"] = lookupval;
                           SPList listaWpisy = properties.Web.Lists["Wpisy"];
                           SPListItemCollection listaKontrakty = listaWpisy.Items;
                           foreach (SPListItem kontrakt in listaKontrakty)
                           {
                               item["ID Czas Pracy Wpisy"] = new SPFieldLookupValue(kontrakt.ID, kontrakt.ID.ToString());
                               item["ID_z_Wpisy"] = kontrakt.ID+1;
                           }
                           dzienStart = dzienStart.AddDays(1.0);

                           item.Update();
                       }


           }

       //    SPUtility.Redirect("http://gzawistowskilap/SitePages/Close.aspx?IsDlg=1", SPRedirectFlags.Default, current);

       }



   }

What should I vrite to items update to update some sort of items ?

Edited about Wood answer:

So i should do like this:

SPListItem originalItem = //Get the item with the method you need from the original list most likely from enumerating the original list?
SPListItem item = list.Items.Add();

item["field1"] = originalItem["field1"];
item["field2"] = originalItem["field2"];
item["field3"] = originalItem["field3"];
item["field4"] = originalItem["field4"];
item.Update()

[Edited to take into account your changes, correcting the code]

1 Answer 1

1

At a glance that code should work all you need to do is a little modification and maybe clean it up so it's easier for you to follow, it gets a little crazy but the basics of adding an item are:

SPListItem originalItem = //Get the item with the method you need from the original list most likely from enumerating the original list?
SPListItem item = list.Items.Add();
item["field"] = originalItem["field"];
item.Update()

With that in mind, you can copy the fields over from one items to the new item and update it. I would do it one field at a time, doing the whole item won't work as you intend.

5
  • Please Hugh Wood tell more :) I updated my question about Your answer.
    – Grzegorz Z
    Commented Nov 28, 2012 at 14:20
  • 1
    I updated your question with the alteration.
    – Hugh Wood
    Commented Nov 28, 2012 at 14:43
  • Ok thanks, but i think I'm have something next in mind in my question. In the some of answers yes You are right, but next I want update in update panel existing items, not creating agein a item, but maybe I don't say in right order what I must done. I must done a updeted elements.
    – Grzegorz Z
    Commented Nov 28, 2012 at 15:03
  • 1
    If you are updating elements, then you need to get the element to update via a query first.
    – Hugh Wood
    Commented Nov 28, 2012 at 15:06
  • But how ? CAML Query ?
    – Grzegorz Z
    Commented Nov 28, 2012 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.