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

sp13test 281 Reputation points
2021-04-01T13:11:06.397+00:00
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 ?

Microsoft 365 and Office | SharePoint Server | Development
0 comments No comments
{count} votes

Accepted answer
  1. ZhengyuGuo 10,586 Reputation points Moderator
    2021-04-02T02:11:10.807+00:00

    Hi @sp13test ,

    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.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.