open xml 2.0 add text in sharepoint 2010 template

gogi100 51 Reputation points
2023-10-16T11:15:36.7433333+00:00

i have sharepoint 2010 event receiver the itemadded that use function. this function use existing word document template in which add some text after specific bookmarks, but i have a problem, i cannot see added text. why? i added bookmarks 'imenovnjane' and 'izvrsilacko' in the template

i need help

the code is

public bool CreateOdluka(string sFilenameO, string sContentTypeO, string sList, string id, string pozicija, string brodluke, string vrstakonkursa, string mesec, string datumodluke, string mestorada, string radnomesto, string sektor, string brojizvrsioca, string godina, string vrstaradnogmesta, string url)
       {
           try
           {
               SPSite site = new SPSite(url);
               using (SPWeb web = site.OpenWeb())
               {


                   SPList list = web.Lists[sList];
                   // this always uses root folder
                   SPFolder folder = list.RootFolder;
                   SPFileCollection fcol = folder.Files;

                   // find the template url and open
                   string sTemplate = list.ContentTypes[sContentTypeO].DocumentTemplateUrl;
                   SPFile spf = web.GetFile(sTemplate);
                   byte[] binFile = spf.OpenBinary();
                   // Url for file to be created
                   string destFile = fcol.Folder.Url + "/" + sFilenameO;

                   // create the document and get SPFile/SPItem for 
                   // new document
                   SPFile addedFile = fcol.Add(destFile, binFile);
                   SPItem newItem = addedFile.Item;
                   newItem["Број писарница(Одлука)"] = brodluke;
                   newItem["Број рм"] = brojizvrsioca;
                   newItem["Сектор"] = sektor;
                   newItem["Врста конкурса"] = vrstakonkursa;
                   newItem["Врста радног места"] = vrstaradnogmesta;
                   newItem["Година"] = godina;
                   newItem["Датум писарница(Одлука)"] = Convert.ToDateTime(datumodluke.ToString());
                   newItem["Месец"] = mesec;
                   newItem["Место рада"] = mestorada;
                   newItem["Позиција"] = pozicija;
                   newItem["Радно место"] = radnomesto;
                  
                   newItem.Update();
                   addedFile.Update();
                   SPQuery query = new SPQuery();
                   query.Query = "<Where><Eq>" + "<FieldRef Name='FileLeafRef' />" + "<Value Type='Text'>" + sFilenameO + "</Value>" + "</Eq>" + "</Where>";

                   SPListItemCollection collection = list.GetItems(query);
                   SPFile file = collection[0].File;
                   byte[] byteArray = file.OpenBinary();
                   using (MemoryStream memStr = new MemoryStream())
                   {
                       memStr.Write(byteArray, 0, byteArray.Length);

                       //stand up object that reads the Word doc package
                       using (WordprocessingDocument doc = WordprocessingDocument.Open(memStr, true))
                       {
                         
                           BookmarkStart bookmark= null;
                                       
                           MainDocumentPart main = doc.MainDocumentPart;
                           if (vrstaradnogmesta == "именовање")
                           {
                               var res = from bm in main.Document.Body.Descendants<BookmarkStart>() where bm.Name == "imenovanje" select bm;
                                   bookmark = res.SingleOrDefault();
                           }
                           else
                           {
                               var res = from bm in main.Document.Body.Descendants<BookmarkStart>() where bm.Name == "izvrsilacko" select bm;
                                   bookmark = res.SingleOrDefault();
                               
                           }
                           if (bookmark != null)
                           {
                                  
                               
                               var parent = bookmark.Parent;   // bookmark's parent element
                               
                               // Construct the Text element with your text content
                                 Text text = new Text("- Радно место " + pozicija.ToString() + ", звање " + radnomesto.ToString() + ", са местом рада у " + mestorada.ToString() + ", " + brojizvrsioca.ToString() + " извршилаца");

                               // Construct the Run element with formatting
                                 Run run = new Run();
                                 run.Append(new RunProperties(new Bold(), new Indentation() { FirstLine = "10" }));
                                  run.Append(text);

                               // Construct the Paragraph with the Run
                               Paragraph newParagraph = new Paragraph();
                                 newParagraph.Append(new ParagraphProperties(new Indentation() { FirstLine = "10" }));
                                newParagraph.Append(run);

                               // Insert the new Paragraph after the bookmark's parent element
                               parent.InsertAfterSelf(newParagraph);
                           
                                                            
                           }
                               

                           main.Document.Save();
                           doc.Close();
                           //closing WordprocessingDocument automatically saves the document
                       }
                       // Save the changes to the table back into the document.


                   }
                   //OpenXml kreiranje dokumenata - kraj
                   return true;

               }
           }
           catch (SPException spEx)
           {
               // file already exists?
               if (spEx.ErrorCode == -2130575257)
                   return false;
               else
                   throw spEx;
           }


       }
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,720 questions
{count} votes