views:

3389

answers:

4

I have a custom sharepoint web part that is throwing the following exception:

System.Security.SecurityException: Request for the permission of type 'Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' failed.
   at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode)
   at Waverly.SharePoint.Features.WebParts.SearchesListWebPart.Render(HtmlTextWriter writer)
The action that failed was:
Demand
The type of the first permission that failed was:
Microsoft.SharePoint.Security.SharePointPermission
The Zone of the assembly that failed was:
MyComputer

My theory is that Sharepoint does not have the trust level to run the web part but I'm having difficulty figuring out how to debug this problem. Anyone have any experience debugging something like this?

+2  A: 

There are several options you have. The first is to install your assembly into the GAC. It will then get full trust, but you have to install it in the GAC, strong name it, etc. The second option is to go into the web.config and add it to the trusted list. This article and this one will help you get down that path.

The third option is to drop your assembly in the C:\inetpub\wwwroot\wss\VirtualDirectories[port]_app_bin folder. This is set in the global SharePoint config to fully trust any assemblies in there. You might want to try that first just to see if it helps, and if so, use one of the other two methods (since technically that's an undocumented feature and could be changed)

EDIT: Ok, there's a couple of things you can try. Try bumping up the overall trust level as shown in this article. Basically look for:

<trust level="Full" originUrl="" />

If that works, then it indicates that you are relying on a DLL that isn't trusted. Note that you don't want to leave it with trust level Full, since that means any webpart has full trust.

Does that help?

Cory Foy
GAC-ing the dll was the only thing that seemed to work - it did work though.
sestocker
A: 

I tried to copy to the )app_bin folder as you recommended. That did not seem to work. The web parts were actually already in the safe controls section:

<SafeControls>
  ...
  <SafeControl Assembly="Client.SharePoint.Features, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5" Namespace="Waverly.SharePoint.Features.WebParts" TypeName="*" Safe="True" AllowRemoteDesigner="True" />
  <SafeControl Assembly="Client.SharePoint.Features, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5" Namespace="Waverly.SharePoint.Features" TypeName="*" Safe="True" AllowRemoteDesigner="True" />
</SafeControls>

To further explain what I am trying to do: I am creating a test environment from a production installation of sharepoint. I already used the admin tool to backup/restore. Everything seems to work fine except for two web parts that are erroring out.

sestocker
A: 

It is crazy. I can't execute the following line in a web part:

using (SPSite site = new SPSite(parentWebUrl))

However i can execute it not using web parts.

It throws me a similar exception althought the DLL is in the GAC plus are added to safecontrols.

Exception: SecurityException
Message: Request failed.
Action: System.Security.Permissions.SecurityAction.Demand
Some stack trace:
    at Microsoft.SharePoint.SPWeb.SPWebConstructor(SPSite site, String url, Boolean bExactWebUrl, SPRequest request)
    at Microsoft.SharePoint.SPWeb..ctor(SPSite site, String url, Boolean bExactWebUrl)
    at Microsoft.SharePoint.SPSite.OpenWeb()
    at ....

It also sais that

((System.Security.SecurityException)($exception)).Demanded
((System.Security.SecurityException)($exception)).DenySetInstance
((System.Security.SecurityException)($exception)).FailedAssemblyInfo
((System.Security.SecurityException)($exception)).FirstPermissionThatFailed
((System.Security.SecurityException)($exception)).GrantedSet

threw an exception.

What is the problem with that?

Solution

It turns out that many times errors like thses occur because various actions happen in webpart constructor. You almost cannot interact with SharePoint object model there.

Move code somwhere else and it could work. I moved my code to CreateChildControls and it worked.

Janis Veinbergs
A: 

Hi,

I am new at Sharepoint, I write to you because you are an experienced developer and I wanted to know if you could guide and help me to solve the following problem:

I have my main web application. Within it I have another application that uses a separate virtual directory.

The problem comes when I try to install any webpart, it doesn’t installed and gives me this error:

Some of the files could not be copied during the deployment of the solution. Details of the latest operation: MAD-SIS-VM-001: https: /myapplication/: formularios: Error: cannot find the web.config file section of this site. The controls specified assembly cannot be marked as .

I don’t want to be placed in "formularios". I also have another environment for testing and when I install it, I have no problem, but in this environment doesn’t works! I don’t want to install the webpart (or feature) on “formularios” ... I am avoiding something?

how can I fix it?

First of all thanks for your time,

PD: sorry is my English is not so good

Helen
Have you tried GAC-ing your assembly? That is the only thing that seemed to work for me.
sestocker