Вопрос

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