0

I try to add an item to my sharepoint list from a custom web part. Is there any mistake in my code?

[WebMethod]
        public static string AjouterDemande()
        {
            SPList ListeDemandeConge = SPContext.Current.Site.RootWeb.Lists["DEMANDE CONGÉ"];
            SPListItem ListItem = ListeDemandeConge.Items.Add();
            ListItem["CODE"] = code;
            ListItem["DEMANDEUR"] = Utilisateur.LoginName;
            ListItem["DATE DEMANDE"] = DateTime.Today.Date;
            ListItem["DATE FIN"] = EndDateD;
            ListItem["DATE DEPART"] = StartDateD;
            ListItem["DUREE"] = int.Parse(DayCount);
            ListItem["PREMIER RESPONSABLE"] = Employe.FirstSuperior.LoginName;
            ListItem["DEUXIEME RESPONSABLE"] = Employe.SecondSuperior.LoginName;
            ListItem["PRESENCE PRES"] = Boolean.Parse(FirstRespPresent);
            ListItem["SOLDE CONGE"] = Conge.SoldeAnneeConge;
            ListItem["MOIS"] = int.Parse(StartDateTab[1]);
            ListItem["ANNEE"] = int.Parse(StartDateTab[0]);
            ListItem["JOURS APPROUVEES"] = 0;
            ListItem.Update();
            ListeDemandeConge.Update();
            return "true";

        }

When I use F10 to process debugging step by step and get to my listitem.update(), it went out without giving any error.

  • display name work for me to get those colomns value – mokhtar nebli Sep 15 '15 at 16:02
  • ListeDemandeConge.Update(); any reason for updating the List? – sekhar Sep 15 '15 at 16:05
  • is there any other suggestion ! – mokhtar nebli Sep 15 '15 at 16:07
  • Change the listitem variable name to avoid confusion. – Akhoy Sep 15 '15 at 16:09
  • i tried that and it give nothing, i think that the problem is in ListeDemandeConge.AddItem() because no item is added to the list after this instruction – mokhtar nebli Sep 15 '15 at 16:13
0

Try SPList.AddItem instead of Items.Add

However, I can't figure out why your code is not working! Below are some links for your reference

List.AddItem() and List.Item.Add() … why two methods for same purpose SPList.Add() vs SPList.AddItem() SharePoint 2010

  • it doesn't work also no item is added to list to be updated – mokhtar nebli Sep 15 '15 at 15:45
  • is there any other solution ? – mokhtar nebli Sep 16 '15 at 8:07
  • what is SPList.Items.Count before and after the execution of this code? do you see any possibility of issue with permissions? – Vamsi K K Sep 16 '15 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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