문제

I'm using the following code to add an item to a list on the top level of my application but it is not adding anything, does anyone know why? Is there anything missing?

It doesn't return me any error, just doesn't add the item and the list remains empty.

The code is in the FeatureActivated method of the feature where the list instance is being deployed.

using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    SPList icons = web.GetList(path)

                  SPSecurity.RunWithElevatedPrivileges(delegate()
                  {
                    SPListItem icon = icons.Items.Add();

                    icon[SPBuiltInFieldId.Title] = "title";
                    icon[new Guid("d3429cc9-adc4-439b-84a8-5679070f84cb")] = "class1";

                    icons.Update();
                  }
도움이 되었습니까?

해결책

you have to call the Update() method of the icon object, not icons.

다른 팁

I found out there are 2 ways of successfully add an item to a list:

  1. Like Andreas Scharf said:

SPListItem item = list.Items.Add();
item["Title"] = "some title";
item.Update();

  1. Some other way using the AddItem() instead of the Add() from the items collection

SPListItem item = list.AddItem();
item["Title"] = "some title"; // Add item's field values
item.Update(); //also the item is updated, not the list

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top