Exemplo n.º 1
0
        private TcmUri GetTcmUri(string fieldName, ItemFields fields, TcmUri contextPublicationUri)
        {
            TcmUri uri = GetTcmUriFromField(fieldName, fields);

            if (!TcmUri.IsNullOrUriNull(contextPublicationUri))
            {
                uri = TransformTcmUri(uri, contextPublicationUri);
            }

            if (TcmUri.IsNullOrUriNull(uri))
            {
                IsValid = false;
            }

            return(uri.GetVersionlessUri());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get TCM URI from item (text) field using context Publication id
        /// </summary>
        /// <param name="fieldName">the XML field name</param>
        /// <param name="fields">ItemFields collection</param>
        /// <param name="contextPublicationId">ID for the context Publication</param>
        /// <returns>TcmUri object from field value using context Publication or UriNull if not parsable or empty</returns>
        public static TcmUri GetTcmUriFromField(string fieldName, ItemFields fields, int contextPublicationId)
        {
            TcmUri uri = GetTcmUriFromField(fieldName, fields);

            if (!TcmUri.IsNullOrUriNull(uri))
            {
                try
                {
                    uri = new TcmUri(uri.ItemId, uri.ItemType, contextPublicationId);
                }
                catch (InvalidTcmUriException ex)
                {
                    Logger.Write(ex, Name, LoggingCategory.General, TraceEventType.Error);
                }
            }

            return(uri.GetVersionlessUri());
        }
        /// <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));
            }
        }