I am trying to load the template file my SPDocumentLibrary has set, do some transforms in OpenXML then save it back to the library as a document. I think I have all the steps down except reading the template. I don’t know the “correct” way to open it (I could just use a WebClient to download it but it makes me feel dirty just typing that). Here is what I have so far.
public string GetOrGenerateChecklist(string PracticeName, string ContractID, string EducationDate, string MainContactInfo, string Address)
{
using (SPWeb web = SPContext.Current.Web)
{
SPDocumentLibrary list = (SPDocumentLibrary)web.Lists["Educator Checklists"];
var templetAddr = String.Concat(web.Url, '/', list.DocumentTemplateUrl);
SPQuery query = new SPQuery();
query.Query = string.Concat(
//Snip
);
var items = list.GetItems(query);
//if document exists return existing document.
if (items.Count > 0)
return String.Concat(web.Url, "/Educator Checklists/", PracticeName, " - ", ContractID, ".docx");
MemoryStream documentStream;
//copy the stream to memory
using (Stream tplStream = ????????) //<------- my problem
{
documentStream = new MemoryStream((int)tplStream.Length);
CopyStream(tplStream, documentStream);
documentStream.Position = 0L;
}
using (WordprocessingDocument template = WordprocessingDocument.Open(documentStream, true))
{
template.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
MainDocumentPart mainPart = template.MainDocumentPart;
mainPart.DocumentSettingsPart.AddExternalRelationship(
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate",
new Uri(templetAddr, UriKind.Absolute));
ReplaceText(mainPart, "#PracticeName#", PracticeName);
ReplaceText(mainPart, "#EducationDate#", EducationDate);
ReplaceText(mainPart, "#MainContactInfo#", MainContactInfo);
ReplaceText(mainPart, "#Address#", Address);
}
documentStream.Position = 0L;
list.RootFolder.Files.Add(String.Concat(PracticeName, " - ", ContractID, ".docx"), documentStream);
return String.Concat(web.Url, "/Educator Checklists/", PracticeName, " - ", ContractID, ".docx");
}
}
So my question is how do i get the template stored at templetAddr loaded in to the memory stream?
Also I am fairly new to sharepoint so if you see any other big mistakes I have made please let me know.
http://msdn.microsoft.com/en-us/library/ms476063.aspx (SPWeb.GetFile)
http://msdn.microsoft.com/en-us/library/ms470901.aspx (SPFile.OpenBinaryStream)