The What
I’m aware this might not be the best approach to tackle this, but I’m a fan of learning several things at the same time.

For this reason, I’m going to develop a Modular application in WPF, using the MVVM pattern, to access and edit the Active Directory.

The idea is that through the application you’ll be able to create new attributes for objects within the Active Directory, or new classes, define new object types, and also load modules (read: dlls) that will allow you to view these attributes the way you want to.

I’ll be providing links to other posts in order to go deeper into the different topics. This is a post that I’ll be updating as I do this, so bare with me and please feel free to leave feedback. Let me know if you are interested in this topic, or if you are interested in taking a look at something related to this.

Also, if you know/think something could be done better (probably there are 1000 better ways to do this), feel free to let me know. I’m in this for the knowledge :).

The Why
I’ve seen that a lot of people and companies asking how to add pictures to Active Directory users. Surprisingly, this is NOT SIMPLE, or at least not as straight-forward as I thought.

There are a few resources for this, but I was interested in learning how to do it, so lets share it with everyone.

Here’s one with some good links as well from the ITPro’s blog. The fact that we have the same wordpress theme is mere coincidence!

The How

First, to apply the MVVM Pattern, I am using Laurent Bugnion’s MVVM Light Toolkit. Very simple and easy to use and start applying the MVVM to decouple the user interface from the code and avoid using the code behind as much as possible.

A few ActiveDirectory resources that I’m finding very useful:

(Almost) Everything in Active Directory in C#
Searching the Directory
Search Filter Syntax
Quick List for C# Code Examples

Great WPF and MVVM resources:

Under construction 🙂

So visual studio comes with a LOAD of features, most of which we will never use.

Nevertheless, there are a few of these things to consider that, with some practice, will speed up your coding time.

So first things first:

Useful Hotkeys

Go to definition: F12
Use this whenever the cursor is over a member, a class or a method and you need to know what it does. It will take you straight to it.

Find all references: Shift + F12
Need to know where a method, property or attribute is being referenced? Just stand on top of it and press those keys. The results will show up, showing you every place where it is being used.

Clipboard stack
Are you copying a lot of stuff, and going back and forth because you need to copy the same things over and over again? Not anymore!

When pasting just press Ctrl + Shift + V and you will cycle between the last 20 things you Ctrl+C’d 🙂

Yay! No more going around the files and trying to find out what you had to copy!

Find [and Replace]
Well Find is pretty standard: Ctrl + F

However, if you were wondering how to go straight to replace: Ctrl + H does the trick.

Full Screen Mode

Are you coding on a small screen, or low resolution (say round 1024×768 ugh, or 1280×720)?

Full Screen is the way to go!. Toggle between Full Screen and Standard mode by just pressing Shift + Alt + Enter

Click for full size

Visual Studio 2010 Programming in full screen mode.

Regions

Regions are a great feature to use in Visual Studio, allowing us to keep all our code organized.

The following code is not that long, but already has a “lot of stuff”, and adding more methods can make things unmanageable later.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sample_App
{
    public class Employee : IPerson
    {
        //The attributes
        private List _children = null;

        //The properties
        public DateTime Birthdate { get; set; }
        public string Name { get; set; }

        //Here are the constructors
        public Employee(DateTime Birthdate, string Name, IEnumerable Children)
        {
            this.Birthdate = Birthdate;
            this.Name = Name;
            this._children = new List(Children);
        }
        #region
        public Employee()
        {
            this.Birthdate = DateTime.MinValue;
            this.Name = "Mr. John Doe";
            this._children = new List();
        }

        //Here we implement the IPerson interface
        public int IPerson.GetAge()
        {
            return (int)((DateTime.Now - this.Birthdate).TotalDays / 365);
        }

        public bool IPerson.HasChildren()
        {
            return this._children.Count > 0;
        }
    }
}

Using the #region keyword we can simplify how we code, viewing only the portions of the code that we are working on, or that we need. Here’s a collapsed version of our code, and we can work only with what we need, reducing visual noise:

#region using
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#endregion

namespace Sample_App
{
    public class Employee : IPerson
    {
        #region Attributes

        private List _children = null;

        #endregion

        #region Properties

        public DateTime Birthdate { get; set; }
        public string Name { get; set; }

        #endregion

        #region Constructors
        public Employee(DateTime Birthdate, string Name, IEnumerable Children)
        {
            this.Birthdate = Birthdate;
            this.Name = Name;
            this._children = new List(Children);
        }

        public Employee()
        {
            this.Birthdate = DateTime.MinValue;
            this.Name = "Mr. John Doe";
            this._children = new List();
        }
        #endregion

        #region IPerson Members
        public int IPerson.GetAge()
        {
            return (int)((DateTime.Now - this.Birthdate).TotalDays / 365);
        }

        public bool IPerson.HasChildren()
        {
            return this._children.Count > 0;
        }
        #endregion
    }
}

Suppose now we are adding new methods, and that all we need to be looking at is the attributes and properties:
(Note: The + symbols in Visual Studio will allow you to expand those regions.)

Use the command window

I didn’t even know what this was. But it is awe-some. Press Ctrl + Alt + A and the command window pops up.

What now? Type what it is that you want to do. Heads-up: There’s IntelliSense as well!

From here you can access pretty much everything that is available from the UI.

For example: Want to replace the name of a property?

Edit.Replace findwhat replacewith [/all] [/case] [/doc|/proc|/open|/sel] [/hidden] [/options] [/reset] [/up] [/wild|/regex] [/word]

Type:

>Edit.Replace name1 name2 /all

Of course doing this each time is very tedious. Luckily the folks at Microsoft thought of the alias keyword:

Try the following

>alias rep Edit.Replace /all
>rep name1 name2

Hah!
So here you have a much faster replace than going for Ctrl + H then typing into one box, then typing into the other.

There are lots of commands, which you can take a look at here.

Edit multiple lines

This is another pretty useful feature.

It allows us to, for example, modify the visibility of several attributes simultaneously; again saving us time.

To do this, place the cursor where you want to edit, and move it while pressing Alt + Shift:

Hope this guide is useful, and if you have any more tips, let me know and i’ll include them in this post!

Have a great day!