If you ever wanted to relate Sitefinity users to one of your Dynamic Content Modules, you probably noticed that there is not an easy, out-of-the-box way to do that. Luckily, Sitefinity is designed for extensibility from the ground up so this relation is quite easy to achieve. This blog post will detail how you can do that in few simple steps. For your convenience, the source code needed for this feature is already implemented, and the only thing left to do is integrate it into your Sitefinity application. The solution is based on a field control that lets you select one or many user which are going to be related to your dynamic content items. On the dynamic content side, the users are stored as an array of user identifiers (Guid). Both of these functionalities, the custom field control and the array of GUIDs field, have been supported by Sitefinity for a long time. We are now just using their power to create a useful feature which many of you have been asking for or trying to do.
I am not going to explain the implementation of the RelatedUsers field control, because that is not the purpose of this post, but I will just explain how to integrate it to your Dynamic Content Module. You can find the source of the field control in this GitHub repository.
For the sake of a simple example I would assume that your web application has the default name – SitefinityWebApp, and that you use the same folder structure as in the sample: ~/FieldControls/RelatedUsers. If the conditions above are fulfilled, the CLR type of the field control should be: SitefinityWebApp.FieldControls.RelatedUsers.RelatedUsersField
Now build your solution, run the Sitefinity application and go to the Module Builder screen. There you can either create a new Dynamic module or you can edit ones that you already have. Then add a new field, give it a name by your preference and a type Array of GUIDs. Uncheck the This is a hidden field checkbox and enter the CLR type of the RelatedUsers field control in the Type or Virtual path of the custom widget input field: SitefinityWebApp.FieldControls.RelatedUsers.RelatedUsersField. The following screenshot shows the final setup of the field:
Now if you go to your content module and add/edit an item, you will see the RelatedUsers field.
If the current behavior of the user selector does not fit your needs, you can additionally tweak a lot of it. The user selector is based on the Sitefinity FlatSelector, which means that all of the FlatSelector options can be changed. The easiest way to do this is through the template file of the user selector field (RelatedUsersField.ascx), by changing the attributes of the FlatSelector control. Popular tweaks for this control are: AllowPaging, PageSize, AllowMultipleSelection, AllowSearching, ShowProvidersList, etc. See the following screenshot of the current configuration:
Moreover, you can go crazy and further develop some additional functionality which you might need, because you have all of the source code of this selector.
I hope you will find this field control useful. If you run into some troubles or have any questions, tell me about it in the comments bellow.