Ask a questionAsk a question
 

QuestionSecurityException, not understood

  • Tuesday, November 03, 2009 4:49 PMparri2bd Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I am getting a SecurityException and I am not sure why?  Easy enough.

    I am using the Administrator account to try and add a new web part.  It is throwing an exception here...

    SPDocumentLibrary

     

    listContractDocs = (SPDocumentLibrary)webCurrent.Lists["Contract Documents"];

    "Contract Documents" exists as a list.

    I next tried to run the caller with elevated privileges, and then the error moved to the elevated privileges block of code.

    Any thoughts?

All Replies

  • Tuesday, November 03, 2009 4:59 PMDave Hunter Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Can you post the code?  What permissions are set on the contract documents library?
    My SharePoint Blog - http://www.davehunter.co.uk/blog
  • Tuesday, November 03, 2009 6:32 PMparri2bd Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    This is the elevated privileges code...

    // call the method to setup all of the label controls
    SetupLabels();

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
          // call the method to setup all of the server controls not of type label
          SetupUserControls();
    });


    And I have Full Control over the list as well.
  • Tuesday, November 03, 2009 6:51 PMAvinashKT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    As per the error... you are trying to TypeCast a list into SPDocumentLibrary which may be of different type.
    Check the type of Contract Documents  

    Regards, Avinash | avinashkt.blogspot.com
  • Tuesday, November 03, 2009 8:10 PMparri2bd Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I created this document library myself for sand box testing, so I know that it is available for me to get to in the current web context.
  • Wednesday, November 04, 2009 7:01 AMconverscient Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    if you are using the web part as an (Farm) administrator, you probably do not need to run with elevated priviledges. Do try to cast them as a SPFolder object and see if it works

    Never stop learning.
  • Wednesday, November 04, 2009 7:05 AMAbdul Khalid Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,
    Can you post the error/excption message?
    Also, can you post the sharepoint specific code present within the "SetupUserControls();" function?


    Hope that helps, Abdul Khalid.
  • Wednesday, November 04, 2009 9:29 AMDave Hunter Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Also how are you running this code?  On the sharepoint server?
    My SharePoint Blog - http://www.davehunter.co.uk/blog
  • Wednesday, November 04, 2009 12:37 PMparri2bd Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    The code is running in a custom web part.  I am building ASP controls and attaching events to them, text, or other properties.

    The specific error is occuring in the code already posted above.  It occurs when I cast the Document Library out of the collection of lists, and then if I run with elevated privileges, it occurs in the delegate code.

    I am running this with the (Farm) administrator account.  Yes I probably should not have to use the run with elevated privileges.  I was just trying that as a solution.

    The cast is the second line in setupusercontrols().  The first line is getting the SPWeb object out of the current web context.
  • Wednesday, November 04, 2009 12:46 PMDave Hunter Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Can you post the code?  When using RunWithElevatedPrivileges you need to create an new instance of the SPSite and SPWeb so that these objects are created under the context of the system account.

    My SharePoint Blog - http://www.davehunter.co.uk/blog
  • Wednesday, November 04, 2009 5:42 PMparri2bd Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    SPSecurity.RunWithElevatedPrivileges(<span style="color:Blue">delegate</span>
    ()<br/>
    {
         <span style="color:Green">// call the method to setup all of the server controls not of type label</span>
    
         SetupUserControls();<br/>
    });<br/>
    
    .................<br/>
    
    <span style="color:Blue">private</span>
     <span style="color:Blue">void</span>
     SetupUserControls()<br/>
    {<br/>
         <span style="color:Green">// instantiate the drop down list (m_ddlSubPrograms)</span>
    
         m_ddlSubPrograms = <span style="color:Blue">new</span>
     DropDownList();<br/>
    
    
         <span style="color:Green">// create the items in the drop down list (m_ddlSubPrograms)</span>
    
         BindSubProgramsList();
    

    .................
    

    private
     void
     BindSubProgramsList()
    {
    // retrieve the current web object SPWeb webCurrent = SPContext.Current.Web;
    // get the document library called "Contract Documents". This will be the standard name and should not change SPDocumentLibrary listContractDocs = (SPDocumentLibrary)webCurrent.Lists["Contract Documents" ];

    The last line is where it errors out without the RunWithElevatedPrivileges. Right now it currently errors out in the
    RunWithElevatedPrivileges block, meaning it does not even go to the SetupUserControls() method
  • Wednesday, November 04, 2009 5:48 PMparri2bd Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    // call the method to setup all of the server controls not of type label
                    SetupUserControls();
                });


    ...................

    private void SetupUserControls()
            {
                // instantiate the drop down list (m_ddlSubPrograms)
                m_ddlSubPrograms = new DropDownList();
                // create the items in the drop down list (m_ddlSubPrograms)
                BindSubProgramsList();

    ....................

    private void BindSubProgramsList()
            {
                SPWeb webCurrent = SPContext.Current.Web;

                // get the document library called "Contract Documents".  This will be the standard name and should not change
                SPDocumentLibrary listContractDocs = (SPDocumentLibrary)webCurrent.Lists["Contract Documents"];

    This last line is were it originally had an error, now it errors out in the RunWithElevatedPrivileges code block, meaning it does not even run the SetupUserControls() method.
  • Thursday, November 05, 2009 2:36 AMconverscient Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        using (SPWeb webCurrent = SPContext.Current.Site.OpenWeb())
             {
                 SPList listContractDocs = webCurrent.Lists["Contract Documents"];     
                 //carry on with your operations.
             }
    });

    however please be reminded that, when you use SPSecurity.RunWithElevatedPrivileges(delegate()), the audit trail will be modified to "System Account" instead of the actual user. If you need to have proper audit trail, you need to do more :)

    Never stop learning.
  • Thursday, November 05, 2009 12:35 PMparri2bd Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    How is this different then the code I already have?
  • Thursday, November 05, 2009 2:33 PMconverscient Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    have you tried it?
    Never stop learning.
  • Thursday, November 05, 2009 3:50 PMparri2bd Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Yes I tried it, there is no difference, so I ask again, how  was this code different conceptually?