1

As a requirement of our application, we add files with other file's properties within document libraries using the following method:

private static SPFile AddFile(SPListItem item, Stream stream, string filename, SPFolder destinationFolder, string comment)
        {
            string destinationFilePath = destinationFolder.Url + "/" + filename;
            using (var web = item.Web)
            {
                object file;
                switch (SPFarm.Local.BuildVersion.Major)
                {
                    case 12:
                        var parameters2007 = new object[] { destinationFilePath, stream, false, item.File.Author, web.CurrentUser, item.File.TimeCreated, item.File.TimeLastModified, null, comment, true };
                        file = destinationFolder.Files.GetType().GetMethod("AddInternal", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(string), typeof(Stream), typeof(Boolean), typeof(SPUser), typeof(SPUser), typeof(DateTime), typeof(DateTime), typeof(Hashtable), typeof(string), typeof(Boolean) }, null).Invoke(destinationFolder.Files, parameters2007);
                        break;
                    default:
                    case 14:
                        var parameters2010 = new object[] { destinationFilePath, stream, null, item.File.Author, web.CurrentUser, item.File.TimeCreated, item.File.TimeLastModified, comment, true };
                        file = destinationFolder.Files.GetType().GetMethod("Add", BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(string), typeof(Stream), typeof(Hashtable), typeof(SPUser), typeof(SPUser), typeof(DateTime), typeof(DateTime), typeof(string), typeof(Boolean) }, null).Invoke(destinationFolder.Files, parameters2010);
                        break;
                }
                return file as SPFile;
            }
        }

The reason we use reflection is that, we need to override created_by and modified_by properties of the file when we create it; which had proved very painful to do by other methods. And Sharepoint 2007's API was limited to accomplish that, hence reflection.

There is a scenario where the web.CurrentUser is sharepoint/system (usually because the context is in elevated state) which is fine as long it is used for modified_by. However, in Sharepoint 2013, SPFileCollection.Add method does not override created_by property, it simply keeps it as CurrentUser(independent to modified_by parameter) whatever we do about it. That results messing the whole workflow of our application.

I've checked Sharepoint 2013's dll file using reflector and it seems the method and its parameters are the same as 2010.

Updating properties after creating the files is something I'd like to avoid since I have bad memories with it. Usually fixing one problem creates several, and I don't recall the problems in detail. Not to mention that that change would need extensive testing in 3 different versions.

Is there a functional way to override created_by property of a file using SPFileCollection.Add on Sharepoint 2013?

Natan
  • 2,816
  • 20
  • 37

0 Answers0