question

sp13test-6829 avatar image
0 Votes"
sp13test-6829 asked Jerryzy answered

How to Overwrite SharePoint's Created Date after Doing a SaveBinaryDirect?


 using (var ctx = new ClientContext(DataSharepointSite))
     {
         ctx.AuthenticationMode = ClientAuthenticationMode.Default;
         ctx.Credentials = GetCreds();
         using (FileStream fs = new FileStream(localFile, FileMode.Open))
         {
             Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, sharepointFile, fs, true);
         }
    
         //****************
         // Now somehow set Modified Date to local File's Modified Date
         //****************
         var file = ctx.Web.GetFileByServerRelativeUrl(sharepointFile);
         ListItem lstItem = file.ListItemAllFields;
         ctx.Load(lstItem);
         ctx.ExecuteQuery();
         lstItem["CreatedDate"] = System.IO.File.GetCreationTime(localFile).ToUniversalTime();
         lstItem.Update();
         ctx.ExecuteQuery();
     }

But I can't set into item, is there any wrong in the code ?

office-sharepoint-server-development
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Jerryzy avatar image
1 Vote"
Jerryzy answered

Hi @sp13test-6829 ,

Modify a bit in the code:


     var file = ctx.Web.GetFileByServerRelativeUrl(sharepointFile);
     ListItem lstItem = file.ListItemAllFields;
     ctx.Load(lstItem);
     ctx.ExecuteQuery();
     lstItem["Created"] = System.IO.File.GetCreationTime(localFile).ToShortDateString();
     lstItem.Update();
     ctx.ExecuteQuery();

Thanks
Best Regards



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.