none
Updating "Modified by" and "modified" in list where versioning is enabled

    Question

  • hi I have a document library in which versioning and content approval is enabled.I have to update the modified by and  modified date field through code.I can update teh field when versioning is enabled but whne versioning is enabled I update the field and the publish my document again modified by field chnges to system account.

    I cannot disable versioing .How can I update the fields and publish?

    My code is

     

      if (newContentUser != null)//this is spuser object which I am getting from anothr func
                                        {

                                            item[ownerFieldName] = newContentUser.ID;//this is people field

                                       
                                            item[modifiedByFieldName] = itemCreatedUser.ID;//assigning value to the modified by field

                                           item[modifiedDateFieldName] = date;//assigning value to the modified date field
                                       item.Update();
                                       
                                  //  item.SystemUpdate(false);
                                         

                                        }

                                        f (enableVersioningRequired && file != null)
                                       {


                                         if (checkFileLevel)
                                      {
                                         file.Publish(publsihMessage);
                                         }
                                           if (contenApprovalRequired)
                                          {
                                              file.Approve(approveMessage);

                                          }

                                     }
                                      //  item["Modified By"] = itemCreatedUser.ID;

                                    //    item["Modified"] = date;
                                        //  item.UpdateOverwriteVersion();

     

     

    I have tried diff way of updating the field but it doesn't work.I have put the field before versioing or approving but nothing works .Any suggestions.I am testing this on form application but I will deploy it as timer job

    Friday, August 13, 2010 10:54 AM

All replies

  • Hi,

    Use following code

      item["Editor"] = itemCreatedUser;
      item["Modified"] = date;

     Hope this will helpful to you.


    Thanks Shrivallabh Kulkarni
    Friday, August 13, 2010 11:46 AM
  • hi

    I am able to update the fields editor and modified by but if the versioning is enabled then after updating the field value I have to publish the item.Once published these field values automatically changes to current date and time which I donot want.I cannot disable versioning

    Friday, August 13, 2010 11:53 AM
  • Hi,

    then you could write an ListItemEventReceiver where you can catch the Event ItemUpdated and then change the FieldValues and make an item.SystemUpdate.


    Best regards,
    Yavuz B.

    My Blog http://www.starcoder.net
    Friday, August 13, 2010 12:18 PM
  • hi

    If I update my field in update event receiver then I think item will change again in draft mode and I have to publish it again which will again modify the field.When I publish the file then my field modified by changes to system account automatically.How can I prevent this change in the field while publishing my item

    Friday, August 13, 2010 12:41 PM
  • You might want to try this.

    Since you have the SPUser object of the user you want, then open your SPSite with the SPUser.UserToken property in the constructor. Then retrieve the SPFile and call the publish method. The modified by property will be set to that user.


    certdev.com
    Saturday, August 14, 2010 3:07 AM
  • I will try but when i publish modified date also changes how can I prevent that?.My modified date should not change but only the modifed by that also the user taken by code but the user will be given by me.

    According to you Steve I will able to publish with the user but not stop updating the modified date field.

    Monday, August 16, 2010 4:46 AM
  • To prevent modified date to be updated by current date, We need to overwrite it with modifed date before publishing it.

     SPListItem itmNewVersion = fileDest.Item;
         itmNewVersion["Modified"] = dateModifiedOn.ToLocalTime();
         itmNewVersion.UpdateOverwriteVersion();

          if (libDest.EnableMinorVersions)
          fileDest.Publish(strVerComment);

    Thursday, December 26, 2013 8:49 AM