Reputation: 105
I am getting a word document from SharePoint using Microsoft graph API as a stream and changing some content in that file and downloading the saved content as a file but when I open the file, the modified content is not available. The downloaded file still shows the original content.
using (var memoryStream = new MemoryStream())
{
templateStream.Position = 0;
// Copying the stream that I've got into memory stream
await templateStream.CopyToAsync(memoryStream).ConfigureAwait(false);
memoryStream.Position = 0;
using (var wordDocument = WordprocessingDocument.Open(memoryStream, true))
{
RevisionAccepter.AcceptRevisions(wordDocument);
var document = wordDocument.MainDocumentPart.GetXDocument();
var content = document.Descendants(W.p).ToList();
//based on the dictionary I've I am replacing the contents of the file
foreach (var field in dataDictionary)
{
var regex = new Regex(field.Key, RegexOptions.IgnoreCase);
OpenXmlRegex.Replace(content, regex, field.Value.ToString(), null);
}
//not showing the modified content
wordDocument.Save();
//this is also not updating the memorystream variable with the modified content
wordDocument.MainDocumentPart.Document.Save();
memoryStream.Position = 0;
await memoryStream.FlushAsync().ConfigureAwait(false);
}
var result = memoryStream.ToArray();
memoryStream.Flush();
return result;
}
once I got the byte array from the above code I am downloading the file using this line from my controller
return File(returnResponse, System.Net.Mime.MediaTypeNames.Application.Octet, $"Test-
{System.DateTime.Now}.docx");
What am I doing wrong?
Upvotes: 2
Views: 465
Reputation: 1791
As outlined in this answer you need to call PutXDocument() method on the MainDocumentPart
for your changes to be successfully reflected, because currently, you are making changes but not commiting them to the required document.
Upvotes: 0