1
Answer

chunk.feedata(mem) isn't writing (I think), help please?

Ask a question
Bob

Bob

11y
1.2k
1
  sharepoint 2010 site.  Document set full of Word docs.  Trying to merge them.  

In The line:
parent.InsertAfter(altChunk, sdt);

altChunk's inner text is "", so I'm guessing the previous chunk.feedata(mem) is not writing the data into the chunk (mem is not empty, I stepped thru an dwatched it get increase in size when the byte array is added to it)

I started with code from HERE

Here's what I have so far:  (sorry about the code formatting....I couldn't find any site protocol on that)


using System; //THis is the Eric Blog stuff
using
System.IO;
using
System.ComponentModel;
using
System.Web;
using
System.Text;
using
System.Xml;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
Microsoft.SharePoint;
using
Microsoft.SharePoint.WebControls;
using
Microsoft.SharePoint.Client;
using
System.Linq;
using
DocumentFormat.OpenXml.Packaging;
using
DocumentFormat.OpenXml.Wordprocessing;
using
ClientOM = Microsoft.SharePoint.Client;
using
Microsoft.Office.Word.Server.Conversions;

namespace
BobsDocMerger.VisualWebPart1
{
[
ToolboxItemAttribute(false)]
public class VisualWebPart1 : WebPart
{
// Visual Studio might automatically update this path when you change the Visual Web Part project item.
private const string _ascxPath = @"~/_CONTROLTEMPLATES/BobsDocMerger/VisualWebPart1/VisualWebPart1UserControl.ascx";
protected override void CreateChildControls()
{
System.Web.UI.
Control control = this.Page.LoadControl(_ascxPath);
Controls.Add(control);
base.CreateChildControls();
Button btnSubmit = new Button();
btnSubmit.Text =
"Assemble Documents";
btnSubmit.Click +=
new EventHandler(btnSubmit_Click);
Controls.Add(btnSubmit);
}
void btnSubmit_Click(object sender, EventArgs e)
{
SPFolder folder = SPContext.Current.ListItem.Folder;
char[] splitter = { '/' };
string[] folderName = folder.Name.Split(splitter);
string filePrefix = @"Weekly/" + folderName[0] + "/" + folderName[0];

// SPFile template = folder.Files["My stupid doc.docx"];
SPFile template = folder.Files[filePrefix + " - Aggregate Report Doc.docx"];
SPFile file;
byte[] byteArray = template.OpenBinary();
using (MemoryStream mem = new MemoryStream())
{
mem.Write(byteArray, 0, (
int)byteArray.Length);
using (WordprocessingDocument myDoc = WordprocessingDocument.Open(mem, true))
{
MainDocumentPart mainPart = myDoc.MainDocumentPart;
//Loop thru content controls
foreach (SdtElement sdt in mainPart.Document.Descendants<SdtElement>().ToList())
{
SdtAlias alias = sdt.Descendants<SdtAlias>().FirstOrDefault();
if (alias != null)
{
//The 2 tags in the Report are AggregateHeader and AggregateBody
string sdtTitle = alias.Val.Value;
string sdtTag = sdt.GetFirstChild<SdtProperties>().GetFirstChild<Tag>().Val;
// if (sdtTitle == "Merge")
// {
for (int i = 0; i < folder.Files.Count; i++)
{
file = folder.Files[i];
//Do all files that are NOT the Aggregate Report
if (file.Name.IndexOf("Aggregate Report") == -1)
{
if (i == folder.Files.Count - 1)
{
AddAltChunk(mainPart, sdt, file,
true);
}
else
{
AddAltChunk(mainPart, sdt, file,
false);
}
}
}
//}
}
}
HttpResponse resp = HttpContext.Current.Response;
using (Stream t = template.OpenBinaryStream())
{
using (WordprocessingDocument myDoc2 = WordprocessingDocument.Open(t, true))
{
using (XmlWriter writer = XmlWriter.Create(resp.OutputStream))
{


resp.ClearContent();
resp.ClearHeaders();
resp.AddHeader(
"Content-Disposition", "attachment; filename=Assembled Document.docx");
resp.ContentEncoding = System.Text.
Encoding.UTF8;
resp.ContentType =
"application/msword";
resp.OutputStream.Write(mem.ToArray(), 0, (
int)mem.Length);
myDoc.MainDocumentPart.Document.WriteTo(writer);
resp.Flush();
resp.Close();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
}
}
}
}




protected int id = 1;
void AddAltChunk(MainDocumentPart mainPart, SdtElement sdt, SPFile filename, bool LastPass)
{
string altChunkId = "AltChunkId" + id;
id++;
byte[] byteArray = filename.OpenBinary();
AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
AlternativeFormatImportPartType.WordprocessingML, altChunkId);
using (MemoryStream mem = new MemoryStream())
{
mem.Write(byteArray, 0, (
int)byteArray.Length);
mem.Seek(0,
SeekOrigin.Begin);
chunk.FeedData(mem);
}
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;

//Replace content control with altChunk information
DocumentFormat.OpenXml.
OpenXmlElement parent = sdt.Parent;
parent.InsertAfter(altChunk, sdt);
if (LastPass) { sdt.Remove(); }
}


}
}





 

Answers (1)