Monday, April 14, 2014

SharePoint Menu Item to Change your AD Password

It totally slays me why Microsoft hasn't included a feature to change your Active Directory password directly through SharePoint, especially when they position the system as an extranet portal.  Maybe it's because they want you to invest in UAG or something.

Anyway, adding such a feature is pretty straight forward.  You'll need to do the following high level steps:

1) Create a WebPart (Visual, SharePoint, or otherwise)
2) Create a page to host the web part
3) Add a CustomAction in an Elements file

So when I when through this process (1) was a Visual WebPart in Visual Studio 2010; (2) was part of a Files Module and deploys to the /StyleLibrary of the site; and (3) references a WebControl that is in the same namespace as my ChagePassword (1) VWP.

I'm going to assume that you know how to create a VWP and deploy it.  And, I'll assume you know how to create an ASPX page as part of a SharePoint project.  Also I'll assume, for now, you know all about the System.DirectoryServices.AccountManagement API, so that you can do all that work on your own.  In another post, I'll cover the Account Management API and cover some of the topics related to Kerberos and how that enhances the ability to work with the Active Directory API's in general.

Right now, we'll concentrate on how you'd deploy the ASPX page to the /StyleLibrary and how you deploy a custom action to the Standard Menu.  First off, I called my password change page ChagnePassword.ASPX and my new custom action ChangePasswordMenuItem, and I deployed my custom resources in a folder called AccountManagement.  Original, I know.

Ok, lets first start with the menu item WebControl.



public class ChangePasswordMenuItem : WebControl
{
       protected override void CreateChildControls() {
              base.CreateChildControls();

              MenuItemTemplate menuItem = new MenuItemTemplate() {
                     Text = "Change Password",
                     Description = "Change your Login Password",
                     Sequence = 1,
                     ClientOnClickNavigateUrl =  
                            SPContext.Current.Site.Url.ToString() +  
                            "/Style Library/AccountManagement/ChangePassword.aspx?Source=" + 
                            SPHttpUtility.UrlKeyValueEncode( 
                                          SPUtility.OriginalServerRelativeRequestUrl),
                     ImageUrl = SPContext.Current.Site.Url.ToString() + 
                            "/Style Library/AccountManagement/cp.png"
              };

              Controls.Add(menuItem);
       }
}

When this control is loaded, it creates a new menu item to be placed in the Standard Menu, otherwise know as the "User Menu," that one in the top right-hand corner.  The reason it's in this menu instead of another menu?  There's an addition you need to manually make to one of the Elements.xml files in your VS2010 SharePoint project.



<Elements xmlns="http://schemas.microsoft.com/sharepoint/" >
       <CustomAction
              Id="MyChangePassword"
              Location="Microsoft.SharePoint.StandardMenu"
              GroupId="PersonalActions"
              ControlAssembly="$SharePoint.Project.AssemblyFullName$"
              ControlClass="<NAMESPACE>.ChangePasswordMenuItem">
       </CustomAction>
</Elements>

The CustomAction tag in the elements file causes SharePoint to register a new menu item in the Standard Menu.  You can cut and past my code here, but you'll need to replace <NAMESPACE> with the namespace you're using in your project.

Next, you'll need to add a destination for the custom ASPX page you develop.  Remember I called mine ChangePassword.aspx?  We'll it's also in a project folders called SupportComponents\Style Library\AccountManagement, and I created a custom icon for my menu item that looks like a pair of keys on a key ring.  I actually modified a couple of other SharePoint icons to come up with this one.  It's pictured in the image at the top of this post.


<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
   <Module Name="SupportComponents" RootWebOnly="TRUE" Url="Style Library/AccountManagement">
      <File Path="SupportComponents\Style Library\AccountManagement\ChangePassword.aspx"
            Url="ChangePassword.aspx" />
      <File Path="SupportComponents\Style Library\AccountManagement\CP.png"  
            Url="CP.png" />
   </Module> 
</Elements>

This element file modification will copy both of my files to the ~/Style Library/AccountManagement folder.

So, once this project is deployed, we now have a custom action in the Standard Menu, that references a page in the Style Library, and that page hosts our web part that allows the user to change his Active Directory Password.


No comments:

Post a Comment