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]