简体   繁体   中英

Cannot add user to Sharepoint 2010 within event handler

Using both synchronous and asycnhronous event handlers (itemAdded and itemAdding), I have been unable to add a user to my Sharepoint 2010 site - using Forms Based Authentication (which I know to be properly setup and working because it has been working perfectly from the Sharepoint UI from what I can tell)

Calling Membership.CreateUser is not working and constantly throws an exception of "object reference not set to an instance of an object". Debugging and looking into Membership, it is clear that something is wrong. Most members are null or throw exceptions as well. I know Membership doesn't have this issue in an Application Page (I can add users, add them to groups, etc.. from an Application page, but it fails inside of an event handler). We originally thought it was a HttpContext issue, but we can get HttpContext.Current inside itemAdding(), so that might not make much difference.

Any clues? Is it possible to add users to SP2010 within an event handler?

Background: when I add a new row to a list (with an Email column), permissions are granted to the user with the specified email address - if that user doesn't exist I want to create it (again, for Forms Based Authentication)

Im have never done any share point development but it sounds like a cross thread problem.. The event will create a new thread which wont have acces to the thread that contains 'Membership'. try passing it through using a delegate? eg

Object.ItemAdded+= (o, args) => Class.Method(o, args, Membership);

It seems to me that your httpcontext.current is null. At least i had the same problem and that was the reason. Check this http://kbochevski.blogspot.com/2011/01/accessing-membership-database-inside.html I hope it may be of help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM