none
How to Set Listitem value In ItemAdding Event Receiver?

    Question

  • Hi

    I want to Set the value of a ListItem while ItemAdding Function.

    I was unable to get the list refernce here.

    Any Idea how to do ?

    Thanks
    Harish
    Saturday, February 20, 2010 6:41 AM

Answers

  • Harish

    Please try following code

    public override void ItemAdding(SPItemEventProperties properties)
    {
    string Name = “”;
    using (SPWeb web = properties.OpenWeb())
    {
    Name = web.Lists[properties.ListId].Fields["Name"].InternalName;

    }
    properties.AfterProperties[Name] = “ChangedValue”;
    }

     

    Let me know this work for you or not?

     

    Ashish

    • Marked as answer by Harish D Monday, February 22, 2010 5:14 AM
    Saturday, February 20, 2010 7:29 AM

All replies

  • Harish

    Please try following code

    public override void ItemAdding(SPItemEventProperties properties)
    {
    string Name = “”;
    using (SPWeb web = properties.OpenWeb())
    {
    Name = web.Lists[properties.ListId].Fields["Name"].InternalName;

    }
    properties.AfterProperties[Name] = “ChangedValue”;
    }

     

    Let me know this work for you or not?

     

    Ashish

    • Marked as answer by Harish D Monday, February 22, 2010 5:14 AM
    Saturday, February 20, 2010 7:29 AM
  • Hi Ashish


    When i try to debug it throws error at this line

    Name = web.Lists[properties.ListId].Fields["Name"].InternalName;

    Error is :
    {"Value does not fall within the expected range."}


    Thanks
    Harish

    Saturday, February 20, 2010 8:16 AM
  • Please check whether the properties.ListID or the field name "Name" really exists. You can use

    list.Fields.ContainsField()

    to check whether fieldName exists in the list.
    Regards
    NLV
    Saturday, February 20, 2010 10:38 AM
  • Hi



    If I see in QuicKWatch it is returning true value  but when I press F11 it throws the above error.



    Thanks
    Harish



    Saturday, February 20, 2010 10:58 AM
  • I would use ItemAdded for this. 
            public override void ItemAdded(SPItemEventProperties properties)
            {
                base.ItemAdding(properties);
                properties.ListItem["FieldName"] = "My Value";
                properties.ListItem.Update();
            }
    Saturday, February 20, 2010 6:06 PM
  • Hi Evc 


    Thanks for the reply

    It wont work here I have already tried here 

    FYI

    http://www.sharepoint-tips.com/2006/09/synchronous-add-list-event-itemadding.html

     
    Thanks
    Harish

    Sunday, February 21, 2010 2:48 AM
  • Harish

    Have you tried my code with other field for testing purpose?

    Ashhish
    Sunday, February 21, 2010 3:49 AM
  • hi , you can use the afterproperties, i think that you should add the specified field to the list definition schema. if you are using sharepoint build in fields , so try to refer to this field using SPBuiltInFieldId , like : SPBuiltInFieldId.Title
    Sunday, February 21, 2010 9:20 PM
  • Thanks  Ashish

    I have tried with Title column it is working fine
    I dont know the error before 
    but Now I have created a new List and Tried It is workign fine


    below is the code.

     

     

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

     

    {

     

     

    using (SPWeb web = properties.OpenWeb())

    {

    Name = web.Lists[properties.ListId].Fields[

    "Test"].InternalName.ToString();

    }

     

    string strIp = current.Request.ServerVariables["REMOTE_ADDR"].ToString();

    properties.AfterProperties[Name] =strIp ;

    }

     



    Thanks
    Harish

    • Proposed as answer by maybeIgotit Friday, April 19, 2013 10:33 AM
    Monday, February 22, 2010 5:13 AM
  • Harish

    Glad to know that you are all set.

    Good Luck



    Ashish Kanoongo, MCP, MCSD, MCTS
    Monday, February 22, 2010 5:27 AM
  • Thanks Ashish for the Post It help me a lot


    I was trying to get Ip address to the list  for the current user logged in

    from past 2 weeks I was working on this how to get this


    Thanks
    Harish
    Monday, February 22, 2010 6:15 AM
  • Hello Folks,

    I am trying to GET value of column which is lookup column. But, I am getting ID when I tried fetching value of that column.

    Below is the code:

     string s = properties.AfterProperties["DirectorName"].ToString();//DirectorName is a lookup column to Director List.

    //s = 70. 70 is a ID in Director List.

    However, I need actual value of Director. Pls. assist.


    Thanks, Chintan

    Friday, December 21, 2012 11:00 AM