Alert.update() giving access denied in sharepoint?
I am trying to add alert for loging user using programming. It is giving "Access denied".
I am running this code in under SPSecurity.RunWithElevatedPrivileges(delegate()
. I checked application pool identity rights in all places. He is in Farm Admin group, Site collection group and database owner group. Still getting same error. Why this happening?
protected void ChkBx41_CheckedChanged(object sender, EventArgs e)
{
SPUser user = SPContext.Current.Web.CurrentUser;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPWeb eweb = new SPSite(url.Text).OpenWeb())
{
SPUser juser = null;
eweb.AllowUnsafeUpdates = true;
try
{
juser = eweb.AssociatedMemberGroup.Users[user.LoginName];
}
catch (Exception)
{
}
if (ChkBx41.Checked)
{
if (juser == null)
{
eweb.AssociatedMemberGroup.AddUser(eweb.EnsureUser(user.LoginName));
createalert(SPAlertFrequency.Daily, eweb, eweb.EnsureUser(user.LoginName),true);
}
RBList4.SelectedValue = "Daily";
RBList4.Enabled = true;
}
else
{
if (juser != null)
{
eweb.A开发者_JS百科ssociatedMemberGroup.RemoveUser(juser);
removealert(eweb, juser);
RBList4.SelectedValue = null;
RBList4.Enabled = false;
}
}
}
});
}
public void createalert(SPAlertFrequency frq, SPWeb pweb, SPUser puser,bool mail)
{
try
{
pweb.AllowUnsafeUpdates = true;
foreach (SPList lst in pweb.Lists)
{
if (lst.BaseTemplate == SPListTemplateType.Announcements || lst.BaseTemplate == SPListTemplateType.DiscussionBoard || lst.BaseTemplate == SPListTemplateType.Events )
{
SPAlert alt = puser.Alerts.Add();
alt.AlertType = SPAlertType.List;
alt.List = lst;
alt.EventType = SPEventType.All;
alt.AlertFrequency = frq;
alt.Title = "SPE " + lst.Title;
if (frq != SPAlertFrequency.Immediate)
{
alt.AlertTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 15, 0, 0);
}
if (mail)
{
alt.Update(); // Access denied exception
}
else
{
alt.Update(false);
}
}
}
}
pweb.AllowUnsafeUpdates = false;
}
catch (Exception ee) {
throw ee.Message;
}
pweb.Dispose();
}
I don't know if this is the solution. Can you please update eweb before moving ahead(Create/Delete Alert)
eweb.AssociatedMemberGroup.AddUser(eweb.EnsureUser(user.LoginName));
eweb.AssociatedMemberGroup.Update();
eweb.update();
精彩评论