public static Sitecore5xItem GetItem(string sItemPath, Sitecore5x.VisualSitecoreService sitecoreApi, Sitecore5x.Credentials credentials, ConverterOptions Options)
        {
            XmlNode        node = sitecoreApi.GetXML(sItemPath, false, Options.Database, credentials);
            Sitecore5xItem item = new Sitecore5xItem(node.SelectSingleNode("//item"), null, sitecoreApi, credentials, Options);

            return(item);
        }
 public void SetAPIConnection(Sitecore5x.VisualSitecoreService sitecoreApi, Sitecore5x.Credentials credentials)
 {
     _sitecoreApi = sitecoreApi;
     _credentials = credentials;
 }
        private Sitecore5xItem(XmlNode itemNode, IItem parent, Sitecore5x.VisualSitecoreService sitecoreApi, Sitecore5x.Credentials credentials, ConverterOptions Options)
        {
            _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];
                // If template is "template" then there is an error in sitecore webservice and
                // we change it to "Standard template".
                if (Util.GuidToSitecoreID(_TemplateID) == "{AB86861A-6030-46C5-B394-E8F99E8B87DB}")
                {
                    _sTemplateIDs[0] = "{1930BBEB-7805-471A-A3BE-4858AC7CF696}";
                }
                else
                {
                    _sTemplateIDs[0] = Util.GuidToSitecoreID(_TemplateID);
                }
            }
            else
            {
                _sTemplateIDs = baseTemplateNode.InnerText.Split('|');
            }

            // _sParentID = itemNode.Attributes["parentid"].Value;

            _sSortOrder = Util.GetNodeFieldValue(itemNode, "//field[@key='__sortorder']/content");
            if ((itemNode.Attributes["haschildren"] != null) && (itemNode.Attributes["haschildren"].Value == "1"))
            {
                _bHasChildren = true;
            }

            _sPath = "";
            string      sLatestVersion = "1";
            XmlNodeList versions       = itemNode.SelectNodes("//version[@language = '" + this.Options.Language + "']");

            if (versions != null && versions.Count > 0)
            {
                sLatestVersion = versions[versions.Count - 1].SelectSingleNode("@version").Value;
            }

            XmlNode     contentNode = _sitecoreApi.GetItemFields(Util.GuidToSitecoreID(_ID), this.Options.Language, sLatestVersion, false, _Options.Database, _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;
                }
            }


            // 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 + "' and @version = '" + sLatestVersion + "']/fields/field");

            foreach (XmlNode node in fieldList)
            {
                Sitecore5xField field          = new Sitecore5xField(node);
                var             existingfields = from f in _fields
                                                 where f.TemplateFieldID == field.TemplateFieldID
                                                 select f;
                if (existingfields.Count() == 0)
                {
                    // The content is cleared because it is from another language layer
                    //                    field.Content = "";

                    if (field.Name.ToLower() == "__icon")
                    {
                        _sIcon = field.Content;
                    }

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

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

                    // Skip security field, it is copied in the extended sitecoreApi
                    if (field.Name.ToLower() == "__security")
                    {
                        continue;
                    }

                    // Skip standard values, it's set in the extended sitecoreApi
                    if (field.Name.ToLower() == "__standard values")
                    {
                        continue;
                    }


                    // Caching of template fields items
                    XmlNode templateFieldNode = null;
                    if (!_Options.ExistingTemplateFields.TryGetValue(field.TemplateFieldID, out templateFieldNode))
                    {
                        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);
                            }
                        }
                    }
                    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 item
            if ((_sPath.ToLower().IndexOf("/sitecore/templates") > -1) && (_sName != "__Standard Values"))
            {
                XmlNode childrenNode = _sitecoreApi.GetChildren("{" + _ID.ToString().ToUpper() + "}", _Options.Database, _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, _Options.Database, _credentials);
                        foreach (XmlNode tempNode in sectionChildNodes.SelectNodes("./item"))
                        {
                            XmlNode templateFieldNode = GetItemXml(tempNode.Attributes["id"].Value);

                            Sitecore5xField field = new Sitecore5xField(
                                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);
                            }
                        }
                    }
                    // Clear template items values, will be fille later with standard values
//                    foreach (Sitecore5xField field in _fields)
//                        field.Content = "";
                }

                // Fill template field values with standard values
                Sitecore5xItem standardValueItem = GetSitecore5xItem(_sPath + "/__Standard Values");
                if (standardValueItem != null)
                {
                    foreach (Sitecore5xField standardField in standardValueItem._fields)
                    {
                        // Add field values from this and inherited templates
                        Sitecore5xField curField = Util.GetFieldByID(standardField.TemplateFieldID, Fields) as Sitecore5xField;
                        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;
        }