0

I'm trying to add a couple items of from an XML file to a SharePoint list. Unfortunately, only the last item gets added. Please, show me where am I going wrong.

  private void UploadExcelFileToSharePointList()
    {

        if (FileUpload1.HasFile)
        {
            string Url = SPContext.Current.Web.Url;

            using (SPSite spSite = new SPSite(Url))
            {
                using (SPWeb spWeb = spSite.OpenWeb())
                {
                    spWeb.AllowUnsafeUpdates = true;


                    SPList Termeslist = spWeb.Lists.GetList(new Guid(ConstVariables2.Termes2Guid), true);

                    using (MemoryStream ms = new MemoryStream(FileUpload1.FileBytes))
                    {

                        XElement xelement = XElement.Load(ms);

                        IEnumerable<XElement> elements = xelement.Elements("text");

                        Console.WriteLine(elements.Count());

                        if (elements.Descendants("body") != null && elements.Descendants("body").Descendants("termEntry") != null)
                        {
                            var elemtermEntries = elements.Descendants("body").Descendants("termEntry");
                            Console.WriteLine(elemtermEntries.Count());

                            int count = elemtermEntries.Count();

                            foreach (XElement xEle in elemtermEntries)
                            {
                                SPListItem itemm = Termeslist.AddItem();

                                var id = xEle.Attribute("id").Value;

                                itemm[ConstVariables2.Terme_Terme_Id_encodé] = id;

                                itemm.Update();
                                Termeslist.Update();

                            }
                        }

                    }

                }
            }

        }

    }
5
  • Did you try to debug this? Does the for loop iterate twice?
    – Amit
    Aug 7, 2015 at 11:28
  • yeah it iterate on all items
    – ezzaam
    Aug 7, 2015 at 11:39
  • You don't need to update the list everytime you add an item. That might be the issue. Aug 7, 2015 at 12:01
  • Paul is right Put ' Termeslist.Update(); ' out of the foreach loop. This will solve your problem. Aug 7, 2015 at 12:13
  • 1
    "Termeslist.Update()" is not required when you want to add/update/delete item to list. Its required only when you have changed some list settings or property. can you post you xml schema here which you are using for adding data? Aug 7, 2015 at 12:21

1 Answer 1

-3

Use this

 SPListItem itemm = Termeslist.Items.Add(); 


`itemm.Update();`not needed  


 Termeslist.Update();

Instead

SPListItem itemm = Termeslist.AddItem();

itemm.Update();
Termeslist.Update();
1
  • If you are changing metadata or the properties of the added item you definitely need to update it. List update is not needed at the end. And you should NEVER use SPList.Items.Add() as this will load the whole item collection to the memory. Instead use SPList.AddItem(); Aug 7, 2015 at 12:02

Your Answer

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

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