zoukankan      html  css  js  c++  java
  • 根据SPWeb递推得到地下的所有子网站和List的Json

    1.开发缘由

    其实这样的工具很多,在网站中一点点看也是大概能够了解的。但是属于强迫症,总想看一下所有。

    2.设计思路

    开始是想做成思维导图的模式,jQuery插件,Html都搜过,结果只找到了http://qunee.com/buy.html。有什么办法呢,只能随便找个tree显示。

    Json拼写开始以为收到擒来,结果写逻辑生疏的我竟然写了两三个小时。

    公布出来大家一起学习吧。

    3.代码部分

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using Microsoft.SharePoint;
    
    public partial class _Default : System.Web.UI.Page
    {
        public string strr;
        bool IsAddList = true;
        string path = "http://xxxx18:8099/1234/";
        protected void Page_Load(object sender, EventArgs e)
        {
            strr = GetJsonFormWeb(path);
            strr = "[" + strr + "]";
        }
    
        public string GetJsonFormWeb(string path)
        {
            string jsonStr = "";
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using (SPSite site = new SPSite(path))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        if (web.Webs.Count>0)
                        {
                            for (int i = 0; i < web.Webs.Count; i++)
                            {
                                jsonStr += "{text:'" + "Web " + (i+1).ToString() + "----" + web.Webs[i].Name + "'";
                                if (web.Webs[i].Webs.Count > 0)
                                {
                                    jsonStr += ",children: [";
                                    jsonStr += GetJsonFormWeb(web.Webs[i].Url);
                                    if (IsAddList && web.Webs[i].Lists.Count > 0)
                                    {
                                        jsonStr +="," + GetListJson(web.Webs[i]);
                                    }
                                    jsonStr += "]";
                                }
                                else
                                {
                                    if (IsAddList && web.Lists.Count > 0)
                                    {
                                        jsonStr += ",children: [";
                                        jsonStr += GetListJson(web.Webs[i]);
                                        jsonStr += "]";
                                    }
                                }
                                //if (web.Webs[i].Webs.Count > 0 && web.Webs[i].Lists.Count > 0)
                                //{
                                //    jsonStr += ",";
                                //}
                                jsonStr += "}";
                                if (i < web.Webs.Count - 1)
                                {
                                    jsonStr += ",";
                                }
                            }
                        }
                        else
                        {
                            
                            if (web.Lists.Count > 0 && IsAddList)
                            {
                                jsonStr += ",children: [";
                                jsonStr += GetListJson(web);
                                jsonStr += "]";
                            }
                        }
                        if (web.Webs.Count > 0 && web.Lists.Count > 0 && IsAddList)
                        {
                            jsonStr += "," + GetListJson(web);
                        }
                        
                    }
                }
            });
            return jsonStr;
        }
    
        public string GetListJson(SPWeb web)
        {
            if (!IsAddList)
            {
                return "";
            }
            string str = "";
            if (web.Lists.Count>0)
            {
                for (int i = 0; i < web.Lists.Count; i++)
                {
                    if (web.Lists[i].Title.Trim().Length>0)
                    {
                        str += "{text:'" + web.Lists[i].Title + "'}";
                        if (i < web.Lists.Count - 1)
                        {
                            str += ",";
                        }
                    }
                }
            }
            return str;
        }
    }
    

      

    4.源码:

    开发工具:VS2005

    http://app.yinxiang.com/shard/s12/sh/765c7019-04ec-4efd-9f83-8e4d0e068c6b/dbab5493b8c06753d4cd6779ec929067

  • 相关阅读:
    触摸屏多媒体查询展示自主设计系统开发过程
    hashtable数据循环读取的顺序问题
    vs2010英文版打包中文框架出错的解决办法
    Silverlight游戏开发初探(上篇)
    PB之——编码规范
    时间相加 ,使用SQL完成
    PB(POWERBUILDER) 基础介绍
    PB之——流程控制
    PB之——基本数据类型
    PB [Grid风格数据窗口改变线条颜色] 的变通实现方法(也可以成为 带表头的Grid数据窗口)
  • 原文地址:https://www.cnblogs.com/yixiaozi/p/3947658.html
Copyright © 2011-2022 走看看