Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.

Yanıt Setting Default values for list items

  • 30 Nisan 2012 Pazartesi 15:38
     
     

    Hello,

    I have a sharepoint list in which I have a field whos default value is set to 2..When I create a new item i can see the default value in the column but if I edit the item and I remove the default value and then save the item the default value is gone..What I need to know that how can i make the default value stay in the field if no value is entered in that field even after the item is added and updated.

    Thanks

Tüm Yanıtlar

  • 30 Nisan 2012 Pazartesi 15:47
    Yanıtlayıcı
     
     Yanıt Kod İçerir

    Hello,

    you can add an event on the "itemupdated" and itemadded" event, you check if your column is empty and if it is you set the value to 2.

    you can find some info here : http://msdn.microsoft.com/en-us/library/ms437502.aspx

    something like

    public override void ItemAdded(SPItemEventProperties properties)
    {
        SPListItem oItem = properties.ListItem;
    if(string.IsNullOrEmpty(oItem["YourField"].ToString())
        {oItem["YourField"] = "2";
        oItem.Update();}
    }

    Hope this help



    Best regards, Christopher.
    Blog | Mail


  • 30 Nisan 2012 Pazartesi 15:56
     
     

    Thanks for the reply..So there is no other way than writing code in which we can set the default value which remains there is the user has not entered anything in the field..

  • 30 Nisan 2012 Pazartesi 21:34
     
     
    You cannot put the value back in if the user removes it if the field is not required to have a value. Out of the box you can make it required, or create a list validation the checks that the value is greater than 0, and if not display a message stating you must enter a value and the default should be 2.

    Blog | SharePoint Field Notes Dev Tool | ClassMaster