A good “friend of the blog”, Israel V., was good enough to point out to me recently that pretty much all of the code samples that we have for custom claims providers contain an irritating little flaw – if you follow these samples then the welcome emails that get sent out when you add a new person to a site will not be sent out. I, of course, am as guilty as anyone of this, so I took a look at the situation a little closer, as well as a quick review of some code that Israel had developed to work around this problem.
In a nutshell, you will see this problem happen if you are adding a user to a site collection for the very first time and so there is no email address associated with that person – because a profile sync hasn’t occurred or whatever. So, as you can imagine the key here (and I’m boiling this down to the simplest case scenario) is to grab an email address for the user at the time they are added and then plug it into the appropriate property in your PickerEntity class. Now let’s talk about some of the particulars.
WHERE you get the email address from is going to totally depend on your claims provider. If you’re pulling your data from Active Directory then you can query AD to get it. If you’re using SAML and email address is the identity claim, then you can simply reuse that. Basically, “it depends” so you’ll need to make the call here.
WHEN you want to use it is when the FillResolve method is called. As you know, this method can be called either after someone adds an entry via the People Picker or when they type a value in the type in control and click the resolve button. As I’ve shown in many of my code samples, during that process you will create an instance of the PickerEntity class so that you can add it to the List<PickerEntity> that is passed into the method.
HOW you add it is just to set the property on the PickerEntity instance like so:
//needed to make welcome emails work:
pe.EntityData[PeopleEditorEntityDataKeys.Email] = "firstname.lastname@example.org";
In this example “pe” is just the instance of the PickerEntity class that I created and return to my FillResolve method.
That’s all there really is too it. The biggest trick may very well be just getting the email address value. Once you have it though it’s pretty easy to add it to the PickerEntity to ensure your welcome emails will work. I tested this out and verified that both a) the welcome emails were not getting sent out with my original custom claims provider and b) they DID start going out after incorporating this change. Thanks again to Israel V. for the heads up and code sample on this issue.