예제 #1
0
 public void SetAPIConnection(Sitecore61.VisualSitecoreService sitecoreApi, Sitecore61.Credentials credentials)
 {
     _sitecoreApi = sitecoreApi;
     _credentials = credentials;
 }
예제 #2
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;
        }