当前位置: 动力学知识库 > 问答 > 编程问答 >

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.

网友答案:

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.

网友答案:

some code i have for importing documents

$user = $web.EnsureUser(@"domain\user")
$item["Created By"] = $user
$item["Modified By"] = $user
$item.UpdateOverwriteVersion();
分享给朋友:
您可能感兴趣的文章:
随机阅读: