0

I have crated a custom site that would provide registration for new user who wold like to access my sharepoint site with asp.net membership account. When new user clicks register, i would like to send email notification about new user to all members of a particular sharepoint group. The problem is, the registration site allows anonymous access (well it has to :)) but the code (second line) that gets all the users in group redirects me to a login page:

var web = SPContext.Current.Web;
return web.Groups[groupName].Users;

I have created a new user group and set 'Who can view the membership of the group?' to everyone, but still, I can't get the groups without being logged in. Is is possible at all?

matt99
  • 869
  • 2
  • 8
  • 19

2 Answers2

0

SharePoint has the ability to run code blocks using RunWithElevatedPrivileges, which runs under the identity of the SharePoint system account. If you wrap your code block above, you should be able to get the group you are referencing.

It is really important to make sure you are properly calling Dispose on your code so you are not leaving around reference to the spSite object etc. As a result, almost all RunWithElevatedPrivileges examples utilize the using construct.

More info at

http://msdn.microsoft.com/en-us/library/bb466220.aspx

John Ptacek
  • 1,886
  • 1
  • 15
  • 20
  • This seems like something that should solve the problem, however it does not in my case, i still get redirected to my login page. maybe it has something to do with site properties, but i believe i have all them set ok. – matt99 Apr 26 '11 at 12:02
  • I'll ad my code just in case, maybe there's something i do wriong here and cant see it: SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite ElevatedsiteColl = new SPSite(SPContext.Current.Site.ID)) { using (SPWeb ElevatedSite = ElevatedsiteColl.OpenWeb(SPContext.Current.Web.ID)) { userColl = ElevatedSite.SiteGroups[groupName].Users; } } }); – matt99 Apr 26 '11 at 12:03
  • Hard to tell from the code, but you need to "use" the object you want to reference within the ElevatedPrivileges block. It falls out of scope once you leave the block or will no longer run under the higher security context – John Ptacek Apr 26 '11 at 12:42
  • ifound out that the code actualy worked on other computer, i can't figure out the reason (probably some configuration problem) why it did not work at my computer – matt99 May 09 '11 at 09:12
0

When I did this before, I created a list that allowed anonymous users to create new items and then placed an alert on the list that sent notifications to the appropriate people/group. I don't remember there being any security problems sending notifications this way.

Rich Bennema
  • 10,295
  • 4
  • 37
  • 58