/// <summary>
        /// Gets an imported template's content. Works with tcm uri's, web dav urls, or physical file paths.
        /// </summary>
        /// <param name="path">The path to check.</param>
        /// <param name="engine"></param>
        /// <returns></returns>
        private string GetImportTemplateContent(string path)
        {
            TcmUri templateID = new TcmUri(_templateID);

            if (path.ToLower().StartsWith("tcm:") || path.ToLower().StartsWith("/webdav/") || !path.Contains("\\"))
            {
                if (!path.ToLower().StartsWith("tcm:") && !path.ToLower().StartsWith("/webdav/"))
                {
                    path = GetRelativeImportPath(path);
                }

                TemplateBuildingBlock template;
                try
                {
                    template = Session.GetObject(path) as TemplateBuildingBlock;
                }
                catch (Exception ex)
                {
                    _logger.Warning("Error import of '" + path + "'. " + ex.ToString());
                    return(String.Empty);
                }

                if (template == null)
                {
                    _logger.Warning("Import of '" + path + "' not found.");
                    return(String.Empty);
                }

                _logger.Debug("Comaring import template " + template.Id + " to razor tbb ID " + templateID);
                // Get local copy of the imported template if possible.

                int publicationID = templateID.PublicationId;

                if (TcmUri.IsNullOrUriNull(templateID))
                {
                    // Is new item, so templateID is tcm:0-0-0.  We need to grab the pub id manually.
                    string[] webDav    = _webDavUrl.Split('/');
                    string   pubWebDav = "/webdav/" + webDav[2];

                    publicationID = Session.GetObject(pubWebDav).Id.ItemId;
                }

                if (template.Id.PublicationId != publicationID)
                {
                    // If import is from diff publication, try to grab local copy.
                    try
                    {
                        template = (TemplateBuildingBlock)Session.GetObject(TemplateUtilities.CreateTcmUriForPublication(publicationID, template.Id));
                    }
                    catch
                    {
                        _logger.Warning("Error trying to get local copy of template '" + template.Id + "' for Publication ID '" + publicationID + "'");
                    }
                }

                // Don't import itself
                if (template.Id.GetVersionlessUri().Equals(templateID.GetVersionlessUri()))
                {
                    return(String.Empty);
                }

                return(template.Content);
            }
            else
            {
                // If its a file path, get the contents of the file and return.
                return(File.ReadAllText(path));
            }
        }
Example #2
0
        public T GetContextIdentifiableObject <T>(TcmUri tcmUri) where T : IdentifiableObject
        {
            var contextTcmUri = TemplateUtilities.CreateTcmUriForPublication(this.Publication.Id.ItemId, tcmUri);

            return(this.Engine.GetObject <T>(contextTcmUri));
        }