Forum Discussion

Jason Chan's avatar
Jason Chan
Copper Contributor
May 05, 2017

Pnp-sites-core upload file with custom modified and created field

When using the Pnp-Powershell Add-PnpFile cmdlet, we can upload a file and alter the Created or Modified field at the same time. 

 

e.g. I can change the Modified field on the uploaded file
PS:> Add-PnPFile -Path .\sample.doc -Folder "Shared Documents" -Values @{Modified="1/1/2016"}

 

I would like to know are there equivalent code in Pnp-sites-core component?

3 Replies

  • The PowerShell Cmdlet uses the following code:

     

    Microsoft.SharePoint.Client.File file;
                if (ParameterSetName == "AsFile")
                {
    
                    file = folder.UploadFile(FileName, Path, true);
                }
                else {
                    file = folder.UploadFile(FileName, Stream, true);
                }
    
                if (Values != null)
                {
                    var item = file.ListItemAllFields;
    
                    foreach (var key in Values.Keys)
                    {
                        item[key as string] = Values[key];
                    }
    
                    item.Update();
    
                    ClientContext.ExecuteQueryRetry();
                }

    So you could use the same.

    • Jason Chan's avatar
      Jason Chan
      Copper Contributor
      Thanks, just wanna know if pnp-sites-core have handy extension doing the same thing with fewer line of code...

Resources