SharePoint Products TechCenter >
SharePoint Products and Technologies Forums
>
SharePoint - Development and Programming
>
SecurityException, not understood
SecurityException, not understood
- 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
- Can you post the code? What permissions are set on the contract documents library?
My SharePoint Blog - http://www.davehunter.co.uk/blog - 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. - 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- Edited byMike Walsh MVPMVP, ModeratorWednesday, November 04, 2009 7:05 AMBlock quote deleted. Unnecessary.
- 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.
- 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. - 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. - Also how are you running this code? On the sharepoint server?
My SharePoint Blog - http://www.davehunter.co.uk/blog - 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. - 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 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- 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. - 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. - How is this different then the code I already have?
- have you tried it?
Never stop learning. - Yes I tried it, there is no difference, so I ask again, how was this code different conceptually?

