Ejemplo n.º 1
0
        /// <summary>
        /// Change a templates inheritance from another template
        /// returns TemplateItem
        /// </summary>
        private Sitecore6xItem ChangeTemplateInheritance(Sitecore6xItem templateItem, string sTemplatePath, string sBaseTemplatePath)
        {
            // Get standard template
            XmlNode baseTemplate = GetItemXml(sBaseTemplatePath);
            string sBaseTemplateID = baseTemplate.Attributes["id"].Value;

            templateItem._fields.Add(new Sitecore6xField("__base template", "__base template", "tree list", new Guid("{12C33F3F-86C5-43A5-AEB4-5598CEC45116}"), sBaseTemplateID, null, ""));

            // Return changed template item
            return templateItem;
        }
Ejemplo n.º 2
0
        private void CopyChildren(Sitecore6xItem parentItem, IItem CopyFrom)
        {
            if (Util.backgroundWorker.CancellationPending)
                return;

            IItem[] sourceChildren = CopyFrom.GetChildren();
            foreach (IItem child in sourceChildren)
            {
                if (Util.backgroundWorker.CancellationPending)
                    return;
                try
                {
                    Sitecore6xItem newItem = parentItem.CopyItemTo(child);

                    // recursively copy children
                    if (newItem != null)
                        CopyChildren(newItem, child);
                }
                catch (Exception ex)
                {
                    if (Util.ExceptionContainsMessage(ex, "Unknown item"))
                    {
                        Util.AddWarning("Found an 'Unknown item' at: " + child.Path + ", the item is probably missing its template.");
                        continue;
                    }
                    else
                        throw new Exception("Error while copying from: " + child.Path, ex);
                }
            }
        }
Ejemplo n.º 3
0
 private void AddItemToCache(Sitecore6xItem item)
 {
     // Add to cache two times because we might request it by key or by path
     if (!_Options.ExistingTemplates.ContainsKey(item.ID.ToLower()))
         _Options.ExistingTemplates.Add(item.ID.ToLower(), item);
     if (!_Options.ExistingTemplates.ContainsKey(item.Path.ToLower()))
         _Options.ExistingTemplates.Add(item.Path.ToLower(), item);
 }
Ejemplo n.º 4
0
        private Sitecore6xItem AddFieldFromTemplate(Sitecore6xItem templateFieldItem, string sFieldTemplate, string sContent)
        {
            string sName = sFieldTemplate.Remove(0, sFieldTemplate.LastIndexOf("/") + 1);
            string sKey = sFieldTemplate.Remove(0, sFieldTemplate.LastIndexOf("/") + 1).ToLower();
            // Field already exist, just replace content
            IField existingField = Util.GetFieldByName(sName, templateFieldItem.Fields);
            if (existingField != null)
                existingField.Content = sContent;
            else
            {
                // Get sType field template
                XmlNode field = GetItemXml(sFieldTemplate);
                string sTemplateFieldID = field.Attributes["id"].Value;
                string sfieldTypename = field.SelectSingleNode("//item/version[@language = '" + this.Options.Language + "']/fields/field[@key='type']").InnerText;

                templateFieldItem._fields.Add(new Sitecore6xField(sName, sKey, sfieldTypename, new Guid(sTemplateFieldID), sContent, null, ""));
            }

            return templateFieldItem;
        }
