Reputation: 27803
I am trying to create a command line quick program to do a once-off import of users from an old system (non-DNN) to a new system. However, a NullReferenceException
gets thrown at the following line of code:
var user = new UserInfo();
user.PortalID = portalId;
user.FirstName = firstName;
On that last line is where the exception occurs. I know this code works when run in a module, as it's part of a library I'm using. I imagine that this is erroring because the UserInfo
class is relying on information that's usually setup in a web environment.
Is there any way I can do this? I really don't want to have this as a module running on a production site.
Upvotes: 3
Views: 951
Reputation: 6962
You need to configure the necessary providers in order to use the UserController
and UserInfo
classes. The most straightforward way to do this is to use the website's working configuration and implement the app as a DNN module.
But you can also try to copy the required DLL's and configuration sections from the DNN site to the console application, and use the DNN source to debug problems.
In this case, the source tells that setting the UserInfo
object's FirstName
property fails because the FirstName
property is backed by the profile provider, which uses the caching provider and data provider for data access.
By default, profile is implemented by the DNNProfileProvider
that uses the FileBasedCachingProvider
and SqlDataProvider
to get the profile properties and data. Profile property definitions are retrieved also for a new UserInfo
object when ProfileController.GetUserProfile
is called. That is why the NullReferenceException
gets thrown.
The corresponding properties in DNN 5.6.3 are:
UserInfo.vb
<SortOrder(1), MaxLength(50), Required(True)> _
Public Property FirstName() As String
Get
Return Profile.FirstName
End Get
Set(ByVal Value As String)
Profile.FirstName = Value
End Set
End Property
<Browsable(False)> _
Public Property Profile() As UserProfile
Get
'implemented progressive hydration
'this object will be hydrated on demand
If _Profile Is Nothing Then
_Profile = New UserProfile
ProfileController.GetUserProfile(Me)
End If
Return _Profile
End Get
Set(ByVal Value As UserProfile)
_Profile = Value
End Set
End Property
Upvotes: 1