0

i have a textfield in a list, that i want to fill with custom text when a item is being added. nothing happens when i try to debug it, the breakpoint in the method is never reached.

Please can someone provide me with some example code about how to accomplish this.

2 Answers 2

1

first create one event receiver, sample code (not tested, just to give some basic idea)

public class YourClass: SPItemEventReceiver
{
   public override void ItemAdded(SPItemEventProperties properties)
   {
      SPListItem item=properties.ListItem;
      item["YourFieldNameInWhichYouWantToPlaceCustomText"]="Custom Text";
      //any other logic
      item.Update(); //must!!! or you can use item.SystemUpdate() also based on your requirement
   }
}

then create another feature receiver

public class MyFeatureReceiver: SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {            
            SPWeb web = properties.Feature.Parent as SPWeb;
            if (web != null && web.Lists["YouList"] != null)
            {
                web.Lists["YourList"].EventReceivers.Add(SPEventReceiverType.ItemAdded,
                    "Featurename, Version=1.0.0.0, Culture=neutral, PublicKeyToken=token",
                    "EventReceiverClass");
            }            
    }

    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    {            
            SPWeb web = properties.Feature.Parent as SPWeb;

            if (web != null && web.Lists["YourList"] != null)
            {
                SPEventReceiverDefinitionCollection Recievers = site.Lists["YourList"].EventReceivers;
                //delete event receiver or perform some other activity
            }            
    }
}

that's all in a nutshell, if you dont have explicit reason, i would suggest go with ItemAdded rather than ItemAdding, ItemAdding shoudl be used only when synchronous activity is required, if you still face problem kindly post your code here

7
  • Thanks for your reply! before i saw your code i solved it with the exact same solution. it was enough with the first few lines that you posted. thanks again
    – David
    Oct 6, 2012 at 16:50
  • u r welcome, can you please accept it as answer, a thread should always be completed, thank you
    – Diptarag
    Oct 7, 2012 at 20:55
  • Hi Diptarag! Iam having small problems with my ItemAdded method event receiver. Here is the Code: SPListItem item = properties.ListItem; item["casenumber"] = "support-case-"+item["ID"]; item.Update(); When i Add my first item to the list. The value is not set to the field. The first item should get the value support-case-1. But when i add my second item to the list, then i can see the first items value. do you have any clue why it does like that?
    – David
    Oct 11, 2012 at 14:59
  • when you add second item you see the value "support-case-1" for second item? or when you add first item nothing happens and after adding second item both gets updated? please clarify
    – Diptarag
    Oct 12, 2012 at 5:41
  • Yes exactly when i add the second item both gets updated!
    – David
    Oct 12, 2012 at 8:25
0

this first popped from googling it: http://msdn.microsoft.com/en-us/library/ms437502.aspx

Which event are you using? ItemAdded or ItemAdding? This seems to be the common issue...

2
  • i have tried both of them. When i try the ItemAdding event, the event fires. But not when i use the ItemAdded. To specify my requirments more. I have a list which name is "cases". I have a field in that list which name is "casenumber" that field is of type Text. When you add a new item to that list the field "casenumber" should not be filled in newform When the item have been added, i want the field value to be "casenumber" and then also add the items built in sharepoint ID. so for example first item will be casenumber1 casenumber2 and so on... thanks for your help and reply!
    – David
    Oct 5, 2012 at 11:29
  • Please anyone that can help me?? have been working with this for hours!
    – David
    Oct 5, 2012 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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