开发者

SPListItem not added to SPList

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 th开发者_如何学Ce 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