Getting into Context with your CSJobs
While working on an add-on that forces users to change their passwords in CS, I stumbled upon a problem that I've seen others also have had problems with. To be able to get a context for certain operations/calls within the CS API. For example to get a UserSet with the help of a UserQuery requires a Context within the CS API.
I got help from CarKnee over at CS.org on how to do that, and I thought it would be good to post it here as well for those of you that might do similar CSjobs. Now as CarKnee says there are a few CS Core jobs that already uses this code. But I believe it is important enough to lift it out as a good example.
So instead of doing the work directly in the Execute method. The IterateSiteSettings method is used and the work is done with a delegate method like this:
1: Public Sub Execute(ByVal node As System.Xml.XmlNode)
2: Implements CommunityServer.Configuration.IJob.Execute
3:
4: SiteSettingsManager.IterateSiteSettings(New _
5: SiteSettingsListIterator(AddressOf RunMyJob))
6: End Sub
7:
8: Private Sub RunMyJob(ByVal SettingsID As Integer)
9:
10: CSContext.Create(SettingsID)
11:
12: 'get the users here
13:
14: End Sub
In my case the work was to get a UserSet via a UserQuery, which I then could do within my RunMyJob method.
1: Dim objQuery As UserQuery = New CommunityServer.UserQuery()
2:
3: objQuery.Status = UserAccountStatus.Approved
4: objQuery.Order = SortOrder.Ascending
5: objQuery.IncludeHiddenUsers = False
6:
7: Dim objUserSet As UserSet = New UserSet()
8:
9: objUserSet = CommunityServer.Users.GetUsers(objQuery, False)
If you enjoyed this post Subscribe to my feed via RSS or e-mail!