Suresh Chaudhary
Suresh Chaudhary

Reputation: 1649

update created by and modified by filed in document library, sharepoint 2010

i have uploaded the document in document library using event handler in sharepoint 2010. but when the document is uploaded the created by and modified field is always show system account user.if my login user is other then system account too. so, can any one help me how to update document library modified by and created by field using event handler. my code to update field is:

item.Web.AllowUnsafeUpdates = true; item["Author"] = "testuser"; item.Update(); item.Web.AllowUnsafeUpdates = false;

but i got the error: Author field is read only.

please help me.

Upvotes: 0

Views: 5934

Answers (2)

KVD
KVD

Reputation: 11

When file is an SPFile object and oUser is an SPUser object, you can set the Created/Modified values with this approach:

file.Item["Created"] = DateTime.Now.AddDays(-30);
file.Item["Modified"] = DateTime.Now.AddDays(-30);

file.Item["Created By"] = oUser;
file.Item["Modified By"] = oUser;

file.Item.Update();

With this the names of created/modified by get updated with the name of oUser, and the modified/created dates get updated with the date of last month (30 days back).

Don't forget to update the item afterwards to save the changes.

Upvotes: 1

djeeg
djeeg

Reputation: 6765

some code i have for importing documents

$user = $web.EnsureUser(@"domain\user")
$item["Created By"] = $user
$item["Modified By"] = $user
$item.UpdateOverwriteVersion();

Upvotes: 0

Related Questions