This blog post is about an issue I faced during data import into SharePoint document libraries. I hope it helps others facing the same issue.
Documents had to be migrated to SharePoint 2007 document libraries from an existing application along with relevant metadata. In the first stage, documents were exported to a file server and corresponding metadata values were made available in an excel sheet. In the second stage, documents were to be imported into the document libraries.
I was able to get all the documents into document library along with all metadata except following 4 fields:
Initial Work (actually Initial Search)
I did what most developers do - surf the Internet and search for a "readymade" solution :-)
After a lot of searching and researching, I found most relevant information on Sowmyan Soman's Blog in his post - Can we update the values of "Created By", "Modified By" columns in SharePoint lists ? His solution worked for Lists but not Document Libraries; I don't know the reason yet.
There was also a comment - Remove Read-Only on "Created By", etc. to that post by "Dan". Dan mentioned about removing the ReadOnly="TRUE" attribute from the relevant fields in ONET.XML file. Suggested solution seemed logical but did not work for me.
Enter the Microsoft Support team. They resolved the matter in no time by pointing to an existing method available in the SDK!! The solution is using:
SPFileCollection.Add Method (String, Stream, SPUser, SPUser, DateTime, DateTime) Creates a file in the collection based on the specified URL, on a Stream object that contains a file, on user objects that represent the users who created and last modified the file, and on DateTime values that specify when they did so.
The SDK article also has a code example for copying files in a document library of a Web site to a document library in another Web site. The example preserves the time created and last modified values of the original library. I modified the code to work in my scenario.
Lesson learnt: Read the SDK first and then search on the Internet. For some reason, results from SDK do not end up high in search results on Google or Live. Not sure where the problem lies, with the search engines or with MSDN library.
The Fine Print
The code works when files are being uploaded into a document library. There is no SDK method to update Created and Modified information for exisiting files in a document library.
The Code inside a Try - Catch block
//Get the Document library
SPFolder objFolder = oWeb.GetFolder(strDocLibName);
//Get all files in a collection
mystream.Read(contents, 0, (
note: variables for which you need to set the value are shown in italics. For actual code, please follow the Best Practices: Using Disposable Windows SharePoint Services Objects too.
Hope you are doing good.
We were getting into the same trap too. Hope your solution saves us. I would try and get back.
Works well! Thanks a lot!
Hii, I am currently in the same issue, unable to figure out the solution. I am currently using the sharepoint WSS web service API to upload/ download documents into a document library. I want to set the created by and modified by fields for a folder/document to some thing else, like the users' accounts other than the system account which i use to upload the documents. Is there any way to do this through the WSS web service API? Can you please help me? I tried to write a custom web service using the sharepoint object model in the sharepoint server so that i can call this service remotely from my application. It works fine with setting properties other than these 4 fields (created, created by, modified, modified by). When i try to update these fields, it results in a error "The document at "<docLib/document>" is invalid or non existent....
Can any one please shed some light over this...?
how to do the same using moss web service. Any ideas...
Thanks in advance,
You can actually update the author on a document library, but you must update the Author and the Editor fields, and call UpdateOverwriteVersion() on the item.
You will most likely want to modifiy the vti_author property too. Assuming $itm is a valid item reference here is some powershell to accomplish the modification-
$itm["Author"].tostring() #show current author
$itm["Author"] = $oUser #list item author
$itm.Properties["vti_author"] = $oUser.User.LoginName #file properties author
$itm["Editor"] = $oUser #this needs to be changed as well to update the item.
$itm["Author"].tostring() #print the changed author