1

This question already has an answer here:

I am trying to update current items, but all items in the list get updated why?

 public override void ItemAdding(SPItemEventProperties properties)
    {
        using (SPWeb web = properties.OpenWeb())
        {

            SPList myList = web.Lists.TryGetList("DocumentList");
            SPListItemCollection itemCollection = myList.Items;
            web.AllowUnsafeUpdates = true;

            for (int iCount = 0; iCount < itemCollection.Count; iCount++)
            {

                SPListItem _item = itemCollection[iCount];

                _item["MyField"] = "";
                _item.Update();
            }




        }}

marked as duplicate by Eric Alexander Jun 17 '15 at 12:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • You just want to update the item currently being added? In the code you are updating all the items – Arsalan Adam Khatri Jun 17 '15 at 10:04
  • Because you loop over all items and update them? – Dolgsthrasir Jun 17 '15 at 10:08
  • @ArsalanAdamKhatri Yes i only want to update the current item being added, – Batistuta Jun 17 '15 at 10:13
  • @TheRock use properties.ListItem instead, because that is the one that is being added. Don't loop over all items. – Dolgsthrasir Jun 17 '15 at 10:14
  • properties.ListItem is null in ItemAdding. If you want to use that, use ItemAdded. – Jussi Palo Jun 17 '15 at 10:20
1

You can modify item properties, except ID like this.

public override void ItemAdding(SPItemEventProperties properties)
{
    string mycol_internal = "";

    using (SPWeb web = properties.OpenWeb()) {
        mycol_internal = web.Lists[properties.ListId].Fields["My Column"].InternalName;
    }

    properties.AfterProperties[mycol_internal] = "your new value";

    //Update title with another column value etc
    properties.AfterProperties["Title"] = properties.AfterProperties[mycol_internal].ToString();
}

Source

OR, use ItemAdded where properties.ListItem is available.

  • I want to modify an existing column and set the string to empty, should i not use Itemadded now? – Batistuta Jun 17 '15 at 10:20
  • I'd say it's a matter of preference. Should work both ways. – Jussi Palo Jun 17 '15 at 10:22
  • Alright will try it out now using ItemAdded.. – Batistuta Jun 17 '15 at 10:28
  • Well, if you just want to clear one field, I'd say ItemAdding is much neater, no need to check in after changes and worry about versions if you have versioning enabled. – Jussi Palo Jun 17 '15 at 10:29
  • I will update i dont get it to work still using ItemAdded – Batistuta Jun 17 '15 at 10:33
-1

In the code you have written a for loop which is looping all the items in the list. So for every item it is updating the column value until it reaches the last list item count. Use properties.ListItem to update the current item in which you have ItemAdding event will fire.

  • Can you please elaborate your answer with some code examples you can take reference of one available in question. – Asad Refai Jun 17 '15 at 10:32
  • No need to elaborate, properties.ListItem is null in ItemAdding. – Jussi Palo Jun 17 '15 at 10:50
  • My bad, Ithought you are using ItemAdded event. In item adding you will not get properties.Listitem rather use AfterProperties as follows – shatabdi Jun 17 '15 at 10:59
  • public override void ItemAdding(SPItemEventProperties properties) { properties.AfterProperties["MyField"] =""; } – shatabdi Jun 17 '15 at 11:00

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