title: C# OpenXML SDK - Inserting a new slide from slide masters
body: <p>I am attempting to implement the solutions given <a href="https://stackoverflow.com/questions/32076114/c-sharp-openxml-sdk-2-5-insert-new-slide-from-slide-masters-with-the-layout/34500170">here</a> and/or <a href="https://stackoverflow.com/questions/25504305/openxml-set-a-slide-layout-for-a-slide-in-presentation">here</a>.</p>
  
  <p>I have a .pptx file that contains zero slides initially. One of the layouts is named "One content". For now, I just want to produce a new PPTX file with a single slide based on this layout. Should be trivial, no? No, apparently not.</p>
  
  <p>In file <strong>OpenXmlUtils.cs</strong> I have the following method which I use to create a new PPTX from the "template" file:</p>
  
  <pre><code>public static void CopyTemplate(string template, string target)
  {
      string targetPath = Path.GetFullPath(target);
      string targetFolder = Path.GetDirectoryName(targetPath);
      if (!System.IO.Directory.Exists(targetFolder))
      {
          System.IO.Directory.CreateDirectory(targetFolder);
      }
      System.IO.File.Copy(template, targetPath, true);
  }
  </code></pre>
  
  <p>My <strong>PPTWriter.cs</strong> broken down to MCVE:</p>
  
  <pre><code>public PPTOpenXMLWriter(string templatePath, string presSaveAsPath)
  {
      if (File.Exists(presSaveAsPath)) { File.Delete(presSaveAsPath); }
  
      OpenXmlUtils.CopyTemplate(templatePath, presSaveAsPath);
  
      _createPresentation(presSaveAsPath);
  
  }
  
  private void _createPresentation(string presSaveAsPath)
  {
      using (PresentationDocument presentationDocument = PresentationDocument.Open(presSaveAsPath, true))
      {
  
          string layoutName = "One content";
  
          _insertNewSlide(presentationDocument.PresentationPart, layoutName);
  
          presentationDocument.Save();
      }
  }    
  
  private void _insertNewSlide(PresentationPart presentationPart, string layoutName)
  {
      Slide slide = new Slide(new CommonSlideData(new ShapeTree()));
      SlidePart slidePart = presentationPart.AddNewPart&lt;SlidePart&gt;();
      slide.Save(slidePart);
      SlideMasterPart slideMasterPart = presentationPart.SlideMasterParts.FirstOrDefault();
      SlideLayoutPart slideLayoutPart = slideMasterPart.SlideLayoutParts.SingleOrDefault
              (sl =&gt; sl.SlideLayout.CommonSlideData.Name.Value.Equals(layoutName, StringComparison.OrdinalIgnoreCase));
      slidePart.AddPart&lt;SlideLayoutPart&gt;(slideLayoutPart);
      slidePart.Slide.CommonSlideData = (CommonSlideData)slideLayoutPart.SlideLayout.CommonSlideData.Clone();
  
      SlideIdList slideIdList = null;
      if ( presentationPart.Presentation.SlideIdList is null)
      {
          presentationPart.Presentation.SlideIdList = new SlideIdList();
      }
      slideIdList = presentationPart.Presentation.SlideIdList;
      // find the highest id
      uint maxSlideId = 0;
      if (slideIdList.ChildElements.Count() &gt; 0)
          maxSlideId = slideIdList.ChildElements
              .Cast&lt;SlideId&gt;()
              .Max(x =&gt; x.Id.Value);
  
      // Insert the new slide into the slide list after the previous slide.
      SlideId newSlideId = new SlideId();
      slideIdList.Append(newSlideId);
      newSlideId.Id = maxSlideId;
      newSlideId.RelationshipId = presentationPart.GetIdOfPart(slidePart);
  
      // Save the modified presentation.
      presentationPart.Presentation.Save();
  }
  </code></pre>
  
  <p>The resulting file is corrupt and needs to be "repaired" by PowerPoint, after which repair process the slide layout is <strong>not</strong> the layout that was specified. In fact it's a completely different layout with a radically different XML structure and all I can gather is that it's somehow defaulting back to the ordinally <em>first</em> layout in the master ("Title"), because it doesn't know how to handle whatever it's actually been given via OpenXML.</p>
  
  <p>This seems like it ought to be a fairly common use-case, and perhaps my expectations are wrong, but it seems like given an <em>already existing</em> slide layout, you ought to be able to (relatively easily) create a new slide <em>based on that layout</em> which will contain all of the same placeholder shapes, etc.</p>