This project has moved and is read-only. For the latest updates, please go here.

integration with sharepoint 2010

May 26, 2012 at 6:57 PM

can I use docx to manipulate files in a sharepoint 2010 document library?

May 26, 2012 at 7:12 PM

Yes. I don't see why it wouldn't work. You just have to write code to access sharepoint and get the file.

May 28, 2012 at 5:03 PM
Edited May 28, 2012 at 5:04 PM
ghostme wrote:

can I use docx to manipulate files in a sharepoint 2010 document library?

Yes, Its simple, Download the file from Sharepoint lib as a stream, and use

using (var doc = DocX.Load(stream))

{

	//Your code here
	//Once you finished working save it back to the stream
	doc.Save(stream);
	
}

Upload it back to sharepoint.

May 28, 2012 at 5:08 PM

If you can please provide code on how to achive this so next time we won't have questions like that :-)

May 29, 2012 at 10:14 AM

Hello Thanks for hint, however from the samples that I ran I am running into some difficulties in inserting header to the documents in a document library.

 

Code below

string siteUrl = "http://moss1";

 using (SPSite spSite = new SPSite(siteUrl))
 {

 Console.WriteLine("Querying for doc1.docx");
 SPList list = spSite.RootWeb.Lists["Test"];
 SPQuery query = new SPQuery();
 query.ViewFields = @"<FieldRef Name='FileLeafRef' />";
 query.Query =
 @"<Where>
 <Eq>
 <FieldRef Name='FileLeafRef' />
 <Value Type='Text'>doc1.docx</Value>
 </Eq>
 </Where>";
 SPListItemCollection collection = list.GetItems(query);
 if (collection.Count != 1)
 {
 Console.WriteLine("doc1.docx not found");
 Environment.Exit(0);
 }
 Console.WriteLine("Opening");
 SPFile file = collection[0].File;
 byte[] byteArray = file.OpenBinary();
 

 //using (MemoryStream memStr = new MemoryStream())
 //{
 //memStr.Write(byteArray, 0, byteArray.Length);
 //using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(memStr, true))
 //{
 //Document document = wordDoc.MainDocumentPart.Document;
 //document.MainDocumentPart.DeleteParts(document.MainDocumentPart.HeaderParts);
 //MainDocumentPart mainPart = wordDoc.MainDocumentPart;

 //AddPicHeader(wordDoc);
 //document.Save();
 //wordDoc.Close();
 
 //}

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

         using (var doc = DocX.Load(memStr))

{

	//Your code here
	//Once you finished working save it back to the stream
              // Get the default Header for this document.
    Header header_default = doc.Headers.even;
 
    //// Get the default Footer for this document.
    //Footer footer_default = doc.Footers.even;

    // Insert a Paragraph into the default Header.
    Paragraph p1 = header_default.InsertParagraph();

    p1.Append("Hello Header.").Bold();

    // Insert a Paragraph into the document.
    Paragraph p2 = doc.InsertParagraph();
    p2.AppendLine("Hello Document.").Bold();
 
    //// Insert a Paragraph into the default Footer.
    //Paragraph p3 = footer_default.InsertParagraph();
    //p3.Append("Hello Footer.").Bold();
	doc.Save();
	
}
     
 Console.WriteLine("Saving");
 string linkFileName = file.Item["LinkFilename"] as string;
 file.ParentFolder.Files.Add(linkFileName, memStr, true);
 }

 }
 }

This code throws a nullreference exception on this line doc.InsertParagraph();

May 29, 2012 at 11:33 AM

Are you using newest source code compiled?

May 29, 2012 at 12:06 PM
Edited May 29, 2012 at 12:28 PM

I am using the DLL downloaded from the download page

Just compiled the latest source and I am still getting the errors

May 30, 2012 at 5:30 PM

Hello guys, any clue with regards this?

May 30, 2012 at 5:33 PM

I will take a look as soon as I get some time. Try to create/edit document on your hdd first and see if that works fine. I presume it doesn't work locally as well with your code.. or does it ?

May 30, 2012 at 6:08 PM

Hello thanks for the prompt reply

 

It does work on the local machine. Also I have updated the code and added these method which I omitted previously

 

string siteUrl = "http://moss1";

 using (SPSite spSite = new SPSite(siteUrl))
 {

 Console.WriteLine("Querying for doc1.docx");
 SPList list = spSite.RootWeb.Lists["Test"];
 SPQuery query = new SPQuery();
 query.ViewFields = @"<FieldRef Name='FileLeafRef' />";
 query.Query =
 @"<Where>
 <Eq>
 <FieldRef Name='FileLeafRef' />
 <Value Type='Text'>doc1.docx</Value>
 </Eq>
 </Where>";
 SPListItemCollection collection = list.GetItems(query);
 if (collection.Count != 1)
 {
 Console.WriteLine("doc1.docx not found");
 Environment.Exit(0);
 }
 Console.WriteLine("Opening");
 SPFile file = collection[0].File;
 byte[] byteArray = file.OpenBinary();
 

 

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

         using (var doc = DocX.Load(memStr))

{

	//Your code here
	//Once you finished working save it back to the stream
              // Get the default Header for this document.

doc.AddHeaders();(omitted previously)
         doc.AddFooters();(omitted previously)
    Header header_default = doc.Headers.even;
 
    //// Get the default Footer for this document.
    //Footer footer_default = doc.Footers.even;

    // Insert a Paragraph into the default Header.
    Paragraph p1 = header_default.InsertParagraph();

    p1.Append("Hello Header.").Bold();

    // Insert a Paragraph into the document.
    Paragraph p2 = doc.InsertParagraph();
    p2.AppendLine("Hello Document.").Bold();
 
    //// Insert a Paragraph into the default Footer.
    //Paragraph p3 = footer_default.InsertParagraph();
    //p3.Append("Hello Footer.").Bold();
	doc.Save();
	
}
     
 Console.WriteLine("Saving");
 string linkFileName = file.Item["LinkFilename"] as string;
 file.ParentFolder.Files.Add(linkFileName, memStr, true);
 }

 }
 }

