forge

Creating a file on WSS with the same created/modified date as a file on the file system.

Apparently, I hit a sore spot for the community with my discussion about created and modified fields.  The code that follows is a very rough hack at how you can get a file into WSS while setting the created and modified dates.  I didn’t bother making the code lookup the users’ ID for the site from the name on the file — but that could be done with another half a dozen lines of code or so.  So here’s a command line utility to upload a file to WSS with the same created/modified dates as the original file…  Remember this is a hack… it’s definitely not designed to be used for anything more than realizing how it can be done…

using System;
using System.IO;
using Microsoft.SharePoint;

namespace TESTCREATED
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
if (args.Length < 3)
{
Console.WriteLine(“TESTCREATED siteUrl ListName FilePath FileName”);
}
string paramSite = args[0];
string paramList = args[1];
string paramPath = args[2];
string paramName = args[3];

Help Your SharePoint User

SPSite spSite = new SPSite(paramSite);
SPWeb spWeb = spSite.OpenWeb();

SPListCollection spLists = spWeb.Lists;
spLists.IncludeRootFolder = true;
SPList spList = spLists[paramList];

SPFolder rf = spList.RootFolder;

FileInfo fi = new FileInfo(paramPath);
FileStream fs = File.OpenRead(paramPath);
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, (int) fs.Length);
SPUser createdBy = spWeb.Users[0];
SPUser modifiedBy = spWeb.Users[0];
SPFile file = rf.Files.Add(paramName, bytes, createdBy, modifiedBy, fi.CreationTimeUtc, fi.LastWriteTimeUtc);

}
}
}