Development

Add a Dynamic Content selector to a User Profile

For a project I was working on I needed to create a relation between Projects (which is an entity from my Sitefinity Module Builder) and a Sitefinity User. There are a couple of ways to do this and I choose one that fits the actual need of my client.

Daniel Plomp

April 15th, 2019

Introduction

For a project, I was working on I needed to create a relation between Projects (which is an entity from my Sitefinity Module Builder) and a Sitefinity User. There are a couple of ways to do this and I choose one that fits the actual need of my client.

What do we need?

  1. We first need a Dynamic Module, created with the Module Builder that holds project information. In my case, this was a really simple entity. Title, Date, Documents.

  2. Second, we create a selector field, which allows us to pick one or more projects. The results of this selector are returned as a Guid array.

  3. The last thing we need is a new custom field in our user profile which will hold the Projects that we select through our selector.

The problem

Sounds fairly simple, but the thing is that through the Sitefinity interface it is currently not possible to add custom fields of type Guid[] (a Guid array). So even if we can enter a custom field like our project selector, created by Sitefinity Thunder, it wouldn't work, since it expects that the results are stored inside this field of type Guid[].

The solution

At this time we can't do anything other than doing this through code, by using the Sitefinity API. We can create a new custom field of type Guid[] and then edit all the views to add the project selector. These views are for example when you create a user, edit a user, etc.

The following method helps us create a new field and the related selector. It is a generic method, so you can use it for other field elements also.

We also use this helper method to check if the field exists:

We can use this code inside a simple .aspx page with a button to execute or add it to an e.g. helper library.

To call the method, it will look something like this:

RegisterFieldForUserProfile<ProjectSelectorElement>("Telerik.Sitefinity.Security.Model.SitefinityProfile", "Projects");

So when this is executed, the new field is created and we now have our project selector added to the user profile.

So there we have our solution. Thanks to Atanas Valchev for helping me with this.

Check out this weblog or sitefinity.com if you need more help and resources.

All rights reserved © ZimplerApps 2019