Ejemplo n.º 5
0
        private Sitecore6xItem(XmlNode itemNode, Sitecore6xItem parent, Sitecore61.VisualSitecoreService sitecoreApi, Sitecore61.Credentials credentials, ConverterOptions Options, bool bAllFields = false)
        {
            _Parent = parent;
            _sitecoreApi = sitecoreApi;
            _credentials = credentials;
            _Options = Options;

            _ID = new Guid(itemNode.Attributes["id"].Value);
            _sName = itemNode.Attributes["name"].Value;
            _sKey = itemNode.Attributes["key"].Value;
            _TemplateID = new Guid(itemNode.Attributes["tid"].Value);
            _sTemplateName = itemNode.Attributes["template"].Value;
            // If basetemplate isn't defined use _TemplateID otherwise extract list of inherited templates
            XmlNode baseTemplateNode = itemNode.SelectSingleNode("//field[@key='__base template']/content");
            if (baseTemplateNode == null)
            {
                _sTemplateIDs = new string[1];
                _sTemplateIDs[0] = Util.GuidToSitecoreID(_TemplateID);
            }
            else
                _sTemplateIDs = baseTemplateNode.InnerText.Split('|');

            // _sParentID = itemNode.Attributes["parentid"].Value;
            _sSortOrder = itemNode.Attributes["sortorder"].Value;
            if ((itemNode.Attributes["haschildren"] != null) && (itemNode.Attributes["haschildren"].Value == "1"))
                _bHasChildren = true;

            _sPath = "";
            string sItemVersion = GetItemVersion(itemNode);
            XmlNode contentNode = _sitecoreApi.GetItemFields(Util.GuidToSitecoreID(_ID), this.Options.Language, sItemVersion, bAllFields, Util.CurrentDatabase, _credentials);
            XmlNodeList paths = contentNode.SelectNodes("/path/item");
            foreach (XmlNode path in paths)
            {
                if (_sPath == "")
                    _sPath = "/" + path.Attributes["name"].Value;
                else
                    _sPath = "/" + path.Attributes["name"].Value + _sPath;
            }

            if (bAllFields)
            {
                foreach (XmlNode node in contentNode.SelectNodes("field"))
                {
                    Sitecore6xField field = new Sitecore6xField(node);
                    _fields.Add(field);
                }
            }

            // Add missing fields from other language versions, this is nessesary because new templates
            // are created from an items fields.
            // The program always assumes that all fields exists, so the fieldvalues could be empty in
            // order to prevent copying to another language.
            XmlNodeList fieldList = itemNode.SelectNodes("version[@language='" + this.Options.Language + "']/fields/field");
            foreach (XmlNode node in fieldList)
            {
                Sitecore6xField field = new Sitecore6xField(node);
                var existingfields = from f in _fields
                                     where f.TemplateFieldID == field.TemplateFieldID
                                     select f;
                if (existingfields.Count() == 0)
                {
                    if ((field.Name.ToLower() == "__icon") && (field.Content.Length > 0))
                        _sIcon = field.Content;

                    if (field.Name.ToLower() == "__sortorder")
                        _sSortOrder = field.Content;

                    if ((field.Name.ToLower() == "__display name") && (field.Content != ""))
                        _sName = field.Content;

                    // Caching of template fields items
                    XmlNode templateFieldNode = null;
                    if (!_Options.ExistingTemplateFields.TryGetValue(field.TemplateFieldID, out templateFieldNode))
                    {
                        lock (_Options.ExistingTemplateFields)
                        {
                            try
                            {
                                templateFieldNode = GetItemXml(field.TemplateFieldID);
                                _Options.ExistingTemplateFields.Add(field.TemplateFieldID, templateFieldNode);
                            }
                            catch (Exception ex)
                            {
                                if (ex.Message.ToLower().IndexOf("object reference not set to an instance of an object.") > -1)
                                {
                                    // Do nothing this happens when there is content that is missing it's template field (probably the whole template)
                                    continue;
                                }
                                else
                                    throw new Exception("Error retrieving sitecore Field.", ex);
                            }
                        }
                    }

                    // Get name and field sortorder, unfortunately we have to retrieve the field item which is slow
                    field.SortOrder = templateFieldNode.Attributes["sortorder"].Value;
                    field.Name = templateFieldNode.Attributes["name"].Value;
                    // Get section from contentNode
                    XmlNode fieldNode = contentNode.SelectSingleNode("/field[@fieldid='" + field.TemplateFieldID + "']");
                    if ((fieldNode != null) && (fieldNode.Attributes["section"] != null))
                        field.Section = fieldNode.Attributes["section"].Value;

                    _fields.Add(field);
                }
            }

            // This is a template itemm
            if ((_sPath.ToLower().IndexOf("/sitecore/templates") > -1) && (_sName != "__Standard Values"))
            {
                XmlNode childrenNode = _sitecoreApi.GetChildren("{" + _ID.ToString().ToUpper() + "}", Util.CurrentDatabase, _credentials);

                XmlNodeList nodeList = childrenNode.SelectNodes("./item");
                foreach (XmlNode node in nodeList)
                {
                    // Get full-blown item
                    XmlNode item = GetItemXml(node.Attributes["id"].Value);
                    string sSection = "";
                    if (item.Attributes["template"].Value == "template section")
                    {
                        sSection = item.Attributes["name"].Value;

                        XmlNode sectionChildNodes = _sitecoreApi.GetChildren(node.Attributes["id"].Value, Util.CurrentDatabase, _credentials);
                        foreach (XmlNode tempNode in sectionChildNodes.SelectNodes("./item"))
                        {
                            XmlNode templateFieldNode = GetItemXml(tempNode.Attributes["id"].Value);

                            Sitecore6xField field = new Sitecore6xField(
                                        templateFieldNode.Attributes["name"].Value,
                                        templateFieldNode.Attributes["name"].Value.ToLower(),
                                        Util.GetNodeFieldValue(templateFieldNode, "//field[@key='type']/content"),
                                        new Guid(templateFieldNode.Attributes["id"].Value),
                                        "",
                                        Util.GetNodeFieldValue(templateFieldNode, "//field[@key='__sortorder']/content"),
                                        sSection);
                            field.Source = Util.GetNodeFieldValue(templateFieldNode, "//field[@key='source']/content");
                            field.LanguageTitle = Util.GetNodeFieldValue(templateFieldNode, "//field[@key='title']/content");

                            var existingfields = from f in _fields
                                                 where f.TemplateFieldID == field.TemplateFieldID
                                                 select f;
                            if (existingfields.Count() == 0)
                            {
                                _fields.Add(field);
                            }
                        }
                    }
                }

                // Fill template field values with standard values
                Sitecore6xItem standardValueItem = GetSitecore61Item(_sPath + "/__Standard Values");
                if (standardValueItem != null)
                {
                    foreach (Sitecore6xField standardField in standardValueItem._fields)
                    {
                        // Add field values from this and inherited templates
                        Sitecore6xField curField = Util.GetFieldByID(standardField.TemplateFieldID, Fields) as Sitecore6xField;

                        // Prevent lock from being copied, known to cause problems
                        if ((curField != null) && (curField.Name == "lock"))
                            continue;

                        if (curField == null)
                            _fields.Add(standardField);
                        else
                            curField.Content = standardField.Content;
                    }
                }
            }
            // This is a "__Standard Values" item for the current language layer
            else if ((_sPath.ToLower().IndexOf("/sitecore/templates") > -1) && (_sName == "__Standard Values"))
            {
            }

            _sXmlNode = itemNode.OuterXml;
        }