Now at this point, I don't have the exception anymore,however every other line of code works as expected accept for the ones for the headers and footers.

May 30, 2012 at 6:11 PM

So if you save it locally it saves Header/Footers ok. If you save it back to SharePoint no headers and footers are saved but everything else is?

May 30, 2012 at 7:10 PM

ok guys I think I have it sorted now

 

updated code below:

 

string siteUrl = "http://moss";

 using (SPSite spSite = new SPSite(siteUrl))
 {

 Console.WriteLine("Querying for doc.docx");
 SPList list = spSite.RootWeb.Lists["Test"];
 SPQuery query = new SPQuery();
 query.ViewFields = @"<FieldRef Name='FileLeafRef' />";
 query.Query =
 @"<Where>
 <Eq>
 <FieldRef Name='FileLeafRef' />
 <Value Type='Text'>doc.docx</Value>
 </Eq>
 </Where>";
 SPListItemCollection collection = list.GetItems(query);
 if (collection.Count != 1)
 {
 Console.WriteLine("Doc.docx not found");
 Environment.Exit(0);
 }
 Console.WriteLine("Opening");
 SPFile file = collection[0].File;
 byte[] byteArray = file.OpenBinary();




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

     using (var doc = DocX.Load(memStr))
     {

         //Your code here
         //Once you finished working save it back to the stream
         // Get the default Header for this document.
         doc.AddHeaders();
         doc.AddFooters();
         // Force the first page to have a different Header and Footer.
        // doc.DifferentFirstPage=true;

         // Force odd & even pages to have different Headers and Footers.
         //doc.DifferentOddAndEvenPages =true;

         Header header_default = doc.Headers.first;
         Header header_odd = doc.Headers.odd;
         Header header_even = doc.Headers.even;

         //// Get the default Footer for this document.
         Footer footer_default = doc.Footers.first;
         Footer footer_odd = doc.Footers.odd;
         Footer footer_even = doc.Footers.even;


         // Insert a Paragraph into the default Header.
         Paragraph p1 = header_default.InsertParagraph();

         p1.Append("Hello Header.").Bold();
         //insert a paragraph into the odd header
         Paragraph p4 = header_odd.InsertParagraph();

         p4.Append("Hello Header odd.").Bold();

         //insert a paragraph into the even header
         Paragraph p5 = header_even.InsertParagraph();

         p5.Append("Hello Header even.").Bold();


         // Insert a Paragraph into the document.
         Paragraph p2 = doc.InsertParagraph();
         p2.AppendLine("Hello Document.").Bold();

         //// Insert a Paragraph into the default Footer.
         Paragraph p3 = footer_default.InsertParagraph();
         p3.Append("Hello Footer.").Bold();

         //// Insert a Paragraph into the default Footer.
         Paragraph p6 = footer_odd.InsertParagraph();
         p6.Append("Hello Footer odd.").Bold();
        // p2.InsertPageBreakAfterSelf();
         doc.Save();

     }
     
 Console.WriteLine("Saving");
 string linkFileName = file.Item["LinkFilename"] as string;
 file.ParentFolder.Files.Add(linkFileName, memStr, true);
 }

Now I can effectively add text headers and footers, now if I need to add image header how do I go about this? That is really where I am getting to. Also the images will be sourced from a SharePoint library too.

 

Thanks

May 31, 2012 at 8:26 AM

Here you go. You can modify it per your need. Of course the relevant part "get" from sharepoint is on your side :-)

 

                    #region Header
                    Image header;
                    if (rTyp.ToLower() == "Raport".ToLower()) {
                        Image header_miesieczny = documentWord.AddImage(Assembly.GetExecutingAssembly().GetManifestResourceStream("CRM.Resources.image_header.jpg1"));
                        header = header_miesieczny;
                    } else {
                        Image header_kwartalny = documentWord.AddImage(Assembly.GetExecutingAssembly().GetManifestResourceStream("CRM.Resources.image_header2.jpg"));
                        header = header_kwartalny;
                    }
                    Picture headPicture = header.CreatePicture();
                    headPicture.Height = 161;
                    headPicture.Width = 610;
                    documentWord.AddHeaders();
                    Header headerOdd = documentWord.Headers.odd;
                    Header headerFirst = documentWord.Headers.first;
                    Paragraph picture_header_odd = headerOdd.Paragraphs[0].AppendPicture(headPicture);
                    Paragraph picture_header_first = headerFirst.Paragraphs[0].AppendPicture(headPicture);
                    //picture_header_first.AppendPicture(headPicture);
                    //picture_header_odd.AppendPicture(headPicture);
                    #endregion Header