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

Possible Duplicate:
How tu update One list from another, element by element

I want update with some changes one list from another. The secand list should have details of first list. I want to updayte one list by secound list

First list has got elements:

                Contract | Worker | Day Start   | Day Stop   | Percent
                --------------------------------------------------------
 Example        Con1     | Paul   | 2012-10-12  | 2012-10-15 | 1/8

Secound List should have

                Contract   |   Worker   |   Day      |   Percent
                ------------------------------------------------
Should Have     Con 1      |    Paul    | 2012-10-12 |    1/8
                Con 1      |    Paul    | 2012-10-13 |    1/8
                Con 1      |    Paul    | 2012-10-14 |    1/8
                Con 1      |    Paul    | 2012-10-15 |    1/8

So In this example I want get from one list and one data record, 4 record with escribe of days. Please help me

I have vriten a Code, but it always copy elements, and only I want to update when anothere element is added:

    protected void Button1_Click(object sender, EventArgs e)
    {




        SPWeb witryna = SPContext.Current.Web;
        SPListItemCollection czasPracyWpisy = witryna.Lists["DR2013 Czas Pracy Wpisy"].Items;
        SPListItemCollection czasPracySzczegoly= witryna.Lists["DR2013 Czas Pracy Szczegóły"].Items;



        int index;
        string hfzm1, hfzm2;


        foreach (SPListItem oCzasPracyKolejnyWpis in czasPracyWpisy)
        {
            foreach(SPListItem oCzasPracyKolejnySzczegoly in czasPracySzczegoly){



            // Dodawanie poszczególnych elementów

               hfzm1 = oCzasPracyKolejnySzczegoly["Numer Kontraktu/Oferty/Szansy"].ToString();
               HFzm1.Value=hfzm1;
               hfzm2 = oCzasPracyKolejnyWpis["Numer Kontraktu/Oferty/Szansy"].ToString();
                HFzm2.Value=hfzm2;


            }

            if (
(HFzm2.Value != HFzm1.Value && HFzm1.Value ==HFzm2.Value || HFzm1.Value=="" || HFzm2.Value=="" )
                )
            {
                SPListItem item = czasPracySzczegoly.Add();



                item["Numer Kontraktu/Oferty/Szansy"] = oCzasPracyKolejnyWpis["Numer Kontraktu/Oferty/Szansy"].ToString();
                item["Pracownik"] = oCzasPracyKolejnyWpis["Pracownik"].ToString();
                item["Dzień"] = oCzasPracyKolejnyWpis["Dzień Start"].ToString();
                index = oCzasPracyKolejnyWpis["Procent"].ToString().IndexOf("#");
                item["Procent"] = oCzasPracyKolejnyWpis["Procent"].ToString().Substring(index + 1);
                item.Update();
            }
            else
            {
                Label6.Text = "Mamy do czynienia ze zduplikowanymi wartościami";
            }
        }

    }

}

}

Only I want to update one list when anothere element is put. Without a Duplication value

share|improve this question

marked as duplicate by Anders Rask Oct 12 '12 at 13:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
please dont create duplicate questions! –  Anders Rask Oct 12 '12 at 13:29
add comment

1 Answer 1

you are using SPListItem item = czasPracySzczegoly.Add();

that will add a list item thats the reason

You first have to get the list item you want, update properties and then update the list item

using (SPSite oSPsite = new SPSite("http://website url/"))
{
using (SPWeb oSPWeb = oSPsite.OpenWeb())
      {
            oSPWeb.AllowUnsafeUpdates = true;

            // Fetch the List
            SPList list = oSPWeb.Lists["MyList"];



            // Update the List item by ID
            SPListItem itemToUpdate = list.GetItemById(yourid);
            itemToUpdate["Description"] = "Changed Description";
            itemToUpdate.Update();



            oSPWeb.AllowUnsafeUpdates = false;
       }
}
share|improve this answer
    
Yes, but then I will have duplicate values. Where are loops ? –  Grzegorz Z Oct 12 '12 at 14:23
    
Nobody is going to do the work for you, what kind of programmer are you if you cant do a loop? did you at least tried something? –  L.V. Sharepoint Architect Oct 15 '12 at 7:57
add comment

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