Search This Blog

Saturday, December 17, 2011

programmatically Open and save documents in document library SharePoint




The example below has two functions, one for opening and one for saving a specific document in a document library.
This code requires the Open XMl SDK, so you will need to download and install it and reference its assembly. 
In addition, you need to add a reference to the WindowsBase assembly and the Microsoft.SharePoint assembly.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.IO;
using System.Threading;

\\Add Reference
\\C:\Program Files (x86)\Open XML SDK\V2.0\lib\DocumentFormat.OpenXml.dll

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;



  using (SPSite spSite = new SPSite("http://igrid102:1974"))
                {
                    //Get the document from the library using SPQuery
                    SPList list = spSite.RootWeb.Lists["Shared Documents"];
                    SPQuery query = new SPQuery();
                    query.ViewFields = "<FieldRef Name='FileLeafRef' />";
                    query.Query = "<Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='Text'>MyDoc.docx</Value></Eq></Where>";
                    SPListItemCollection collection = list.GetItems(query);
                    //Get the first document returned. There should be one only.
                    SPFile file = collection[0].File;
                    byte[] byteArray = file.OpenBinary();

                    using (MemoryStream memStr = new MemoryStream())
                    {
                        memStr.Write(byteArray, 0, byteArray.Length);

The type ‘System.IO.Packaging.Package’ is defined in an assembly that is not referenced. You must add a reference to   assembly ‘WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35′
                        using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(memStr, true))
                        {
                            //Getting the document object
                            Document document = wordDoc.MainDocumentPart.Document;
                            // Read first paragraph
                            Paragraph firstParagraph = document.Body.Elements<Paragraph>().FirstOrDefault();
                            // Now lets append our own paragraph, if document already has text
                            if (firstParagraph != null)
                            {
                                Paragraph testParagraph = new Paragraph(
                                new Run(new Text("We are Adding this text for Testing!")));
                                firstParagraph.Parent.InsertBefore(testParagraph,
                                firstParagraph);
                            }
                            //After your done. Lets just save it back. Call the SaveDoc method
                           obj.SavingDoc(file, memStr);

                        }
                   
                    }
                }

               private void SavingDoc(SPFile file, MemoryStream memStr)
                {
                string linkFileName = string.Empty;
                linkFileName = file.Item["LinkFilename"].ToString();
                file.ParentFolder.Files.Add(linkFileName, memStr, true);
                }

No comments:

Post a Comment