locked
Cannot copy list items from one list to another RRS feed

  • General discussion

  • I am trying to copy one list from one web to another.

    The list is copied everytime but while copying items it exits with an exception and says that the field cannot be updated.

    When i check in the web both lists have same number of fields with same titles but why it is still not copying the data.

    foreach (SPListItem item in SourceList.Items)
                    {
                        SPListItem newDestItem = Destlist.Items.Add();
    
                        foreach (SPField field in item.Fields)
                        {
                            if (!Destlist.Fields.ContainsField(field.Title))
                            {
                                if (!Destlist.Fields.Equals(field.Title))
                                {
                                    Destlist.Fields.Add(field);
                                    Destlist.DefaultView.ViewFields.Add(field);
                                }
                            }
                            if (!field.Title.Equals("Title"))
                            {
                                newDestItem[field.Title] = item[field.Title];
                                newDestItem.Fields[field.Title].ShowInViewForms = true;
                            }
    
    
    
                        }
                        newDestItem.Update();
                    }
                    Destlist.OnQuickLaunch = SourceList.OnQuickLaunch;
                    Destlist.Update();
                    DestWeb.Update();

    Above is the code i dont have any idea what the hell is happenig with this why it is not copying the items.

    Anyone can suggest anything to me. having hard time with this...


    kukdai

    Sunday, March 4, 2012 6:32 PM

All replies

  • Try below code




    foreach (SPListItem item in SourceList.Items)
                    {
                      


                        foreach (SPField field in item.Fields)
                        {
                            if (!Destlist.Fields.ContainsField(field.Title))
                            {
                                if (!Destlist.Fields.Equals(field.Title))
                                {
                                    Destlist.Fields.Add(field);
                                    Destlist.DefaultView.ViewFields.Add(field);
                                }
                            }






     Destlist.Update();SPListItem newDestItem = Destlist.Items.Add();


      


                            if (!field.Title.Equals("Title"))
                            {
                                newDestItem[field.Title] = item[field.Title];
                                newDestItem.Fields[field.Title].ShowInViewForms = true;
                            }






                        }
                        newDestItem.Update();
                    }
                    Destlist.OnQuickLaunch = SourceList.OnQuickLaunch;
                    Destlist.Update();
                    DestWeb.Update();



    Rahul Sharma
    http://sharepointarrow.blogspot.com/

    Tuesday, March 6, 2012 8:11 AM