根据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

 

你可能感兴趣的:(json)