Friday, September 12, 2008
Bridge SharePoint - User Profiles and User Profile Properties (part 2 of 2)
As we have found out in part 1, SharePoint can manipulate data using the user profile management objects and can accept data from external sources. Using these two features apart we have two limited tools with limited usage, but using them together gives us a powerful, flexible and efficient method of storing and using data from any internal or external resource.
It’s time to see this solution to our problems in action.
Code example of getting/setting the profile property from a C# application
The concept is the same for any source of information (web services, web applications, etc.):
Get the user’s profile->Get the required profile property->Get/Set profile property value
Code example: (note that to access profile properties, this code must run with elevated privileges)
// Change the site address for different deployment environments
// Note: WIN2003STD is a place holder for your environment, usually your server name
SPSite site = new SPSite("http://WIN2003STD/");
SPWeb web = site.OpenWeb();
// Get the profile manager object for the site
UserProfileManager profileManager = new UserProfileManager(ServerContext.GetContext(site));
// Use the username from the User Information Item to get the full profile of an user
UserProfile user_profile = profileManager.GetUserProfile(System.Web.HttpContext.Current.User.Identity.Name);
// Get the required profile property value from the profile
value = user_profile["MyProperty"].Value.ToString();
catch (Exception e)
return "Error getting user info: " + e.Message.ToString();
user_profile["MyProperty"].Value gets or sets the profile property value.
This is a very convenient way of using profile properties to store required information for each user. Properties can be set as read-only or not to appear in the user’s profile, which gives you even more control.System.Web.HttpContext.Current.User.Identity.Name returns the username (the login username) and it is used in the code example above to get the profile of the currently logged-in user.
Tools You Never Knew You Had… (and what to do with them now that you’ve wised up!)With this simple solution to getting/setting a user’s profile property value, endless opportunities are now at hand. This small code snippet helps developers control, validate and use values stored for each user, without corrupting the database or resorting to other more complex and error-prone solutions.
For those not needing to use an external application to get information from the users, a custom SharePoint web application can be developed and deployed on SharePoint. This way all the controls offered by ASP .NET or custom controls can be used to perform required operations on the data before it’s stored.
External applications running on the server can access this information the same way, so a bridge between SharePoint and applications like web sites, game servers, messaging apps, etc. can be easily created.
SharePoint Complications, As Usual
Watch out that the profile property might be set as read-only in SharePoint. Even if it’s the case however, the above code should still be able to access the profile because it’s running with elevated privileges. So if there is a situation in which the user is allowed to see the data but only modify it by using a service (like a web application or web service), this is a good way of doing it.Another gotcha is that the code above can only be used on the SharePoint server machine. This is due to the framework that SharePoint uses. To get information or to change data from a network on internet location, a SharePoint custom web service or web application can come in handy. Other ways of passing information will work too, like server-client applications, as long as the part running the above code is on the SharePoint server machine.
So we now have a way to store data and manipulate it according to our needs. We can control it, we can validate it and most important of all, we decide how the user interacts with the data.We’re now ready to start doing some serious SharePoint development!
Labels: c#, code quality, SharePoint, string conversion, web development, Win32
Wow, awesome blog layout! How long have you been blogging for?
you made blogging look easy. The overall look of your
site is magnificent, let alone the content!
my web page :: 안마
I was very hapρy to discover thiѕ site. I neｅd tօ
to tһank you for oneѕ time jᥙst foг this fantastic гead!!
I definiteⅼy loved eѵery рart оf it and i also
hɑve you book marked tօ look ɑt new stuff on ʏour site.
his article is very effective and valuable. I am very glad to read your blog. I hope you will soon share your next post about this discussion. Thanks for sharing and keep sharing
chapter 7 bankruptcy lawyer near me