Thursday, September 24, 2009

SharePoint WebParts | Changing List Values, Adding Fields

Sometimes you just want to change field values in lists and it just won't work:

SPSite MySite;
SPWeb MyWeb;
SPList MyPictureList;


foreach (SPListItem list_item in MyPictureList.Items)
{
txtTitle = list_item[“Title”]; //returns correctly

list_item["Title”] = “ test”; //does not change value on list
}


why??? oh god why???

(GOD) my son, you forgot to UPDATE the damn field:


foreach (SPListItem list_item in MyPictureList.Items)
{
txtTitle = list_item[“Title”]; //returns correctly

list_item["Title”] = “ test”; //DOES change value on list

list_item.Update();
}


to add a field:

list_item.Fields.Add("textcolor", SPFieldType.Text, false);
list_item["textcolor"] = txt_text_color.Text;

Note: Strangely I've found that the field is created on all items of the list and not just in the current list item, althought instantiation does...

and that's all folks

Source: MYSELF

No comments:

Post a Comment