| By Brad Abrams | Article Rating: |
|
| April 15, 2009 02:00 AM EDT | Reads: |
43,900 |
Part 2: Save Personalization Settings
In this part I will show how to leverage the ASP.NET profile system to store and retrieve data tied to a particular user. The beautiful thing about this is there is no explicit database configuration required.
First, let's go back to the ASP.NET server project and add the ProfileService.svc. The easiest way to do this might be to copy the AuthenticationService.svc change the file name and then change the contents as show below.
<%@ ServiceHost Language="C#" Service=
"System.Web.ApplicationServices.
ProfileService" %>
Now, we need to enable the service via WCF configuration in web.config. This looks just like the config for the prfofile service. In the system.serviceModel\services section add a new node.
<!-- this enables the WCF
ProfileService endpoint --> <service name=
"System.Web.ApplicationServices.
ProfileService" behaviorConfiguration=
"ProfileServiceTypeBehaviors"> <endpoint contract=
"System.Web.ApplicationServices.
ProfileService" binding="basicHttpBinding"
bindingConfiguration="userHttp" bindingNamespace=
"http://asp.net/
ApplicationServices/v200"/> </service>
and in the system.serviceModel\behaviors\serviceBehaviors add a new node
<behavior name=
"ProfileServiceTypeBehaviors"> <serviceMetadata
httpGetEnabled="true"/> </behavior>
Now we need to configure the profile system. Again, in web.config add a section listing all the profile properties. In the System.web section, add the following node.
<profile> <properties> <add name="Color" type=
"string" defaultValue="Red" /> </properties> </profile>
Now we need to enable profile service to be accessed from the webservice. To do this, add the following node to the system.web.extensions\scripting\webservices section.
<profileService enabled=
"true" readAccessProperties=
"Color" writeAccessProperties=
"Color"/>
Now, let's go to the Silverlight client and add a reference to this service. This is the same as we did authentication service. Add Service Reference, Discover and select the profileService.
Now, we need to add a little UI to the page.xaml in order to give us a way to view and edit the personalized setting.
<StackPanel Orientation=
"Horizontal" HorizontalAlignment=
"Center"> <TextBlock>Favorite Color:
</TextBlock> <TextBox x:Name=
"colorNameBox"
Width="100" Height=
"25"></TextBox> <Button x:Name=
"submitButton"
Width="50" Height="25" Content="submit" Click=
"submitButton_Click">
</Button> </StackPanel>
Now, let's extend loginComplete to retrieve all the profile properties for the user that just logged in. Again, we need to do that asynchronously so we don't block the browser.
void client_LoginCompleted
(object sender,
LoginCompletedEventArgs e) { if (e.Error != null) statusText.Text =
e.Error.ToString(); else { statusText.Text = e.UserState +
" logged In result:" + e.Result; ProfileServiceClient client =
new ProfileServiceClient(); client.GetAllProperties
ForCurrentUserAsync(false); client.GetAllProperties
ForCurrentUserCompleted +=
new EventHandler
<GetAllPropertiesFor
CurrentUserCompletedEventArgs>
(client_GetAllProperties
ForCurrentUserCompleted); } }
When we get the property values back, we just set the LayoutRoot to have that background.
void client_GetAllProperties
ForCurrentUserCompleted
(object sender,
GetAllPropertiesForCurrent
UserCompletedEventArgs e) { if (e.Error == null) { colorNameBox.Text =
e.Result["Color"]; ChangeBackgroundColor
(e.Result["Color"]); } }
private void ChangeBackgroundColor
(string colorName) { SolidColorBrush brush =
new SolidColorBrush(); switch (colorName.ToLower()) { case "black": brush.Color = Colors.Black; break; case "blue": brush.Color = Colors.Blue; break; case "brown": brush.Color = Colors.Brown; break; case "green": brush.Color = Colors.Green; break; case "orange": brush.Color = Colors.Orange; break; case "purple": brush.Color = Colors.Purple; break; case "yellow": brush.Color = Colors.Yellow; break; case "red": brush.Color = Colors.Red; break; case "white": default: brush.Color = Colors.White; break; } LayoutRoot.Background = brush; }
Finally, when the submit button is pressed we need to set the value on the server.. for completeness, I show waiting until the result comes back from the server before setting the background locally.
private void submitButton_Click
(object sender, RoutedEventArgs e) { ProfileServiceClient client =
new ProfileServiceClient(); Dictionary<string, object> properites =
new Dictionary<string,object>(); properites.Add("Color",colorNameBox.Text); client.SetPropertiesForCurrentUserAsync
(properites, false, properites); client.SetProperties
ForCurrentUserCompleted +=
new EventHandler
<SetPropertiesForCurrent
UserCompletedEventArgs>
(client_SetPropertiesFor
CurrentUserCompleted); } void client_SetPropertiesFor
CurrentUserCompleted
(object sender,
SetPropertiesForCurrentUser
CompletedEventArgs e) { Dictionary<string, object>
properites = e.UserState as
Dictionary<string, object>; ChangeBackgroundColor
((string)properites["Color"]); }
The end result looks good! Notice the employee and the manager can each have different values for color.
Next Page - Part 3. Enable Custom UI Based on a User's Role
Published April 15, 2009 Reads 43,900
Copyright © 2009 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Brad Abrams
Brad Abrams is currently the Group Program Manager for the UI Framework and Services team at Microsoft which is responsible for delivering the developer platform that spans both client and web based applications, as well as the common services that are available to all applications. Specific technologies owned by this team include ASP.NET, Atlas and Windows Forms. He was a founding member of both the Common Language Runtime, and .NET Framework teams.
Brad has been designing parts of the .NET Framework since 1998 when he started his framework design career building the BCL (Base Class Library) that ships as a core part of the .NET Framework. He was also the lead editor on the Common Language Specification (CLS), the .NET Framework Design Guidelines, the libraries in the ECMA\ISO CLI Standard, and has been deeply involved with the WinFX and Windows Vista efforts from their beginning.
He co-authored Programming in the .NET Environment, and was editor on .NET Framework Standard Library Annotated Reference Vol 1 and Vol 2 and the Framework Design Guidelines.
![]() |
radixweb 08/21/08 01:44:07 AM EDT | |||
Hey Brad, Great Post..... Thanks... |
||||
![]() |
Nico 06/04/08 06:15:34 PM EDT | |||
This is great! Been looking forward to seeing more AJAX with Silverlight and this is right up my alley. Now that I know how it works... do you think there's a version out there with advanced escaping or is this security sound across the board? |
||||
- Qt DevDays 2009 - Munich
- The Power of Google and the Promise of Cloud Computing
- Unlocking the Cloud with Enterprise Private PaaS
- Big Data Kills 30-Year-Old Market
- Securing the Cloud and Establishing a Level of Trust
- ExaGrid Sets New Standard in Backup Price, Performance and Capacity with Launch of EX10000E Disk Backup System with Data Deduplication and Expanded 100TB GRID Capacity
- Cloud Computing: Transformative Technology With Financial Benefits
- The Enterprise Private Cloud - From Infrastructure to Applications
- Moving HPC Apps to the Cloud: The Practitioner's Perspective
- Business Service Management: Aligning Business & IT
- IGEL and Quest Software Advance Virtual Desktop Management by Integrating Quest vWorkspace into IGEL Universal Desktops
- World's First 16GB, 2 Virtual Rank Memory Module
- Is Microsoft as Free as Open Source?
- IBM’s Linux-Based ‘Cloud-in-a-Box’ Makes its First Sale
- United Planet offers practical portal building tips for SMBs
- Qt DevDays 2009 - Munich
- The Power of Google and the Promise of Cloud Computing
- Developing APIs for the Cloud
- Unlocking the Cloud with Enterprise Private PaaS
- Testing the Limits with Jack Margo SVP of Developer Shed, (part 1)
- The Bunker achieves PCI DSS Compliance
- Big Data Kills 30-Year-Old Market
- Securing the Cloud and Establishing a Level of Trust
- Excuse Me But Is That a Gazebo On Your Site?!
- The Top 250 Players in the Cloud Computing Ecosystem
- Red Hat Named "Platinum Sponsor" of Virtualization Conference & Expo
- An Introduction to Ant
- Google Web Toolkit: Finally Java Has Been Put into JavaScript!
- AJAX World RIA Conference News - AJAX & RIA with Server-Side JavaScript
- Python Creator Guido van Rossum to Present the Next-Generation Python 3000
- White Paper: "Extended Validation SSL Certificates"
- CEO of Hyperic, Javier Soltero on SYS-CON.TV
- Rating JRuby, Jython, and Groovy on the Java Platform
- Perforce Software Delivers State-of-the-Art Application Lifecycle Management
- TurboGears - Python-Based Framework for AJAX Web Development
- iPhone 3G Only Looks Cheaper






























