This is the old code where copy to a local path, but now i need to save the file on SharePoint. How can i use stream with it and write the stream to file.
File.Copy(oTemplatePath, destinationPath, true);
using (WordprocessingDocument document = WordprocessingDocument.Open(destinationPath, true))
{
document.GetMergeFields("reference_number").ReplaceWithText(refrenceNumber);
document.MainDocumentPart.Document.Save();
for (int i = 0; i < newDoc.jsonFields.Count; i++)
{
if (newDoc.jsonFields[i].type == "date")
{
document.GetMergeFields(newDoc.jsonFields[i].controlName).ReplaceWithText(DateTime.Parse(newDoc.jsonFields[i].data).ToShortDateString());
document.MainDocumentPart.Document.Save();
}
else
{
document.GetMergeFields(newDoc.jsonFields[i].controlName).ReplaceWithText(newDoc.jsonFields[i].data);
document.MainDocumentPart.Document.Save();
}
}
//document.GetMergeFields(newDoc.jsonFields[i].controlName).ReplaceWithText(newDoc.jsonFields[i].data);
//document.MainDocumentPart.Document.Save();
}
You can save to local temp folder
string tempDocx = Path.Combine(Path.GetTempPath(), fileName);
logger.Trace($"Creating Word document {tempDocx}");
File.Delete(tempDocx);
File.Copy("Template.docx", tempDocx);
Then upload to SharePoint, here using a FileStream
using (IO.FileStream fs = new IO.FileStream(tempDocx, IO.FileMode.Open))
{
List documentsList = clientContext.Web.Lists.GetByTitle(libTitle);
clientContext.Load(documentsList.RootFolder);
clientContext.ExecuteQuery();
var fileCreationInformation = new FileCreationInformation();
//Assign to content byte[] i.e. documentStream
fileCreationInformation.ContentStream = fs;
//Allow owerwrite of document
fileCreationInformation.Overwrite = true;
//Upload URL
fileCreationInformation.Url = documentsList.RootFolder.ServerRelativeUrl + "/" + IO.Path.GetFileName(tempDocx);
uploadFile = documentsList.RootFolder.Files.Add(fileCreationInformation);
clientContext.Load(uploadFile);
clientContext.ExecuteQuery();
}