LayerAccess
LayerAccess

Reputation: 73

Open XML SDK 2.5 + SharePoint 2013 - docx-file empty

I'm trying to generate a simple docx-file in SharePoint 2013 using Open XML SDK 2.5.

My function successfully generates a docx-file in the SharePoint-library, but when I try to open it, it is empty or corrupt. I already tried a lot and used different technics I found here and in the web.

 protected void generateDoc(string docName)
    {
        SPWeb spWeb = SPContext.Current.Web;

        spWeb.AllowUnsafeUpdates = true;
        spWeb.Lists.IncludeRootFolder = true;

        SPList docLib = spWeb.Lists["test"];
        string fileUrl = Microsoft.SharePoint.Utilities.SPUtility.GetFullUrl(SPContext.Current.Site, "/test/" + docName);

        SPSecurity.CodeToRunElevated elevatedSubmit = new SPSecurity.CodeToRunElevated(delegate
        {
            // MediaStream erstellen
            using (MemoryStream ms = new MemoryStream())
            {
                // Create a Wordprocessing document.
                using (WordprocessingDocument package = WordprocessingDocument.Create(ms, WordprocessingDocumentType.Document))
                {
                    // Add a new main document part. 
                    package.AddMainDocumentPart();
                    // Create the Document DOM. 
                    package.MainDocumentPart.Document = new Document();
                    package.MainDocumentPart.Document.Append(new Body());
                    package.MainDocumentPart.Document.Append(
                           new Paragraph(
                             new Run(
                               new Text("TEST"))));

                    ms.Position = 0;
                    byte[] content = ms.ToArray();
                    package.MainDocumentPart.Document.Save();

                    SPFile file = docLib.RootFolder.Files.Add(fileUrl, ms, true);
                    file.Update();
                }
            }
        });
        SPSecurity.RunWithElevatedPrivileges(elevatedSubmit);
    }

It would be nice if somebody could give me a hint.

Thanks in advance!!

Upvotes: 1

Views: 1754

Answers (2)

Mohamed Alikhan
Mohamed Alikhan

Reputation: 1335

Actually you are trying to read the content before its written. So it wont contain any data. Remove and put the following code from the second using to the end of first using.

ms.Position = 0;
byte[] content = ms.ToArray();
SPFile file = docLib.RootFolder.Files.Add(fileUrl, ms, true);
file.Update();

for getting the content to the byte array. The modified code will be

      using (MemoryStream ms = new MemoryStream())
            {
                // Create a Wordprocessing document.
                using (WordprocessingDocument package = WordprocessingDocument.Create(ms, WordprocessingDocumentType.Document))
                {
                    // Add a new main document part. 
                    package.AddMainDocumentPart();
                    // Create the Document DOM. 
                    package.MainDocumentPart.Document = new Document();
                    package.MainDocumentPart.Document.Append(new Body());
                    package.MainDocumentPart.Document.Body.Append(
                           new Paragraph(
                             new Run(
                               new Text("TEST"))));

                    //package.MainDocumentPart.Document.Save();
                    package.MainDocumentPart.Document.Save();

                }
                ms.Position = 0;
                byte[] content = ms.ToArray();SPFile file = docLib.RootFolder.Files.Add(fileUrl, ms, true);
file.Update();

            }

Hope this works for you.

Upvotes: 0

Eric White
Eric White

Reputation: 1891

I recommend that instead of fabricating a document from scratch, you start with an empty document and add the content that you want to it. This is a much more reliable way to proceed. You can store the binary document in an embedded resource, or you can embed the document directly in your C# code, as shown in this post.

Another important hint - instead of writing the C# code yourself, use the document reflector capabilities of the Open XML SDK 2.5 Productivity Tool. It can take any document, or any part of a document, or any element in any part, and generate valid C# code that you can use in your application. You can find a demo of the Open XML SDK Productivity Tool in the "Tools" screen-cast that you can find in these Introduction to Open XML screen-casts. You should also be familiar with the other interesting tools, including the Open XML Package Editor PowerTool for Visual Studio 2010 (this is a vital tool). You may want to view the other screen-casts in that series as well.

After viewing the screen-casts on Open XML in general, you may want to view these screen-casts on WordprocessingML.

Cheers, Eric

Upvotes: 1

Related Questions