Click here to Skip to main content
15,886,701 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.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Web.UI;
using Microsoft.SharePoint;

namespace Mullivan.SharePoint.WebParts
{
    public class ListQueryEditor : EditorPart
    {
        private TextBox _tboxList = null;
        private TextBox _tboxViewFields = null;
        private TextBox _tboxQuery = null;
        private TextBox _tboxPageSize = null;
        private RangeValidator _rvPageSize = null;

        public ListQueryEditor(string webPartId)
        {
            this.ID = webPartId + "_ListQueryEditor";
        }

        protected override void OnInit(EventArgs e)
        {
            _tboxList = new TextBox();
            _tboxViewFields = new TextBox();
            _tboxQuery = new TextBox();
            _tboxPageSize = new TextBox();
            _rvPageSize = new RangeValidator();

            _tboxList.ID = "tboxList";
            _tboxViewFields.ID = "tboxViewFields";
            _tboxQuery.ID = "tboxQuery";
            _tboxPageSize.ID = "tboxPageSize";
            _rvPageSize.ID = "rvPageSize";

            _rvPageSize.ControlToValidate = "tboxPageSize";
            _rvPageSize.ErrorMessage = "Page size must be an integer between 1 and 1000.";
            _rvPageSize.MaximumValue = "1000";
            _rvPageSize.MinimumValue = "1";
            _rvPageSize.Type = ValidationDataType.Integer;
            
            base.OnInit(e);
        }

        protected override void CreateChildControls()
        {

            base.CreateChildControls();

            #region "HTML"
            Literal litBeginHtml = new Literal();
            litBeginHtml.Text = @"
<br />
<div>
<table cellspacing=""0"" cellpadding=""0"" border=""0"" style=""width:0px; width:100%;border-collapse:collapse;"">
	<tr>
		<td>
            <div class=""UserSectionTitle"">
                <a id=""{0}_MVQueryCategory_IMAGEANCHOR"" TabIndex=""0"" onkeydown='MSOMenu_KeyboardClick(this, 13, 32);' 
                style='cursor:hand' 
                onclick=""javascript:MSOTlPn_ToggleDisplay('{0}_MVQueryCategory', '{0}_MVQueryCategory_IMAGE', '{0}_MVQueryCategory_ANCHOR', 'Expand category: Query', 'Collapse category: Query','{0}_MVQueryCategory_IMAGEANCHOR');"" 
                title=""Collapse category: Query"" >
                    &nbsp;<img id=""{0}_MVQueryCategory_IMAGE"" alt=""Collapse category: Query"" border=""0"" src=""/_layouts/images/TPMin2.gif"" />&nbsp;
                </a>
                <a TabIndex=""-1"" 
                    onkeydown='MSOMenu_KeyboardClick(this, 13, 32);' 
                    id=""{0}_MVQueryCategory_ANCHOR"" 
                    style='cursor:hand' 
                    onclick=""javascript:MSOTlPn_ToggleDisplay('{0}_MVQueryCategory', '{0}_MVQueryCategory_IMAGE', '{0}_MVQueryCategory_ANCHOR', 'Expand category: Query', 'Collapse category: Query','{0}_MVQueryCategory_IMAGEANCHOR');"" 
                    title=""Collapse category: Query"" >&nbsp;Query
                </a>
            </div>
        </td>
    </tr>
</table>
<div id=""{0}_MVQueryCategory"">
    <table cellspacing=""0"" border=""0"" style=""border-width:0px;width:100%;border-collapse:collapse;"">";
            Literal litEndHtml = new Literal();
            litEndHtml.Text = @"
    </table>
</div>";
            #endregion "HTML"

            this.Controls.Add(litBeginHtml);

            string listClientId = this.ClientID + "_tboxList";
            string viewClientId = this.ClientID + "_tboxViewFields";
            string queryClientId = this.ClientID + "_tboxQuery";
            string serverUrl = SPContext.Current.Web.ServerRelativeUrl.Replace("/", "\\u002f");

            string editListJS = string.Format("LQWP_LaunchListPicker('{0}','{1}')", listClientId, serverUrl);
            string editViewJS = string.Format("LQWP_LaunchViewBuilder('{0}','{1}','{2}')", listClientId, viewClientId, serverUrl);
            string editQueryJS = string.Format("LQWP_LaunchQueryBuilder('{0}','{1}','{2}')", listClientId, queryClientId, serverUrl);

            AppendEditControl("List"
                , "Value that represents the list that is set to be queried."
                ,_tboxList
                ,this.Controls
                , "Browse..."
                , editListJS
                , null);
            AppendEditControl("View"
                , "The fields that you would like to display in the results."
                , _tboxViewFields
                , this.Controls
                , "Build..."
                , editViewJS
                , null);
            AppendEditControl("Query"
                , "The CAML query that will be used to search the list."
                , _tboxQuery
                , this.Controls
                , "Build..."
                , editQueryJS
                ,null);

            AppendEditControl("Page Size"
                , "The amount of items that should be displayed per page."
                , _tboxPageSize
                , this.Controls
                , null
                , null
                ,_rvPageSize);

            this.Controls.Add(litEndHtml);
        }

