I spent a good part of the day today trying to figure out how to pragmatically add files to a document library.  I had to read in a directory of files, parse their names and add them to the document library.  In addition to adding the file, I needed to set a few fields, one of them being a Date and Time column/property.  Easy enough, right?  Well, I quickly found out it wasn't as easy as I originally thought.

My first attempt went as you would think.  I tried to set the Property to a DateTime object.

    1 //create the SP file to upload and set its initial properties

    2 SPFile fileToUpload = docLib.RootFolder.Files.Add(fl.Name, binFile, true);

    3 fileToUpload.Properties["Name"] = string.Format("{0} {1}", fileNameParts[1], fileNameParts[0]);

    4 fileToUpload.Properties["DateField"] = DateTime.Parse("12/18/2007");

    5 fileToUpload.Update();


As I thought, that worked fine.  The problem came when i hit a date that was prior to 1/1/1970.

    1 //create the SP file to upload and set its initial properties

    2 SPFile fileToUpload = docLib.RootFolder.Files.Add(fl.Name, binFile, true);

    3 fileToUpload.Properties["Name"] = string.Format("{0} {1}", fileNameParts[1], fileNameParts[0]);

    4 fileToUpload.Properties["DateField"] = DateTime.Parse("1/1/1968");

    5 fileToUpload.Update();


It throws the following error : "Microsoft.SharePoint.SPInvalidPropertyException: Date Times before 1/1/1970 are not supported.".  So anything after 1/1/1970 works fine, but anything prior to 1/1/1970 throws an error?!?!?!?!?

I searched high and low all over Google, the forums, etc.  Nothing, relating to anything with .NET, DateTime object, and a 1/1/1970 barrier.  So after doing a bunch of trial and error, I stumbled upon it. 
Sharepoint expects Date and Time columns in UTC format.  So what does that mean?

A UTC date should come in the form : yyyy-mm-ddThh:mm:ssZ    where T and Z are literals.
So here is the updated code that worked for me.

    1 //create the SP file to upload and set its initial properties

    2 SPFile fileToUpload = docLib.RootFolder.Files.Add(fl.Name, binFile, true);

    3 fileToUpload.Properties["Name"] = string.Format("{0} {1}", fileNameParts[1], fileNameParts[0]);

    4 //Date's are stored in SP in the format :    yyyy-mm-ddThh:mm:ssZ   T/Z are literals

    5 fileToUpload.Properties["DateField"] = "1950-01-01T00:00:00Z";

    6 fileToUpload.Update();


Posted in: Sharepoint  Tags: , ,

Comments


September 9. 2008 03:05
Cory Foy
Thanks! One correction. The format should be: yyyy-MM-ddThh:mm:ssZ instead of yyyy-mm-ddThh:mm:ssZ.

http://www.cornetdesign.com/http://www.cornetdesign.com/


United States Live_Love_Laugh
May 14. 2009 10:37
Live_Love_Laugh
One more correction. The format should be "yyyy-MM-ddTHH:mm:ssZ" instead of "yyyy-MM-ddThh:mm:ssZ"

no site


January 25. 2010 07:14
instant cash advance
The people who get on in this world are the people who get up and look for the circumstances they want, and, if they can't find them, make them.

http://cashusloans.com/http://cashusloans.com/


January 29. 2010 17:17
Properties in Infonavit
i found your blog, nice post, good theme, very helpfull, keep post, thanks

http://www.casas-infonavit.com/http://www.casas-infonavit.com/


February 1. 2010 08:04
Loans in New Mexico
For success, attitude is equally as important as ability.

http://superpaydayloan.com/state/New-Mexicohttp://superpaydayloan.com/state/New-Mexico


February 1. 2010 08:11
Loans in MI
To cultivate kindness is a valuable part of the business of life.

http://superpaydayloan.com/state/Michiganhttp://superpaydayloan.com/state/Michigan


February 3. 2010 00:52
Clarins
Hi
I feel enjoy in your site. Your site look awesome and cool.
Happy blogging and keep writing.

thanks

http://www.biomedicskincare.org/clarins-skin-care.htmhttp://www.biomedicskincare.org/clarins-skin-care.htm


February 8. 2010 05:25
cash loans
There is joy in work. There is no happiness except in the realization that we have accomplished something.

http://cashusloans.com/http://cashusloans.com/


February 8. 2010 05:30
faxless cash advance
The world of achievement has always belonged to the optimist.

http://cashusloans.com/http://cashusloans.com/


February 9. 2010 02:52
fast payday loans
Experience is not what happens to a man. It is what a man does with what happens to him.

http://fastloansus.com/http://fastloansus.com/


February 9. 2010 02:57
payday loans
Hitch your wagon to a star.

http://fastloansus.com/http://fastloansus.com/

Search Blog

Blog Roll

    OPMLDownload OPML file

    Recent Comments

    Banners

    Theme Grabber
    Disclaimer
    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2010 Tony Testa's World