public static ComponentTemplateDocumentData GetComponentTemplateData(ILocalClient client, string id)
        {
            ComponentTemplateData item = client.Read(id, new ReadOptions()) as ComponentTemplateData;

            if (item == null)
                return null;

            ComponentTemplateDocumentData componentTemplate = new ComponentTemplateDocumentData();
            componentTemplate.Title = item.Title;
            componentTemplate.TemplateType = item.TemplateType;
            componentTemplate.LinkedSchemas = item.RelatedSchemas.Select(x => new SchemaDocumentData {Title = x.Title}).ToList();
            componentTemplate.MetadataSchema = new SchemaDocumentData {Title = item.MetadataSchema.Title};
            componentTemplate.OutputFormat = item.OutputFormat;
            componentTemplate.Priority = item.Priority;
            componentTemplate.Dynamic = item.DynamicTemplate.ToLower() == "dynamic";
            componentTemplate.InlineEditing = item.IsEditable;

            foreach (TbbInfo tbbInfo in Functions.GetTbbList(item.Content))
            {
                TbbDocumentData tbbDocument = TBBHelper.GetTBBData(client, tbbInfo.TcmId);
                componentTemplate.TBBs.Add(tbbDocument);
            }

            return componentTemplate;
        }
예제 #2
0
        public byte[] CreateComponentTemplateDocument(ComponentTemplateDocumentData ctData, string templateLocation)
        {
            byte[] binFile = null;

            byte[] byteArray = File.ReadAllBytes(templateLocation);
            using (MemoryStream mem = new MemoryStream())
            {
                mem.Write(byteArray, 0, byteArray.Length);

                using (WordprocessingDocument doc = WordprocessingDocument.Open(mem, true))
                {
                    Paragraph header = doc.MainDocumentPart.Document.Body.GetFirstChild<Paragraph>();
                    header.ChildElements.First(x => x is Run).Append(new Text(String.Format(" \"{0}\"", ctData.Title)));

                    Table table = doc.MainDocumentPart.Document.Body.GetFirstChild<Table>();

                    RunFonts textFonts = table.GetFirstChild<TableRow>().GetFirstChild<TableCell>().GetFirstChild<Paragraph>().
                            GetFirstChild<ParagraphProperties>().GetFirstChild<ParagraphMarkRunProperties>().
                            GetFirstChild<RunFonts>();
                    string paragraphPropertiesOuterXml = getParagraphPropertiesXml(textFonts);
                    string runPropertiesOuterXml = getRunPropertiesXml(textFonts);

                    table.Elements<TableRow>().ElementAt(0).Elements<TableCell>().ElementAt(1).Elements<Paragraph>().ElementAt(0).Append(new Run(new RunProperties(runPropertiesOuterXml), new Text(ctData.Title)));
                    table.Elements<TableRow>().ElementAt(1).Elements<TableCell>().ElementAt(1).Elements<Paragraph>().ElementAt(0).Append(new Run(new RunProperties(runPropertiesOuterXml), new Text(ctData.TemplateType)));
                    table.Elements<TableRow>().ElementAt(2).Elements<TableCell>().ElementAt(1).Elements<Paragraph>().ElementAt(0).Append(new Run(new RunProperties(runPropertiesOuterXml), new Text(String.Join(", ", ctData.LinkedSchemas.Select(x => x.Title)))));
                    table.Elements<TableRow>().ElementAt(3).Elements<TableCell>().ElementAt(1).Elements<Paragraph>().ElementAt(0).Append(new Run(new RunProperties(runPropertiesOuterXml), new Text(ctData.MetadataSchema.Title)));
                    table.Elements<TableRow>().ElementAt(4).Elements<TableCell>().ElementAt(1).Elements<Paragraph>().ElementAt(0).Append(new Run(new RunProperties(runPropertiesOuterXml), new Text(ctData.OutputFormat)));
                    table.Elements<TableRow>().ElementAt(5).Elements<TableCell>().ElementAt(1).Elements<Paragraph>().ElementAt(0).Append(new Run(new RunProperties(runPropertiesOuterXml), new Text(ctData.Priority.ToString())));
                    table.Elements<TableRow>().ElementAt(6).Elements<TableCell>().ElementAt(1).Elements<Paragraph>().ElementAt(0).Append(new Run(new RunProperties(runPropertiesOuterXml), new Text(ctData.Dynamic == true ? TEXT_YES : TEXT_NO)));
                    table.Elements<TableRow>().ElementAt(7).Elements<TableCell>().ElementAt(1).Elements<Paragraph>().ElementAt(0).Append(new Run(new RunProperties(runPropertiesOuterXml), new Text(ctData.InlineEditing == true ? TEXT_YES : TEXT_NO)));

                    // Clear empty rows
                    foreach (TableRow row in table.Elements<TableRow>())
                    {
                        Text text = row.Elements<TableCell>().ElementAt(1).GetFirstChild<Paragraph>().GetFirstChild<Run>().GetFirstChild<Text>();

                        if (string.IsNullOrEmpty(text.InnerText.Trim()))
                        {
                            table.RemoveChild(row);
                        }
                    }

                    foreach (TbbDocumentData tbbData in ctData.TBBs)
                    {
                        TableRow tr = new TableRow();

                        TableCell tc1 = new TableCell();
                        tc1.Append(new Paragraph(new ParagraphProperties(paragraphPropertiesOuterXml), new Run(new RunProperties(runPropertiesOuterXml), new Text(tbbData.Title))));
                        tr.Append(tc1);

                        TableCell tc2 = new TableCell();
                        tc2.Append(new Paragraph(new ParagraphProperties(paragraphPropertiesOuterXml), new Run(new RunProperties(runPropertiesOuterXml), new Text(tbbData.TbbType))));
                        tr.Append(tc2);

                        TableCell tc3 = new TableCell();
                        //tc3.Append(new Paragraph(new ParagraphProperties(paragraphPropertiesOuterXml), new Run(new RunProperties(runPropertiesOuterXml), new Text(tbbData.FieldType))));
                        tr.Append(tc3);

                        table.Append(tr);
                    }
                }

                binFile = mem.ToArray();
            }

            return binFile;
        }