Search code examples
c#sdkwindows-10-universalonedrive

How to adjust DateModified property on a single OneDrive item


I am able to upload a file using the OneDrive SDK no problem. As per the information on the OneDrive Dev Center, The FileSystemInfo.DateModified refers to the time the file was seen by the service, rather than when it was modified locally.

I am attempting to manually alter it to the local value with the suggestion to include them with the request, but the value being set in my code is not sticking and is returning to the time when the PutAsync<Item> request is completed. What Am I doing wrong?

My code:

if (localfile != null)
                {
                    localprop = await localfile.GetBasicPropertiesAsync();
                    localtime = localprop.DateModified;
                    try
                    {
                        Stream syncstream = await localfile.OpenStreamForReadAsync();
                        using (syncstream)
                        {
                            var upload = await _userDrive.Drive.Special.AppRoot.ItemWithPath(filepath).Content.Request().PutAsync<Item>(syncstream);
                            upload.FileSystemInfo.LastModifiedDateTime = localtime;
                        }
                    }
                    catch (OneDriveException)
                    { }
                }

My query against the same:

oneDItem = await _userDrive.Drive.Special.AppRoot.ItemWithPath(filepath).Request().GetAsync();
                var oneDtime = (DateTimeOffset)oneDItem.FileSystemInfo.LastModifiedDateTime;

Solution

  • If you upload a file to one drive, there is no parameter for LastModifiedDateTime to request together, you may not change the modified time when uploading. But you can update an item metadata by update request. After uploading, you can get the item you just uploaded and update its LastModifiedDateTime meta data. Code as follows:

    if (localfile != null)
    {
        var localprop = await localfile.GetBasicPropertiesAsync();
        var localtime = localprop.DateModified;               
        try
        {
            Stream syncstream = await localfile.OpenStreamForReadAsync();
            using (syncstream)
            {
                DriveItem upload = await _userDrive.Me.Drive.Root.ItemWithPath("regfolder/regdata.jpg").Content.Request().PutAsync<DriveItem>(syncstream);
                DriveItem updateitem = new DriveItem() {
                      FileSystemInfo=new Microsoft.Graph.FileSystemInfo()
                      {
                          LastModifiedDateTime = localtime
                      }
                };                      
                DriveItem Updated = await _userDrive.Me.Drive.Root.ItemWithPath("regfolder/regdata.jpg").Request().UpdateAsync(updateitem); 
            }
        }
        catch (Exception ex)
        { }
    }