Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I got this exception in very easy code and I have running with elevated privileges around it.

 private void ChangeVersioningOnDocumentLibrary(SPListItem item, SPItemEventProperties properties, SPSite site)
        {
            string sitename = item[MeetingsCommon.Constants.FIELDS_TEXT_TITLE_NAME].ToString();
            string prefix = item[MeetingsCommon.Constants.FIELDS_TEXT_TITLE_NAME].ToString().Substring(0, 2);
            bool isConfirmed = item.TaxonomyFieldValueIsGivenValue(properties.AfterProperties, MeetingsCommon.Constants.FIELDS_MEETINGSTATUS_NAME, MeetingsCommon.Constants.TERMVALUE_MEETINGSTATUS_CONFIRMED, 1033);
            bool isPublished = item.TaxonomyFieldValueIsGivenValue(properties.AfterProperties, MeetingsCommon.Constants.FIELDS_MEETINGSTATUS_NAME, MeetingsCommon.Constants.TERMVALUE_MEETINGSTATUS_PUBLISHED, 1033);

            if (isConfirmed)
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPWeb web = site.OpenWeb(prefix + "/" + sitename))
                    {

                            if (web.Exists)
                            {
                                web.AllowUnsafeUpdates = true;
                                SPList agendaPoints = web.GetSafeListByName(MeetingsCommon.Constants.LISTS_AGENDAPOINTS_NAME);
                                agendaPoints.EnableModeration = true;
                                agendaPoints.DraftVersionVisibility = DraftVisibilityType.Author;
                                agendaPoints.Update();
                                web.AllowUnsafeUpdates = false;
                            }

                    }
                });
            }
            if (isPublished)
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPWeb web = site.OpenWeb(prefix + "/" + sitename))
                    {                    
                        if (web.Exists)
                        {
                            web.AllowUnsafeUpdates = true;
                            SPList agendaPoints = web.GetSafeListByName(MeetingsCommon.Constants.LISTS_AGENDAPOINTS_NAME);
                            agendaPoints.EnableModeration = false;
                            agendaPoints.DraftVersionVisibility = DraftVisibilityType.Author;
                            agendaPoints.Update();
                            web.AllowUnsafeUpdates = false;
                        }                    
                    }
                });
            }
        }
share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

You are opening the web from the current context of SPSite, thus not actually elevating privledges. Open the site again within the delegate to elevate priviledges.

For example:

var id = site.ID;

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite newSite = new SPSite(id))
    {
        using (SPWeb web = newSite.OpenWeb(prefix + "/" + sitename))
        {
            ...
        }
    }
});
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.