Ejemplo n.º 6
0
 public static Sitecore6xItem GetItem(string sItemPath, Sitecore61.VisualSitecoreService sitecoreApi, Sitecore61.Credentials credentials, ConverterOptions Options)
 {
     XmlNode node = sitecoreApi.GetXML(sItemPath, false, Util.CurrentDatabase, credentials);
     Sitecore6xItem item = new Sitecore6xItem(node.SelectSingleNode("//item"), null, sitecoreApi, credentials, Options);
     return item;
 }
Ejemplo n.º 7
0
        private Sitecore6xItem GetSitecore61Item(string sItemPath, Sitecore6xItem parentItem, bool bAllFields = false)
        {
            if (_Options.ExistingTemplates.ContainsKey(sItemPath.ToLower()))
            {
                IItem baseItem = null;
                _Options.ExistingTemplates.TryGetValue(sItemPath.ToLower(), out baseItem);
                return baseItem as Sitecore6xItem;
            }
            XmlNode node = _sitecoreApi.GetXML(sItemPath, false, Util.CurrentDatabase, _credentials);
            XmlNode statusNode = node.SelectSingleNode("status");
            if (statusNode.InnerText == "failed")
                return null;

            return new Sitecore6xItem(node.SelectSingleNode("//item"), parentItem, _sitecoreApi, _credentials, _Options, bAllFields);
        }