April 18th, 2008Making "user-aware" Web Parts
I have currently been tasked with the duty of providing a web part that displays patient satisfaction scores for the hospital. However, there was one stipulation:
“Can you make the web part reflect the unit that the nurse is working on?”.
In other words, could I create a web part that would observe the user’s login and display appropriate information based on where, in the hospital, that employee worked. In this case, the requester (ahem, my boss) wanted the scores to display for the nurse’s unit (think of a unit as a “wing” or “floor” in a hospital).
Provocative.
Right off the bat, I figured the best way to access the nurse’s unit was by pulling the “Department” property from our Active Directory schema.
But then I thought: “Why not build a function that would return any property I might want from Active Directory?” That way, from here on out, as I am building my web parts I could make decisions of the data to display based on a user’s department, job title, director, etc.
Essentially, creating web parts that are “user-aware”.
Good idea self!
Below is a namespace (ActiveDirectoryInfo) , class (UserInfo), and function (GetPropertyValue) that I created to do just this:
using Microsoft.Office.Server.Audience;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Text;
using System.Web;
namespace ActiveDirectoryInfo
{
class UserInfo
{
public String GetPropertyValue(string PropKey)
{
//Get user’s Department from Active Directory
SPWeb spw = SPControl.GetContextWeb(HttpContext.Current); //Establishes the current SharePoint site.
SPUser spu = spw.CurrentUser; //Grabs user information from the logged in state of the user.
DirectoryEntry dentry = new DirectoryEntry(@”LDAP://DC=<hidden>,DC=<hidden>”, “<domain_user>”, “<domain_user_password>”);
DirectorySearcher dsearch = new DirectorySearcher(dentry);
String retUserName= “”;
dsearch.Filter = “(objectSid=” + spu.Sid + “)”; //Filters the search result based on the user’s SID.
//dsearch.Filter = “(sAMAccountName=” + spu.LoginName.Replace(@”Domain\”, “”) + “)”; //Filters the search result based on the user’s login (without Domain\).
dsearch.PropertiesToLoad.Add(PropKey); //Loads the specific property to be viewed.
SearchResult result = dsearch.FindOne(); //Peforms the search for one matching record.
if (null == result)
{
//Nothing
}
else
{
retUserName = result.Properties[PropKey][0].ToString();
}
return retUserName;
}
}
}
So here is how we might call upon this function within a web part:
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
UserInfo ui = new ActiveDirectoryInfo.UserInfo();
writer.Write(ui.GetPropertyValue(“cn”)); //Returns user’s full name
writer.Write(ui.GetPropertyValue(“mail”)); //Returns user’s e-mail
writer.Write(ui.GetPropertyValue(“Department”)); //Returns user’s Department
}
April 18th, 2008 at 9:45 am
I am running into an issue using the System.DirectoryServices reference. How did you get it to work without setting full control in the web .config?
April 18th, 2008 at 9:57 am
Hmmm well, I added System.DirectoryServices as a reference within my project. Maybe that did it?
Steps:
1. Right-click on your project in the Solution Explorer.
2. Select “Add a Reference…”
3. Choose System.DirectoryServices from the .NET tab
4. Click “OK”
I’m afraid I don’t know what you mean by “setting full control in the web.config”. It’s possible I have a setting there that you don’t have, however, in building this I made no web.config modifications.
April 18th, 2008 at 10:15 am
Well the huge difference is that I am using a Web User Control. I should have been more explanatory, but there is a WSS_Minimal, WSS_Medium, and Full as Trust Levels. I tried to encapsulate the user control in a web part, rather than using the SMARTPart and for some reason it’s still not working. I may try again. Maybe take all the code from the user control and just write a full on web part and see what it does. I have been a little spoiled in the past few months with the Web User Control UI. I should get back to my roots.
April 21st, 2008 at 9:30 pm
[...] travislowdermilk.com » Blog Archive » Making “user-aware” Web Parts Some general tips for accessing users logged in data (tags: sharepoint authentication webpart) Posted by Richard@Home Filed in 15 [...]
April 22nd, 2008 at 3:27 pm
Becky – I figured out a way to get it to work using the User Control, Smart Part, method.
I copied the System.DirectoryServices.dll from my computer (Mine was located in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727) to inetpub\wwwroot\wss\VirtualDirectories\(your_domain:port)\bin folder.
Now it appears to be working in a Smart Part.
Hopefully that helps!
February 3rd, 2009 at 6:01 am
Travis,
THANK YOU!!!!! I don’t know why having a copy of the file in /bin is required, or if there’s an “official” way of creating the reference, but yours was the only reference I could find that revealed this. Of course, not everyone uses SmartPart extensively, but it seemed odd not to have run into it elsewhere ;o)
This worked great for me too- having AD access makes a world of difference.
Rob
March 16th, 2010 at 8:48 am
[...] SharePoint provides that information by way of collecting your network credentials. I wrote a blog post about how you can go about getting that kind of user information from SharePoint. I used the same [...]