문제

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.AssociatedMemberGroup.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();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top