SharePoint 用递归实现组织结构 C#

后台代码

        public SPSite site
        {
            get
            {
                return SPContext.Current.Site;
            }
        }


        public SPWeb spWeb
        {


            get
            {
                return site.OpenWeb();
            }
        }
        public SPList spList
        {


            get
            {
                return spWeb.Lists["OrgUnitList"];
            }
        }




        public string parentid;


        public string str;


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                parentid = Request.QueryString["parentid"];
                if (parentid != null)
                {
                    GetList(parentid);
                }
            }


        }


        //根结点
        JsonItem rootItem = new JsonItem();


        private void GetList(string pid)
        {
            SPQuery query = new SPQuery();
            SPListItem sitem = spList.GetItemById(Convert.ToInt32(pid));
            string sTitle = sitem["Title"] == null ? "" : sitem["Title"].ToString();
            rootItem.Attributes.Add("id", sitem.ID);
            rootItem.Attributes.Add("title", sTitle);
            rootItem.Attributes.Add("name", sTitle);
            GetChildData(rootItem, Convert.ToInt32(pid));
            str = rootItem.ToString();
        }


        private void GetChildData(JsonItem p, int pid)
        {
            SPQuery cquery = new SPQuery();
            cquery.Query = Camlex.Query().Where(x => x["ParentOU"] == (DataTypes.LookupId)pid.ToString()).ToString();
            SPListItemCollection clistitems = spList.GetItems(cquery);
            JsonItemCollection children = new JsonItemCollection();
            foreach (SPListItem temp in clistitems)
            {
                //子结点
                JsonItem childItem = new JsonItem();
                string cTitle = temp["Title"] == null ? "" : temp["Title"].ToString();
                //子结点添加到子结点集合数组里面
                childItem.Attributes.Add("id", temp.ID);
                childItem.Attributes.Add("title", cTitle);
                childItem.Attributes.Add("name", cTitle);
                GetChildData(childItem, temp.ID);
                children.Add(childItem);
            }
            if (children.Count > 0)
                p.Attributes.Add("children", children);

        }


前台代码

    <link rel="stylesheet" href="https://cdn.rawgit.com/FortAwesome/Font-Awesome/master/css/font-awesome.min.css">
    <link href="../dist/css/jquery.orgchart.css" rel="stylesheet" />

    <script type="text/javascript">
        'use strict';


        (function ($) {
            $(function () {
                var datasource =eval(<%=str%>);
                // sample of core source code
                $('#chart-container').orgchart({
                    'data': datasource,
                    'depth': 2,//默认展开几级
                    'nodeContent': 'title',
                    'nodeID': 'id',
                    'createNode': function ($node, data) {
                        var secondMenuIcon = $('<i>', {
                            'class': 'fa fa-info-circle second-menu-icon',
                            click: function () {
                                $(this).siblings('.second-menu').toggle();
                            }
                        });
                        var secondMenuDiv = '<div class="second-menu"><img class="avatar" src="/OrgImg/' + data.id + '.jpg">';
                        $node.append(secondMenuIcon).append(secondMenuDiv);
                    }
                });
                $(".node").bind("click", function () {
                    var parentid = $(this).attr('id');
                    window.open("/_layouts/15/SP2013GetAllDocuments/ChildImgOrgchart.aspx?parentid=" + parentid);
                });
            });


        })(jQuery);




    </script>

JsonHelper.cs类

using System;
using System.Data;
using System.Configuration;
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 System.Text;
using System.Globalization;


/// <summary>
/// MCSCFJsonHelper 的摘要说明
/// </summary>
public class JsonHelper
{
    public static IFormatProvider JavaScriptFormat = new CultureInfo(1033);


    public static string EncodeAttribute(string s)
    {
        if (String.IsNullOrEmpty(s))
            return String.Empty;


        StringBuilder sb = new StringBuilder();
        foreach (char c in s)
        {
            switch (c)
            {
                case '\"':
                    sb.Append("\\\"");
                    break;
                case '\'':
                    sb.Append("\\\'");
                    break;
                case '\\':
                    sb.Append("\\\\");
                    break;
                case '\b':
                    sb.Append("\\b");
                    break;
                case '\f':
                    sb.Append("\\f");
                    break;
                case '\n':
                    sb.Append("\\n");
                    break;
                case '\r':
                    sb.Append("\\r");
                    break;
                case '\t':
                    sb.Append("\\t");
                    break;
                default:
                    int i = (int)c;
                    if (i < 32 || i > 127)
                    {
                        sb.AppendFormat("\\u{0:X04}", i);
                    }
                    else
                    {
                        sb.Append(c);
                    }
                    break;
            }
        }


        return sb.ToString();
    }


    public static string ConvertToJsonValue(object value)
    {
        if (value == null || Convert.IsDBNull(value))
            return "\"\"";


        if (value is string)
            return "\"" + JsonHelper.EncodeAttribute(Convert.ToString(value)) + "\"";


        if (value is DateTime){
            DateTime date = (DateTime)value;
            return String.Format("\"{0}-{1}-{2} {3}:{4}:{5}\"",
                date.Year.ToString("0000"),
                date.Month.ToString("00"),
                date.Day.ToString("00"),
                date.Hour.ToString("00"),
                date.Minute.ToString("00"),
                date.Second.ToString("00"));
        }


        //欧洲、印度等国家,小数点会转换为",",这在JavaScript中不会识别,JS识别的是"."
        string rv = Convert.ToString(value, JsonHelper.JavaScriptFormat);


        if (value is bool)
            rv = rv.ToLower();


        return rv;
    }
}

JsonItem.cs类

using System;
using System.Data;
using System.Text;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
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;


/// <summary>
/// JsonItem 的摘要说明
/// </summary>
public class JsonItem
{
    public const string TotalRows = "totalRows";


    Dictionary<string,object> _attributes;


    public JsonItem()
    {
    }


    public Dictionary<string, object> Attributes
    {
        get
        {
            if (this._attributes == null)
            {
                this._attributes = new Dictionary<string, object>(StringComparer.InvariantCultureIgnoreCase);
            }


            return this._attributes;
        }
    }


    public void AppendToJsonString(StringBuilder sb)
    {
        sb.Append("{");


        bool firstAttr = true;
        foreach (KeyValuePair<string, object> kv in this.Attributes)
        {
            if (firstAttr)
                firstAttr = false;
            else
                sb.Append(",");


            sb.Append("'" + kv.Key + "'");
            sb.Append(":");
            sb.Append(JsonHelper.ConvertToJsonValue(kv.Value));
        }


        sb.Append("}");
    }


    public override string ToString()
    {
        StringBuilder sb = new StringBuilder();
        AppendToJsonString(sb);
        return sb.ToString();
    }
}

JsonItemCollection.cs类

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Text;
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;


/// <summary>
/// JsonItemCollection 的摘要说明
/// </summary>
public class JsonItemCollection : List<JsonItem>
{
    public void AppendToJsonString(StringBuilder sb)
    {
        sb.Append("[");


        bool firstChild = true;
        foreach (JsonItem item in this)
        {
            if (firstChild)
                firstChild = false;
            else
                sb.Append(",");


            item.AppendToJsonString(sb);
        }


        sb.Append("]");
    }


    public override string ToString()
    {
        StringBuilder sb = new StringBuilder();
        this.AppendToJsonString(sb);
        return sb.ToString();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值