        protected override void OnPreRender(EventArgs e)
        {
            ClientScriptManager csm = this.Page.ClientScript;
            Type lnType = this.GetType();
            //Register our Javascript file
            if (!csm.IsClientScriptIncludeRegistered(@"Mullivan.SharePoint.WebParts.JS.Utility.js"))
            {
                string url = csm.GetWebResourceUrl(lnType, @"Mullivan.SharePoint.WebParts.JS.Utility.js");
                csm.RegisterClientScriptInclude(lnType, "Mullivan.SharePoint.WebParts.JS.Utility.js", ResolveClientUrl(url));
            }

            if (!csm.IsClientScriptIncludeRegistered(@"Mullivan.SharePoint.WebParts.JS.ListQuery.js"))
            {
                string url = csm.GetWebResourceUrl(lnType, @"Mullivan.SharePoint.WebParts.JS.ListQuery.js");
                csm.RegisterClientScriptInclude(lnType, "Mullivan.SharePoint.WebParts.JS.ListQuery.js", ResolveClientUrl(url));
            }

            if (!csm.IsClientScriptIncludeRegistered(@"PickerTreeDialog"))
            {
                string url = @"/_layouts/1033/PickerTreeDialog.js";
                csm.RegisterClientScriptInclude(lnType, "PickerTreeDialog", url);
            }

            if (!csm.IsClientScriptIncludeRegistered(@"QueryBuilderDialog"))
            {
                string url = @"/_layouts/1033/QueryBuilder.js";
                csm.RegisterClientScriptInclude(lnType, "QueryBuilderDialog", url);
            }
            base.OnPreRender(e);
        }

        private void AppendEditControl(string displayName, string description
            , WebControl control, ControlCollection controlCollection
            , string editText, string editJS, Control validationControl)
        {
            Literal litBeginHtml = new Literal();
            Literal litEndHtml = new Literal();

            controlCollection.Add(litBeginHtml);
            controlCollection.Add(control);
            if (validationControl != null)
                controlCollection.Add(validationControl);
            controlCollection.Add(litEndHtml);


            litBeginHtml.Text = string.Format(@"
		<tr>
			<td>
			    <div class=""UserSectionHead"">
			        <LABEL FOR=""{0}"" 
			               TITLE=""{2}"">{1}
			        </LABEL>
			    </div>
			    <div class=""UserSectionBody"">
			        <div class=""UserControlGroup"">
			            <table cellpadding=""0"" cellspacing=""0"" border=""0"">
			                    <tr style=""text-align:left"">
			                        <td>", control.ClientID, displayName, description);
            litEndHtml.Text = @"
                                    </td>
			                    </tr>
			                    <tr style=""text-align:right;{0}"">
			                        <td><input type=""button""  
			                                value=""{1}"" 
			                                title=""Click to edit."" 
			                                tabindex=""0"" 
			                                class=""ms-PropGridBuilderButton"" 
			                                style=""display:inline;cursor:pointer;width:55px;text-align:center""
			                                onclick=""javascript:{2}"" />
			                        </td>
			                    </tr>
			                </table>
			        </div>
			    </div>
			    <div style='width:100%' class='UserDottedLine'>
			    </div>
		    </td>
	    </tr>";

            string display = "";
            if (string.IsNullOrEmpty(editText))
            {
                editText = string.Empty;
                editJS = string.Empty;
                display = "display:none";
            }

            litEndHtml.Text = string.Format(litEndHtml.Text, display, editText, editJS);
            
            control.CssClass = "UserInput";
            control.Style[HtmlTextWriterStyle.Width] = "176px";
        }

        public override bool ApplyChanges()
        {
            EnsureChildControls();

            ListQueryWebPart lqwp = this.WebPartToEdit as ListQueryWebPart;
            if (lqwp == null)
                return false;

            lqwp.Query = _tboxQuery.Text;
            lqwp.ListUrl = _tboxList.Text;
            lqwp.ViewFields = _tboxViewFields.Text;
            lqwp.PageSize = uint.Parse(_tboxPageSize.Text);

            return true;
        }

        public override void SyncChanges()
        {
            EnsureChildControls();

            ListQueryWebPart lqwp = this.WebPartToEdit as ListQueryWebPart;
            if (lqwp == null)
                return;

            _tboxQuery.Text = lqwp.Query;
            _tboxList.Text = lqwp.ListUrl;
            _tboxViewFields.Text = lqwp.ViewFields;
            _tboxPageSize.Text = lqwp.PageSize.ToString();

        }

    }
}

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