We are currently working on a project that involves sharepoint. We have created a template web and that contains specific content that we would like to have duplicated each time we create a new web. We have setup a create web user control running in a webpart. This creates the web from the template, sets up the necessary permissions and adds some features.
We would like to have an alert setup for an email-enabled security group DOMAIN\sharepoint. We are using this VB.Net code:
Dim user As SPUser = w.EnsureUser("DOMAIN\sharepoint") Dim alert As SPAlert = w.Alerts.Add() alert.User = user alert.AlertType = SPAlertType.List alert.List = w.Lists("Information") alert.Title = w.Title & " - " & alert.Title alert.Properties("eventtypeindex") = "2" alert.AlertFrequency = SPAlertFrequency.Daily alert.Update(False)
This works for users who are domain administrators but does not work for people who only have full control at this site's level. They can however go in and manually add the alert once the site is created. I do not have any problems when creating this with my own login but I am a member of domain admins and thus an administrator of all sharepoint sites. The error the users who have Full Control of this site only gets is an Access Denied error.
Edit: I never really stated what I wanted out of this. I would like to know if there is a way to circumvent this or if I can create an eventReceiver of some sort that for when the web itself is created to add the alert.
I have changed the code to the following and now I am getting an error
Dim authorizedCode As SPSecurity.CodeToRunElevated = New SPSecurity.CodeToRunElevated(AddressOf AddAlert) SPSecurity.RunWithElevatedPrivileges(authorizedCode) Dim new_site As New SPSite(SPContext.Current.Site.ID, sysToken) Dim web As SPWeb = new_site.AllWebs(w.ID) Dim user As SPUser = web.EnsureUser("DOMAIN\sharepoint") Dim alert As SPAlert = web.Alerts.Add() alert.User = user alert.AlertType = SPAlertType.List alert.List = web.Lists("Information") alert.Title = web.Title & " - " & alert.Title alert.Properties("eventtypeindex") = "2" alert.AlertFrequency = SPAlertFrequency.Daily alert.Update(False)
sysToken is obtained in
Public Sub AddAlert() Dim lolcatKiller As SPSite = New SPSite(SPContext.Current.Site.ID) sysToken = lolcatKiller.SystemAccount.UserToken End Sub
But I am getting the exception
System.Runtime.InteropServices.COMException: The security validation for this page is invalid.
As far as I can tell I am still within the context of the systemuser