private void FindChildNodes(WebSiteNode node)
        {
            InitiateProvider();

            try
            {
                SPQuery query = new SPQuery();
                query.Query      = "<Where><Eq><FieldRef Name=\"Parent\" /><Value Type=\"Lookup\">" + node.Title + "</Value></Eq></Where>";
                query.ViewFields = "<FieldRef Name=\"Title\" /><FieldRef Name=\"Description\" /><FieldRef Name=\"URL\" /><FieldRef Name=\"Enabled\" /><FieldRef Name=\"Selected\" /><FieldRef Name=\"Breadcrumb\" /><FieldRef Name=\"Separator\" /><FieldRef Name=\"Icon\" /><FieldRef Name=\"LargeImage\" /><FieldRef Name=\"Parent\" /><FieldRef Name=\"ID\" />";
                SPListItemCollection childeren = siteMap.GetItems(query);
                //DataTable childerenDT = ConvertToTable(childeren);

                if (childeren.Count >= 1)
                {
                    foreach (SPListItem item in childeren)
                    {
                        string xmlData = ConvertZRowToRegularXml(item.Xml);

                        using (System.IO.StringReader sr = new System.IO.StringReader(xmlData))
                        {
                            XmlReader reader = XmlReader.Create(sr);
                            node.ReadXml(reader);
                            //nodes[node].Children.Add(wsnode);
                        }
                    }

                    if (node.HasChildren())
                    {
                        foreach (WebSiteNode cnode in node.Children)
                        {
                            FindChildNodes(cnode);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
                //throw;
            }
        }
Beispiel #2
0
        public void ReadXml(XmlReader reader)
        {
            try
            {
                var empty = reader.IsEmptyElement;

                if (reader.Name == "row")
                {
                    XmlReader valueReader = reader.ReadSubtree();
                    while (valueReader.Read())
                    {
                        XmlNodeType nType  = valueReader.NodeType;
                        string      nName  = valueReader.Name;
                        string      nValue = valueReader.Value;
                        Type        type   = valueReader.ValueType;

                        switch (valueReader.NodeType)
                        {
                        case XmlNodeType.Element:
                        {
                            switch (valueReader.Name.ToLowerInvariant())
                            {
                            case "id":
                                valueReader.Read();
                                Id = int.Parse(reader.Value);
                                //Id = reader.ReadElementContentAsInt();
                                break;

                            case "uniqueid":
                                valueReader.Read();
                                Key = reader.Value;
                                //Key = reader.ReadElementContentAsString().Trim();
                                break;

                            case "description":
                                valueReader.Read();
                                Description = reader.Value;
                                break;

                            case "title":
                                valueReader.Read();
                                Title = reader.Value;
                                //Title = reader.ReadElementContentAsString().Trim();//valueReader.Value;
                                break;

                            case "url":
                                valueReader.Read();
                                string[] _url = reader.Value.Split(new char[1] {
                                        ','
                                    });
                                Url         = _url[0];
                                Description = _url[1];
                                //Url = reader.ReadElementContentAsString().Trim();
                                break;

                            /*
                             * case "parent":
                             *  Parent = reader.ReadElementContentAsString().Trim();
                             *  break;
                             */
                            case "enabled":
                                //valueReader.ReadElementContentAsBoolean();
                                valueReader.Read();
                                bool boolenabled = false;
                                if (bool.TryParse(reader.Value, out boolenabled))
                                {
                                    Enabled = boolenabled;            // bool.Parse(reader.Value);
                                }
                                if (reader.Value == "1")
                                {
                                    boolenabled = true;
                                }
                                Enabled = boolenabled;
                                break;

                            case "selected":
                                valueReader.Read();
                                Selected = bool.Parse(reader.Value);
                                break;

                            case "breadcrumb":
                                valueReader.Read();
                                Breadcrumb = bool.Parse(reader.Value);
                                break;

                            case "separator":
                                valueReader.Read();
                                Separator = bool.Parse(reader.Value);
                                break;

                            case "icon":
                                valueReader.Read();
                                Icon = reader.Value;
                                break;

                            case "largeimage":
                                valueReader.Read();
                                LargeImage = reader.Value;
                                break;

                            case "commandname":
                                valueReader.Read();
                                CommandName = reader.Value;
                                break;

                            case "commandargument":
                                valueReader.Read();
                                CommandArgument = reader.Value;
                                break;

                            case "row":
                                break;

                                /*
                                 * default:
                                 *  string _name = valueReader.Name;
                                 *  valueReader.Read();
                                 *  string _value = valueReader.Value;
                                 *  if (ExtendedAttributes == null)
                                 *  {
                                 *      ExtendedAttributes = new SortedList();
                                 *  }
                                 *
                                 *  ExtendedAttributes.Add(_name,_value);
                                 *  break;
                                 */
                            }
                            break;
                        }
                        }
                    }
                }

                /*
                 * if (empty)
                 * {
                 *  return;
                 * }
                 */

                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                    case XmlNodeType.Element:
                        switch (reader.Name.ToLowerInvariant())
                        {
                        case "rows":
                            ReadXml(reader);
                            break;

                        case "row":
                            WebSiteNode child = new WebSiteNode {
                                Parent = this
                            };
                            child.ReadXml(reader);
                            Children.Add(child);
                            break;

                        /*
                         * case "keywords":
                         *     Keywords = reader.ReadElementContentAsString().Trim();
                         *     break;
                         * case "description":
                         *     Description = reader.ReadElementContentAsString().Trim();
                         *     break;
                         */
                        default:
                            throw new XmlException(String.Format("Unexpected element '{0}'", reader.Name));
                        }
                        break;

                    case XmlNodeType.EndElement:
                        reader.ReadEndElement();
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
                throw;
            }
        }
        private void InitiateProvider()
        {
            if (nodes == null)
            {
                nodes = new WebSiteNodeCollection();
            }

            if (rootWeb != null)
            {
                string url = rootWeb.Url;
            }
            SPWeb  currentWeb = SPContext.Current.Site.RootWeb;
            string curl       = currentWeb.Url;

            if (rootWeb == null || rootWeb != currentWeb)
            {
                SPSite site = SPContext.Current.Site;
                site.CatchAccessDeniedException = false;
                rootWeb  = site.RootWeb;
                rootNode = null;
                //if (siteMap == null)
                //{

                siteMap = rootWeb.Lists.TryGetList("SiteMap");

                if (rootNode == null && siteMap != null)
                {
                    SPListItemCollection rootNodes = null;

                    try
                    {
                        SPQuery query = new SPQuery();
                        query.Query = "<Where><Eq><FieldRef Name=\"Parent\" /><Value Type=\"Lookup\">Root</Value></Eq></Where><OrderBy><FieldRef Name=\"Position\" Ascending=\"True\" /></OrderBy>";
                        //query.Query = "<Where><IsNull><FieldRef Name=\"Parent\"/></IsNull></Where>";
                        query.ViewFields = "<FieldRef Name=\"Title\" /><FieldRef Name=\"Description\" /><FieldRef Name=\"URL\" /><FieldRef Name=\"Enabled\" /><FieldRef Name=\"Selected\" /><FieldRef Name=\"Breadcrumb\" /><FieldRef Name=\"Separator\" /><FieldRef Name=\"Icon\" /><FieldRef Name=\"LargeImage\" /><FieldRef Name=\"Parent\" /><FieldRef Name=\"Position\" /><FieldRef Name=\"ID\" />";
                        rootNodes        = siteMap.GetItems(query);
                    }
                    catch (Exception ex)
                    {
                        ex.ToString();
                    }

                    /*
                     * try
                     * {
                     *  string url = rootWeb.RootFolder.WelcomePage;
                     * }
                     * catch (Exception)
                     * {
                     *  HttpApplication application = HttpContext.Current.ApplicationInstance;
                     *  application.Server.ClearError();
                     *  application.Response.StatusCode = (int)HttpStatusCode.Forbidden;
                     *  throw (new HttpException((int)HttpStatusCode.Forbidden, ""));
                     * }
                     */

                    if (rootNodes != null)
                    {
                        rootNode = new WebSiteNode();
                        //nodes.Add(rootNode);
                        try
                        {
                            foreach (SPListItem item in rootNodes)
                            {
                                string xmlData = ConvertZRowToRegularXml(item.Xml);

                                using (System.IO.StringReader sr = new System.IO.StringReader(xmlData))
                                {
                                    XmlReader reader = XmlReader.Create(sr);
                                    rootNode.ReadXml(reader);
                                }
                            }

                            if (rootNode.HasChildren())
                            {
                                //bool isSet = false;
                                foreach (WebSiteNode cnode in rootNode.Children)
                                {
                                    try
                                    {
                                        nodes.Add(cnode);
                                        //int i = nodes.Add(cnode);
                                        //if (!isSet)
                                        //{
                                        //    rootNode = (WebSiteNode)nodes[i];
                                        //    isSet = true;
                                        //}
                                        FindChildNodes(cnode);
                                    }
                                    catch (Exception ex)
                                    {
                                        ex.ToString();
                                        //throw;
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            ex.ToString();
                        }
                    }
                    //}
                }
            }
        }