Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
.NET
.NET Core
.NET MAUI
.NET Standard
Active Directory
ADO.NET
Agile Development
AI
AJAX
AlbertAGPT
Alchemy
Alexa Skills
Algorand
Algorithms in C#
Android
Angular
ArcObject
ASP.NET
ASP.NET Core
Augmented Reality
Avalanche
AWS
Azure
Backbonejs
Big Data
BizTalk Server
Blazor
Blockchain
Bootstrap
Bot Framework
Business
Business Intelligence(BI)
C#
C# Corner
C# Strings
C, C++, MFC
Career Advice
Careers and Jobs
Chapters
ChatGPT
Cloud
Coding Best Practices
Cognitive Services
COM Interop
Compact Framework
Copilot
Cortana Development
Cosmos DB
Cryptocurrency
Cryptography
Crystal Reports
CSS
Current Affairs
Custom Controls
Cyber Security
Data Mining
Data Science
Databases & DBA
Databricks
Design Patterns & Practices
DevExpress
DevOps
DirectX
Dynamics CRM
Enterprise Development
Entity Framework
Error Zone
Exception Handling
F#
Files, Directory, IO
Flutter
Games Programming
GDI+
General
Generative AI
GO
Google Cloud
Google Development
Graphics Design
Graphite Studio
Hardware
Hiring and Recruitment
HoloLens
How do I
HTML 5
Infragistics
Internet & Web
Internet of Things
Ionic
Java
Java and .NET
JavaScript
JQuery
JSON
JSP
Knockout
Kotlin
Langchain
Leadership
Learn .NET
Learn iOS Programming
LINQ
Machine Learning
Metaverse
Microsoft 365
Microsoft Fabric
Microsoft Office
Microsoft Phone
Microsoft Teams
Mobile Development
MongoDB
MuleSoft
MySQL
NEAR
NetBeans
Networking
NFT
NoCode LowCode
Node.js
Office Development
OOP/OOD
Open Source
Operating Systems
Oracle
Outsourcing
Philosophy
PHP
Polygon
PostgreSQL
Power Apps
Power Automate
Power BI
Power Pages
Printing in C#
Products
Progress
Progressive Web Apps
Project Management
Public Speaking
Python
Q#
QlikView
Quantum Computing
R
React
React Native
Reports using C#
Robotics & Hardware
RPA
Ruby on Rails
RUST
Salesforce
Security
Servers
ServiceNow
SharePoint
Sharp Economy
SignalR
Smart Devices
Snowflake
Software Architecture/Engineering
Software Testing
Solana
Solidity
Sports
SQL
SQL Server
Startups
Stratis Blockchain
Swift
SyncFusion
Threading
Tools
TypeScript
Unity
UWP
Visual Basic .NET
Visual Studio
Vue.js
WCF
Wearables
Web API
Web Design
Web Development
Web3
Windows
Windows Controls
Windows Forms
Windows PowerShell
Windows Services
Workflow Foundation
WPF
Xamarin
XAML
XML
XNA
XSharp
Register
Login
1
Answer
chunk.feedata(mem) isn't writing (I think), help please?
Bob
11y
2.5k
1
Reply
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(); }
}
}
}
Post
Reset
Cancel
Answers (
1
)
Next Recommended Forum
code problem
Enumeration Comparison