Richie H’s Weblog

Sharepoint and Stuff

Posts Tagged ‘SPFile’

Collaboration Server

Posted by richieh77 on January 29, 2008

As part of the migration I have been requested to build a tool that will migrate Collaboration server projects including:

  • Creating a Team Site Site Collection for each Collaboration Server project
  • Migrate all documents, version history, modified date and by, and created date, by
  • Mirror Collaboration Server security roles into appropriate Team Site groups

This particular piece of work has proven quite tricky.
The ability to programmatically set the Modified and Created information proved a real challenge and there are several examples out there on how to do this.

The first thing to remember when trying the alter the meta information is not to use the SPFile object directly.
Instead use the item property of the SPFile.

The SPListItem object provide much more flexibility and the Update methods actually work.

There are 3 main ways to update a given item Update, SystemUpdate and UpdateOverwriteVersion

  • The Update method will work but generates a new version of the item.
  • The SystemUpdate didn’t seem to actually save the change.
  • The UpdateOverwriteVersion did exactly what I needed.

This twinned with
http://msdn2.microsoft.com/en-us/library/ms439259.aspx
Allowed me to set the File information correctly.
Be aware that you MUST set the Created and Modified properties after calling the Add method even though you already passed them in

Folders is a little bit more tricky as you don’t get the option to create a folder object and pass the Author in.
To get around this you should create the folder as above and use the SPFolder item property
Once you have the SPListItem you can set the Created and Modified property.  The property will receive an integer that represent the users ID.

Adding file to list and updating the meta information

The file information is queried and held in a data structure

private struct PlumtreeFile
        {
            public System.DateTime Created;
            public System.DateTime Modified;
            public SPUser ModifiedBy;
            public SPUser CreatedBy;
            public string SourcePath;
            public string TargetPath;
            public string Filename;
        }

SPFolder spFolder = newWeb.GetFolder(newWeb.Url + “/Shared Documents/” + pF.TargetPath);
SPFileCollection spFiles = spFolder.Files;
System.IO.FileStream fs = System.IO.File.OpenRead(pF.SourcePath);
SPFile spF = spFiles.Add(pF.Filename, fs, pF.CreatedBy, pF.ModifiedBy, pF.Created, pF.Modified);
SPListItem spFileItem = spF.Item;
spFileItem["Created"] = pF.Created;
spFileItem["Modified"] = pF.Modified;
spFileItem.UpdateOverwriteVersion();
fs.Close();

Adding a folder and changing it’s meta information, as before the information is held in a data structure

private struct PlumtreeFolder
        {
            public System.DateTime Created;
            public System.DateTime Modified;
            public SPUser CreatedBy;
            public string FolderName;
            public Int32 ParentID;

        }

As the source system didn’t allow version control on folders, we will assume that the Created and Modified By user are the same.
SPFolder spNewFolder = fld.SubFolders.Add(urlPath);
int userID;
userID = newWeb.SiteUsers[pF.CreatedBy.LoginName].ID;
spNewFolder.Item["Created By"] = userID;
spNewFolder.Item["Modified By"] = userID;
spNewFolder.Item["Author"] = userID;
spNewFolder.Item["Modified"] = pF.Modified;
spNewFolder.Item["Created"] = pF.Created;
spNewFolder.Item.Update();

Posted in Sharepoint | Tagged: , , , , , , , , , , | 6 Comments »

 
Follow

Get every new post delivered to your Inbox.