none
Set value date/time field to null.

    Question

  • Dear developers,

     

    I have a document library with a couple of fields. For example a start date and a end date. The field type that I’m using is Date/Time. Both fields are not required and the default value is empty.

     

    Within SharePoint you can add documents and let’s say fill in a start date with the value 09-01-2007 and a end date with the value 14-010-2008.  

     

    After this, we decide to leave the end date field empty. We open the item in edit mode, clear the value and save the changes. This all works fine, of course...its standard functionality...

     

    The problem lies in running through the same scenario's using the SharePoint object model....

     I tried various ways to set the value of the end date to “”, null and also the default value, which is empty. 

     

    Code Snippet

    item["EndDate"] = "" ;

    item.Update();

     

     

    Or

     

    Code Snippet

    item["EndDate"] = null;

    item.Update();

     

     

     

    Or

     

    Code Snippet

    SPField field = item.Fields["EndDate"];

    item["EndDate"] = field.DefaultValue;

    item.Update();

     

     

    The above code results in a ”No Valid Date/Time String"  error. Of course this is true: null, "" or the default values aren’t valid date/time values. I really need to clear the value of this field but It looks like its not allowed using the object model.

      

    I hope that somebody found a solution for this problem of just know how to do get this done. I know it should be possible, after all SharePoint is using the same object model.

     

    Thanks in advance!

     

    Arthur van Adrichem

    Tuesday, August 21, 2007 8:21 AM

Answers

  • Well after a long, long trial-and-error code and recode process we finally opened a support ticket with Microsoft... unfortunately we ended up having the following response.

    Indeed it seems to be a bug on the international parsing of date with DateTime Controls and there are several conditions under it will fail... there is no current workaround or hot-fix.

    It might be solved by SP1... whenever it is released... in the mean time... well... you know.


    Sunday, December 02, 2007 9:27 PM

All replies

  • Did find a resolution to this problem. I having the same issues. I can not get that darn date field set to blank.

     

    Let me know.

     

    Thanks!

     

    Tuesday, August 21, 2007 1:50 PM
  •  

    Code Snippet

    using (SPSite site = new SPSite("http://basesmcdev/SiteDirectory/tester1/"))
                {

                    using (SPWeb web = site.OpenWeb())
                    {

                        try
                        {
                            SPList splist = web.Lists["mosstestsearch"];


                            SPListItem myitem = splist.Items.GetItemById(22);
                            myitem["testdate"] = null;
                            myitem.Update();
                           
                       
                        }
                        catch (Exception ex)
                        {
                            string msg = ex.Message;
                        }

          
                   
                    }
                   

                }

     

     

           

    I have no problem with the object model and datetime fields. Are we missing something here. Does the user have the appropriate rights to modifiy the field? How are you obtaining the list item?
    Tuesday, August 21, 2007 4:32 PM
  • i am getting at the listItem in the .ItemUpdating event. when i rum my code i get the errors, any time the code runs "as normal" it works fine.

     

    properties.ListItem where properties = SPItemEventProperties

    Tuesday, August 21, 2007 4:38 PM
  • I'm sure that the user has enough rights. For example, the user is able to add new or delete existing items and the user can even add new fields to the list.

     

    I'm using the following code to find items...

     

    Code Snippet

    private static void UpdateDocumentProperties(SPWeb web)
    {
          try
          {
               foreach (SPDocumentLibrary library in web.GetListsOfType(SPBaseType.DocumentLibrary))
               {

                   // Now iterate through all the documents in that library
                   foreach (SPItem item in library.Items)
                   {

    //Method 1

    SPField field = item.Fields["EndDate"];

    item["EndDate"] = field.DefaultValue;

    item.Update();

     

    //Method 2

    item["EndDate"] = null;

    item.Update();

     

    //etc

                   }
                }
           }
           catch (Exception ex)
           {

           }

    }

     

     

     


           

    Tuesday, August 21, 2007 5:20 PM
  •  

    Gregorio,

     

    What do you mean by run the code "as normal" ?? What is the difference between getting an item the way i do..and the way you do? Both are SPItems

    Thursday, August 23, 2007 5:56 PM
  • I have been facing similar problems... but not only with the object model... but with customized Edit forms.

     

    I have an Spanish localized version of MOSS... does your MOSS is in English?

    Sunday, October 28, 2007 6:02 AM
  • Sie Smile

    Tuesday, October 30, 2007 8:15 AM
  • Well after a long, long trial-and-error code and recode process we finally opened a support ticket with Microsoft... unfortunately we ended up having the following response.

    Indeed it seems to be a bug on the international parsing of date with DateTime Controls and there are several conditions under it will fail... there is no current workaround or hot-fix.

    It might be solved by SP1... whenever it is released... in the mean time... well... you know.


    Sunday, December 02, 2007 9:27 PM
  • Hi ,

    I think you are trying to insert as DBNULL or "", It will give error. Try 

    item["YourDate"] = null;

    hope it will work

    Thanks

     

    Monday, November 02, 2009 6:59 AM
  • Actually it's quite simple, but for a typed language it reads somewhat strange

    item["EndDate"] = (DateTime?) null;

    will do the job..  Casting null pointers is soo much fun ;-)

    Thursday, May 07, 2015 1:46 PM