Welcome to MSDN Blogs Sign in | Join | Help

Correct usage of SPFileCollection.Add() overloaded method in SPS 2003

I was recently involved in a case involving SPFileCollection.Add() method.  When we use one of the overloads of Add() method the “Created” & “Modified” dates weren’t correct (+5:30 hrs at my end & -7 hrs at the customer’s end – not exactly sure why, perhaps it’s defaulting to GMT as that appears to be the only logical reason).  The following are the overloaded methods in SPFileCollection.Add() method.

 

·         Add(string strUrl,byte[]file, bool overwrite)

·         Add(string strUrl,byte[] file)

·         Add(string strUrl,byte[] file,SPUser createdBy,SPUser modifiedBy,DateTime timeCreated,DateTime timeLastModified)

 

If we use the last overloaded method and provide DateTime.Now as the values for timeCreated & timeLastModified, we’ll see a difference in time between the time the document was uploaded and the time that shows up in the document library against “Created” & “Modified” fields.  Looks like DateTime.Now (by default) picks up GMT time.  Note: The other overloaded methods return the correct date & time.

 

A simple fix is to use DateTime.Now.ToUniversalTime()instead.

 

With my limited search experience, I was not able to find thing small information recorded yet.  So, thought of blogging this and saving some of your time J

 

A quick sample here:

 

static void Main(string[] args)

{

      SPSite site = new SPSite("http://<sharepointserverurl>");

      SPWeb web = site.OpenWeb();

      SPFolder folder = web.GetFolder("shared documents");

      SPUser user = site.Owner;

 

      FileStream fs = new FileStream("test.doc",FileMode.Open);

      byte[] fileContents = new byte[(int)fs.Length];

      fs.Read(fileContents,0,(int)fs.Length);

      fs.Close();

 

      SPFile file = folder.Files.Add("test.doc",fileContents,user,user,DateTime.Now.ToUniversalTime(),DateTime.Now.ToUniversalTime());

 

      Console.WriteLine("Successfully uploaded document");

      Console.Read();

}

Published Friday, June 08, 2007 2:48 AM by sridhara
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Tuesday, September 09, 2008 6:44 PM by jim hudson

# re: Correct usage of SPFileCollection.Add() overloaded method in SPS 2003

thanks sridhar, just what i needed. jim

Thursday, December 11, 2008 5:09 AM by jas

# re: Correct usage of SPFileCollection.Add() overloaded method in SPS 2003

Thanks lot  sridhar, thats my requirement. i search for a long time.

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker