Storing data in the user profile in Sitecore PowerShell Extensions
Sitecore PowerShell Extensions has a very powerful dialog feature that allows scripts to be written that create dialogs for capturing information from the user.
This feature is used by a great deal of the scripts that come bundled with SPE, such as the broken links report:
And the package generator:
If you have a script featuring a dialog that you regularly execute, you might want to have your most recently used values saved, so that next time you execute the script you can skip through the dialog. Fortunately, this is actually quite easy to accomplish.
Using the User Profile
There are a number of different locations you could use to persist this data. Assuming you wish to save the values on a per-user basis, one of the easiest methods is to use the ASP.NET User Profile. This is part of the ASP.NET Membership Provider that Sitecore’s user management is built upon. Using the profile means that the data will be stored within SQL Server and therefore persisted across multiple browser sessions and machines. Furthermore, the Sitecore API and SPE makes accessing this profile very simple.
Getting the current user and setting / getting a profile value from it couldn’t be simpler:
|
|
Straightforward, right?
A sample script
Let’s use the above example and apply it to a script that uses a dialog. In this example, we will show a simple dialog with a dropdown list. When the dialog is completed, the value that was selected will be stored in the current user’s profile, ready for next time:
|
|
If you execute the script, you will see that once you have completed the dialog once, the value you last selected is preselected for you the next time you run the script. Easy, right?