Writing modified date of a document library item through CSOM.

Im working on an application which is replicating files from one document library to another. Since it is a replication, I want the modified date to be preserved when I copy the file from the source library to the target, so that the user will see the real modified date in the target library and not the date of the replication run.

I access SharePoint through the client side object model and I have been able to set the modified date on a SharePoint 2010 on-premise using the following straght-forward code:

var listItem = list.GetItemById(id);
context.Load(listItem);
context.ExecuteQuery();

listItem["Modified"] = "03/11/1999";
listItem.Update();

context.ExecuteQuery();

Unfortunately when I execute the same code on any other SharePoint (2013 On-Premise, 2013 Online or 2010 Online) the code will execute without any errors, but the modified date will not be changed at all.

My actual question is: Is there any possibility to change the modified date throught CSOM on all of these SharePoint-versions?

July 2nd, 2013 1:10pm

Hi ,

I updated the "Modified" column with the value format as "2013-07-03" or "2013-07-02T00:00:00", and it worked.

            ClientContext ctx = new ClientContext("http://miti:90/sites/spt/");
            List list = ctx.Web.Lists.GetByTitle("list3");
            ListItemCollection items = list.GetItems(CamlQuery.CreateAllItemsQuery());
            ctx.Load(items); // loading all the fields
            ctx.ExecuteQuery();

            foreach (var item in items)
            {
                // important thing is, that here you must have the right type
                // i.e. item["Modified"] is DateTime
                item["Modified"] = "2013-07-03"; // or value format "2013-07-02T00:00:00"

                // do whatever changes you want

                item.Update(); // important, rembeber changes
            }
            ctx.ExecuteQuery(); // important, commit changes to the server

http://stackoverflow.com/questions/6947491/updating-field-value-in-sharepoint-using-client-object-model

Thanks

July 3rd, 2013 6:51am

Hi Yang,

thanks for your suggestion. Unfortunately that doesnt make a difference in my case. I tried the formats you suggested, also I tried to use a DateTime-value. Note that all these formats are working on my SP 2010 on-premise. What kind of SP did you run your test on?

Thx

Free Windows Admin Tool Kit Click here and download it now
July 3rd, 2013 7:48am

Hi ,

I tested on SP 2010 on-premise, it can be modified.

Thanks

July 4th, 2013 6:11am

Hi Daniel,

The modified date can be updated by using above code. But if Document library version settings enabled then it will create new version right ?.

Please let us know how do we update by without creating new version..?

I tried another way by checkout file and then check-in the file after update the columns, still its not worked.

                    newFile.CheckOut();

                    FieldUserValue CreatedUserValue = new FieldUserValue();
                    CreatedUserValue.LookupId = newUser.Id;

                    item["Created"] = "8/10/2013 7:04 PM";
                    item["Author"] = CreatedUserValue;
                    item["Modified"] = "8/10/2013 7:04 PM";
                    item["Editor"] = ModifiedUserValue;

                      item.Update();
                    _cContext.ExecuteQuery();

                     newFile.CheckIn(string.Empty, CheckinType.OverwriteCheckIn);

Thanks in advance

Rajendra

Free Windows Admin Tool Kit Click here and download it now
August 13th, 2013 5:25am

Hi Rajendra,

Please try using item.UpdateOverwriteVersion()

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.updateoverwriteversion(v=office.14).aspx

Thanks

August 13th, 2013 7:07am

Just if this might be valuable for someone: As long as the user is Site-Collection administrator, I am able to set modified, created, author and editor.
Free Windows Admin Tool Kit Click here and download it now
August 13th, 2013 8:53am

Daniel,

but item.UpdateOverwriteVersion() is not available in client object model right.

August 13th, 2013 9:13am

Your right Fab, but that's not work when versions enabled on document library. It is creating new version when we update the item/File using Item.Update() method.

Free Windows Admin Tool Kit Click here and download it now
August 13th, 2013 10:07am

I think you can solve that if you check out the file first and after you made your changes on the list item you check the file in with CheckinType.OverwriteCheckIn.
August 13th, 2013 10:10am

                    newFile.CheckOut();

            
                    item["Created"] = "8/10/2013 7:04 PM";
                    item["Author"] = CreatedUserValue;
                    item["Modified"] = "8/10/2013 7:04 PM";
                    item["Editor"] = ModifiedUserValue;

                      item.Update();
                    _cContext.ExecuteQuery();

                     newFile.CheckIn(string.Empty, CheckinType.OverwriteCheckIn);

Pls see my above code, I did same way but my original Modified date and modified by values are changing.



Free Windows Admin Tool Kit Click here and download it now
August 13th, 2013 10:34am

Oh, Im sorry, I didnt see that.

I think as soon as you call CheckIn, the modified and created date will be reset by SharePoint, however you can modify them after the checkin and just call update.

August 13th, 2013 10:39am

I got a work around for this problem..

Before updating the Modified, Modified By values, First we need to Disable the version settings for document library. Once the fields updated then again we need to enable the version settings. 

find the updated code below..

                                      //Disable Version settings for DOC LIB before update Metadata

                                        _List.EnableVersioning = false;
                                        _List.Update();
                                        _cContext.ExecuteQuery();

                                       ListItem item = newFile.ListItemAllFields;
                                       _cContext.Load(item);
 
                                        //Updating Metadata Created, CreatedBy, Modified, Modified By

                                         item["Created"] = "8/10/2013 7:04 PM";
                                         item["Author"] = CreatedUserValue;
                                         item["Modified"] = "8/10/2013 7:04 PM";
                                         item["Editor"] = ModifiedUserValue;

                                         item.Update();
                                        _cContext.ExecuteQuery();

                                        //Enable Version settings for DOC LIB after update Metadata
                                           _List.EnableVersioning = true;
                                          _List.Update();
                                        _cContext.ExecuteQuery();

Rajendra
  • Proposed as answer by khwaila 16 hours 12 minutes ago
Free Windows Admin Tool Kit Click here and download it now
August 13th, 2013 1:10pm

I got a work around for this problem..

Before updating the Modified, Modified By values, First we need to Disable the version settings for document library. Once the fields updated then again we need to enable the version settings. 

find the updated code below..

                                      //Disable Version settings for DOC LIB before update Metadata

                                        _List.EnableVersioning = false;
                                        _List.Update();
                                        _cContext.ExecuteQuery();

                                       ListItem item = newFile.ListItemAllFields;
                                       _cContext.Load(item);
 
                                        //Updating Metadata Created, CreatedBy, Modified, Modified By

                                         item["Created"] = "8/10/2013 7:04 PM";
                                         item["Author"] = CreatedUserValue;
                                         item["Modified"] = "8/10/2013 7:04 PM";
                                         item["Editor"] = ModifiedUserValue;

                                         item.Update();
                                        _cContext.ExecuteQuery();

                                        //Enable Version settings for DOC LIB after update Metadata
                                           _List.EnableVersioning = true;
                                          _List.Update();
                                        _cContext.ExecuteQuery();

Rajendra
  • Proposed as answer by khwaila Friday, April 03, 2015 3:20 PM
August 13th, 2013 1:10pm

i dosen't generate an error but the field "Author" is not modified :(
Free Windows Admin Tool Kit Click here and download it now
April 3rd, 2015 1:01pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics