Ejemplo n.º 1
0
        public JsonResult ExtractContent(int nodeid, string sourceLanguage, string targetLanguage, bool includeSubNodes)
        {
            if (nodeid > -1)
            {
                // Get the unpublished version of the current node.
                var content = this.Services.ContentService.GetById(nodeid);

                // Convert it to an IPublishedContent. So this IPublishedContent has the unpublished version.
                var publishedContent = content.ToPublishedContent();

                var collection = new DocumentCollection
                {
                    Id             = nodeid,
                    Name           = publishedContent.Name,
                    SourceLanguage = sourceLanguage,
                    TargetLanguage = targetLanguage
                };


                // Export the Umbraco properties to a document.
                if (!includeSubNodes)
                {
                    var document = publishedContent.ExtractForExport(sourceLanguage);

                    collection.Documents.Add(document);
                }
                else
                {
                    // Things like DescendantsOrSelf() just work even though this IPublishedContent could be unpublished.
                    var publishedContentWithDescendants = publishedContent.DescendantsOrSelf().ToList();

                    foreach (var publishedContentDescendant in publishedContentWithDescendants)
                    {
                        var document = publishedContentDescendant.ExtractForExport(sourceLanguage);

                        collection.Documents.Add(document);
                    }
                }

                // Serialize the document to xml. This could be used so the content can be exported.
                var xml = collection.SerializeObject();

                return(new JsonResult {
                    Data = new { Error = string.Empty, Xml = xml }
                });
            }

            return(new JsonResult {
                Data = new { Error = "No node id" }
            });
        }