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

In the following method, 'list' is simply an SPList ("Documents"). I'm attempting to add ContentTypes to the list and then update it. This code is found in the FeatureActivated event of an event receiver.

   list.ContentTypesEnabled = true;

    foreach (var contentTypeName in DocumentCTNames)
    {
        SPContentType ct = web.Site.RootWeb.ContentTypes[contentTypeName];

        if (ct != null && list.ContentTypes[ct.Name] == null)
        {
            list.ContentTypes.Add(ct);
        }
    }

    list.Update();

The line list.ContentTypes.Add(ct) gives me 2 inner exceptions:

"Make sure you have sufficient privileges to access this resource..."

and

"If you are attempting to access a file make sure it is not readonly..."

What I've tried:

  • Running with elevated priveleges (still same exception)
share|improve this question
1  
Just to be on the safe side, can you post the full code of the run with elevated privileges version? Also, can you specify which user you are using to activate the feature? Are you activating it from the web interface or comand line (for example in powershell)? –  SPArchaeologist Sep 26 '12 at 12:27
    
@SPArchaeologist - thanks for your reply. I'll get the EleveatedPrivileges code copied across asap and put into the OP. The site is created from the code-behind of an application page. The user, in this particular instance, is the System Account. I'm the site collection admin - I suppose, on reflection, this code shouldn't be without RunWithElevatedPrivileges obviously as not all users will be admin. –  DeeMac Sep 26 '12 at 12:39
    
Cracked it, I'll post my answer shortly for those landing on this page with a similar issue. Thanks for your response @SPArchaeologist –  DeeMac Sep 26 '12 at 12:49
    
what was your solution deemac? –  user11576 Oct 24 '12 at 13:54

1 Answer 1

Make sure to re-open a new SPSite inside the SPSecurity.RunWithElevatedPrivileges in order to run as the administrator.

share|improve this answer
    
Thanks a lot for your reply! I'm aware of this from coming across RunWithElevatedPrivileges not actually running with elevated privileges in a previous project - so my using statements are all in order, lessons learnt this time - I'll update the OP in a moment with my RunWithElevatedPrivileges 'version' of this code. Thanks again. –  DeeMac Sep 26 '12 at 12:41

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.