none
Console application to add a user to a sharepoint group using a custom membership provider

    Question

  • string mprovider = spSite.WebApplication.IisSettings[SPUrlZone.Internet].MembershipProvider;
    SPUser spUser = spWeb.EnsureUser(mprovider + ":" + iUser);

    I want to write a tiny console application which adds a certain user to a certain sharepoint group.
    The web application is extended properly, the custom membership provider is working perfectly within central administration.

    However I'm unable to find any user using the method above. It always says it couldn't find any users. I think it doesn't even use my membership provider settings configured for that zone (spsite and spweb are in the SPUrlZOne.Internet zone and should therefore use my custom membership provider).

    Any idea how I can get/find my user from command line?

    Monday, October 26, 2009 11:44 AM

Answers

  • Hi,

             As your information, you have got user could not be found Exception with EnsureUser method when using custom membership/role providers in your Console application.

             It is SPcontext caused this problem in my mind, as your web application has access to a context object and also has access to the your web.config file, but the console application does not have this information available. 

             So the SharePoint assemblies can’t access this information in console application as this did not exist.

             Would you please try this:

    A.       Create an HttpContext yourself before using spWeb.EnsureUser

     

    B.      Change your app.config and add nodes as web.config

     

             Some articles and solutions for the same problem:

             http://www.calvinirwin.net/2009/08/05/sharepoint-spweb-ensureuser-and-thecustom-membership-provider/

             http://blog.mastykarz.nl/inconvenient-programmatically-sharepoint-users-spweb-ensureuser/

     

             Hope this can help.     

    Best Regards,

    -Aaron

    • Marked as answer by Venenum Thursday, October 29, 2009 11:34 AM
    Wednesday, October 28, 2009 6:54 AM

All replies

  • May be you want to first check  that your SPSite object is created in proper zone.Because if you intialize SPSite its always created in default zone.
    so may be you can change like this

    SPSite site = new SPSite(SPContext.Current.Site.Zone, SPContext.Current.Site.ID);


    http://razi.spaces.live.com/
    Monday, October 26, 2009 12:42 PM
  • Thanks for the input.

    As I'm writing a console application there's no such thing like SPContext (I guess).
    Hence I went for this solution:

    using (SPSite spSite = new SPSite(iSite))
    {
       using (SPSite extSite = new SPSite(spSite.ID, SPUrlZone.Internet))
       {
          // ...
       }
    }

    Unfortunately I cannot report any changes in my efforts to get my user object :-(

    SPException: The specified user MyMemberShipProvider:MyUser could not be found.

    Monday, October 26, 2009 3:08 PM
  • Hi,

             As your information, you have got user could not be found Exception with EnsureUser method when using custom membership/role providers in your Console application.

             It is SPcontext caused this problem in my mind, as your web application has access to a context object and also has access to the your web.config file, but the console application does not have this information available. 

             So the SharePoint assemblies can’t access this information in console application as this did not exist.

             Would you please try this:

    A.       Create an HttpContext yourself before using spWeb.EnsureUser

     

    B.      Change your app.config and add nodes as web.config

     

             Some articles and solutions for the same problem:

             http://www.calvinirwin.net/2009/08/05/sharepoint-spweb-ensureuser-and-thecustom-membership-provider/

             http://blog.mastykarz.nl/inconvenient-programmatically-sharepoint-users-spweb-ensureuser/

     

             Hope this can help.     

    Best Regards,

    -Aaron

    • Marked as answer by Venenum Thursday, October 29, 2009 11:34 AM
    Wednesday, October 28, 2009 6:54 AM
  • I understand what you are saying. Still I think it *should* work because I am using sharepoint api methods. They should be aware of the zone and its membership provider - why else would I be able to open a certain Web inside a specific site zone.
    Or is this really something exclusively available to SPContext?
    Wednesday, October 28, 2009 3:16 PM