Click here to Skip to main content
15,893,588 members
Articles / Web Development / ASP.NET

Update SharePoint UserInfo List with More Active Directory Info

Rate me:
Please Sign up or sign in to vote.
4.45/5 (5 votes)
19 Mar 2009CPOL2 min read 59.7K   371   16  
Shows how to write a job that updates the UserInfo list with more Active Directory information.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Xml;
using Mullivan.Logging;
using System.Text;
using Microsoft.SharePoint;
using System.Reflection;
using System.Globalization;
using System.Security;
using Microsoft.SharePoint.WebControls;

namespace Mullivan.SharePoint.Pages.WebControls
{
    public class QueryTreeView : TreeView
    {
        protected override TreeNode CreateNode()
        {
            return new QueryTreeNode();
        }
    }

    public class QueryTreeNode : TreeNode
    {
        private string _internalName;
        private string _comparer;
        private bool _isUserInput;
        private string _value;
        private Guid _listId;
        private Guid _webId;
        private QueryNodeType _nodeType;

        public enum QueryNodeType
        {
            Where,
            And,
            Or,
            Predicate
        }

        public QueryTreeNode() 
            : base() 
        {
            this.Value = Guid.NewGuid().ToString();
        }

        public QueryTreeNode(QueryNodeType nodeType)
            : this()
        {
            this.NodeType = nodeType;
            this.SetText();
        }

        public QueryTreeNode(XmlElement xe, Guid listId, Guid webId)
            : this(QueryNodeType.Predicate)
        {
            _webId = webId;
            _listId = listId;
            ReadPredicateXmlElement(xe);
            this.SetText();
        }

        public QueryTreeNode(TreeView owner, bool isRoot) : 
           base(owner, isRoot) { }


        public void SetText()
        {
            if (this.NodeType == QueryNodeType.Predicate)
                this.SetPredicateText();
            else if (this.NodeType == QueryNodeType.Where)
                this.Text = "Where";
            else
                this.SetAndOrText();
        }

        private void SetAndOrText()
        {
            this.Text = string.Format("<span style=\"color:green;\">{0}</span>", this.NodeType.ToString());
        }

        private void SetPredicateText()
        {
            SPList list = null;
            SPField spField = null;

            try
            {
                if (_listId != Guid.Empty)
                {
                    list = this.GetList();
                    spField = list.Fields.GetFieldByInternalName(this.InternalName);
                }
            }
            catch (Exception ex)
            {
                Mullivan.Logging.ProductLog.WriteEntry("Query Builder", ProductLogCategory.Error, ex);
            }

            string value = string.Empty;

            if (this.Comparer != null && this.Comparer.Contains("Null"))
                value = "<span style=\"color:blue;\">null</span>";
            else if (this.IsUserInput)
                value = "<span style=\"color:blue;\">INPUT</span>";
            else
            {
                value = "<span style=\"color:red;\">'{0}'</span>";
                if (spField != null && spField.FieldValueType == typeof(string))
                    value = string.Format(value, HttpUtility.HtmlEncode(spField.GetFieldValueAsText(this.FieldValue)));
                else
                    value = HttpUtility.HtmlEncode(this.FieldValue);

            }

            this.Text = string.Format("({0} {1} {2})",
                spField == null ? this.InternalName : spField.Title,
                this.GetDisplayComparerAsHtml(),
                value);
        }

        public QueryNodeType NodeType
        {
            get
            {
                return _nodeType;
            }
            set
            {
                _nodeType = value;
            }
        }

        public Guid ListId
        {
            get
            {
                return _listId;
            }
            set
            {
                _listId = value;
            }
        }

        public Guid WebId
        {
            get
            {
                return _webId;
            }
            set
            {
                _webId = value;
            }
        }

        public string InternalName
        {
            get
            {
                return _internalName;
            }
            set
            {
                _internalName = value;
            }
        }

        public string Comparer
        {
            get
            {
                return _comparer;
            }
            set
            {
                _comparer = value;
            }
        }

        public bool IsUserInput
        {
            get
            {
                return _isUserInput;
            }
            set
            {
                _isUserInput = value;
            }
        }

        public string FieldValue
        {
            get
            {
                return _value;
            }
            set
            {
                _value = value;
            }
        }

        public void WriteXml(StringBuilder sb)
        {
            if (NodeType == QueryNodeType.Where)
            {
                sb.AppendLine("<Where>");
                WriteChildren(sb);
                sb.AppendLine("</Where>");
            }
            else if (NodeType == QueryNodeType.And)
            {
                sb.AppendLine("<And>");
                WriteChildren(sb);
                sb.AppendLine("</And>");
            }
            else if (NodeType == QueryNodeType.Or)
            {
                sb.AppendLine("<Or>");
                WriteChildren(sb);
                sb.AppendLine("</Or>");
            }
            else
            {
                SPField spField = GetField();
                string type = "Text";
                bool isLookup = false;
                if (spField != null)
                    type = spField.TypeAsString;
                else
                    return;

                switch (spField.Type)
                {
                    case SPFieldType.Lookup:
                    case SPFieldType.User:
                        isLookup = true;
                        break;
                    default:
                        isLookup = false;
                        break;
                }

                string comparer = this.Comparer;

                if (string.IsNullOrEmpty(comparer))
                    comparer = "Eq";

                sb.AppendLine(string.Format("<{0}>", comparer));

                sb.AppendLine(string.Format("<FieldRef Name='{0}' {1} {2} />"
                    , this.InternalName
                    , isLookup ? "LookupId='True'" : ""
                    , spField.Type == SPFieldType.DateTime ? "IncludeTimeValue='TRUE'" : string.Empty));
                if (!comparer.Contains("Null"))
                {
                    if (this.IsUserInput)
                        sb.AppendLine("<UserInput />");
                    else
                    {
                        sb.AppendLine(string.Format("<Value Type='{0}' {2}>{1}</Value>"
                            , type
                            , spField.Type != SPFieldType.DateTime ? SecurityElement.Escape(this.FieldValue) : this.FieldValue
                            , spField.Type == SPFieldType.DateTime ? "IncludeTimeValue='TRUE'" : string.Empty));
                    }
                }
                sb.AppendLine(string.Format("</{0}>", comparer));
            }
        }

        private SPField GetField()
        {
            SPList list = GetList();
            return list.Fields.GetFieldByInternalName(this.InternalName);
        }

        private void WriteChildren(StringBuilder sb)
        {
            foreach (QueryTreeNode qnt in this.ChildNodes)
            {
                qnt.WriteXml(sb);
            }
        }

        private SPList GetList()
        {
            using (SPWeb web = SPContext.Current.Site.OpenWeb(_webId))
            {
                return web.Lists[_listId];
            }
        }

        private string GetDisplayComparerAsHtml()
        {
            if (string.IsNullOrEmpty(this.Comparer))
                return "?";

            string comparer = string.Empty;
            switch (this.Comparer)
            {
                case "BeginsWith":
                    comparer = "BeginsWith";
                    break;
                case "Contains":
                    comparer = "Contains";
                    break;
                case "Eq":
                    comparer = "==";
                    break;
                case "Geq":
                    comparer = ">=";
                    break;
                case "Gt":
                    comparer = ">";
                    break;
                case "Leq":
                    comparer = "<=";
                    break;
                case "Lt":
                    comparer = "<";
                    break;
                case "Neq":
                    comparer = "!=";
                    break;
                //case "DateRangesOverlap":
                case "IsNotNull":
                    comparer = "!=";
                    break;
                case "IsNull":
                    comparer = "==";
                    break;
                default:
                    break;
            }

            return HttpUtility.HtmlEncode(comparer);
        }

        private void ReadPredicateXmlElement(XmlElement xe)
        {
            try
            {
                this.Comparer = xe.Name;

                XmlElement xeFieldRef = (XmlElement)xe.SelectSingleNode("FieldRef");
                if (xeFieldRef != null)
                {
                    string internalName = null;
                    if (!string.IsNullOrEmpty(internalName = xeFieldRef.GetAttribute("Name")))
                        this.InternalName = internalName;
                }

                XmlElement xeUserInput = (XmlElement)xe.SelectSingleNode("UserInput");
                this.IsUserInput = xeUserInput != null;

                if (!this.IsUserInput)
                {
                    XmlElement xeValue = (XmlElement)xe.SelectSingleNode("Value");
                    if (xeValue != null)
                    {
                        this.FieldValue = xeValue.InnerText;
                    }
                }

            }
            catch(Exception ex)
            {
                Mullivan.Logging.ProductLog.WriteEntry("Query Builder", ProductLogCategory.Error, ex);
            }
        }

        protected override void LoadViewState(Object savedState)
        {
            if (savedState != null)
            {
                base.LoadViewState(savedState);
                object[] objs = (object[])savedState;
                if (objs.Length > 2)
                    this._listId = (Guid)objs[2];
                if (objs.Length > 3)
                    this._internalName = (string)objs[3];
                if (objs.Length > 4)
                    this._comparer = (string)objs[4];
                if (objs.Length > 5)
                    this._isUserInput = (bool)objs[5];
                if (objs.Length > 6)
                    this._value = (string)objs[6];
                if(objs.Length > 7)
                    this._nodeType = (QueryNodeType)objs[7];
                if (objs.Length > 8)
                    this._webId = (Guid)objs[8];
            }

            this.SetText();
        }

        protected override Object SaveViewState()
        {
            List<object> saveState = new List<object>((object[])base.SaveViewState());
            saveState.Add(ListId);
            saveState.Add(InternalName);
            saveState.Add(Comparer);
            saveState.Add(IsUserInput);
            saveState.Add(FieldValue);
            saveState.Add(NodeType);
            saveState.Add(WebId);
            return saveState.ToArray();
        }

        protected override void RenderPreText(HtmlTextWriter writer)
        {
            //Ensure that the changes get rendered into the Text Property
            this.SetText();
            base.RenderPreText(writer);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions