Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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]

share|improve this question
add comment

1 Answer

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.

share|improve this answer
    
Please Hugh Wood tell more :) I updated my question about Your answer. –  Grzegorz Z Nov 28 '12 at 14:20
1  
I updated your question with the alteration. –  Hugh Wood Nov 28 '12 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 Nov 28 '12 at 15:03
1  
If you are updating elements, then you need to get the element to update via a query first. –  Hugh Wood Nov 28 '12 at 15:06
    
But how ? CAML Query ? –  Grzegorz Z Nov 28 '12 at 17:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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