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 have created a visual web part in Visual Studio 2010 which is to be deployed to the /bin directory of our SharePoint web applications. The idea being we can have a development, staging, UAT web application each using their own version of the web parts.

As it is to be deployed to the /bin directory I have edited the package xml to include CAS details

<CodeAccessSecurity>
<PolicyItem>
<PermissionSet class="NamedPermissionSet" version="1">
  <IPermission class="SecurityPermission" version="1" Flags="Execution"/>
  <IPermission class="AspNetHostingPermission" version="1" Level="Minimal" />
  <IPermission class="WebPartPermission" version="1" Connections="True" />
  <IPermission class="Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" version="1" ObjectModel="True" />
</PermissionSet>
<Assemblies>
  <Assembly Name="$SharePoint.Project.AssemblyName$" Version="$SharePoint.Project.AssemblyVersion$" PublicKeyBlob="$SharePoint.Project.AssemblyPublicKeyBlob$"/>
</Assemblies>

Note that I've also tried setting the Level to Medium on the AspNetHostingPermission line.

If I deploy this from Visual Studio, I can see the web part listed but I get an error when adding the part to the page. From various tinkering around I have had two messages:-

The file '/_CONTROLTEMPLATES/xxxxx/MyWebPart.ascx' does not exist

and

Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

This makes me think there is a problem with my CAS policy, but I'm at a lost to determine the error. Does anybody have any pointers for me?

share|improve this question
    
Is it a Sandbox solution ? You can't use Visual webpart from within them... –  Sébastien Levert Mar 15 '11 at 17:34
    
No, Sandboxed Solution is set to false. Probably also worth mentioning that Assembly Deployement is set to WebApplication and that I have confirmed that the dll is located in the correct web application bin directory (rather than the GAC) and tha the ascx file is in the control templates sub folder. –  grimorde Mar 16 '11 at 8:42
    
You do realise you have no IPermission node for FileIOPermission? That's what the error you get is complaining about. –  James Love Mar 28 '11 at 13:52
    
As I mentioned in reply to Elena's comment below, I did try adding the FileIOPermission in the CAS file with no effect. –  grimorde Mar 31 '11 at 13:08
add comment

2 Answers 2

Have you tried adding FileIOPermission to the webpart cs file (sample below) in CreateChildControls as there the user control is instantiated from its location on the File system, hence the requirement for FileIOPermission

 [SharePointPermission(SecurityAction.Demand, ObjectModel = true), FileIOPermission(SecurityAction.Demand, Unrestricted = true)]
protected override void CreateChildControls()
{....}
share|improve this answer
    
Actually, no I didn't try that. I did try adding the FileIOPermissions in the CAS file to no effect. Not sure if that's the same or not.... –  grimorde Mar 28 '11 at 13:40
add comment
up vote 0 down vote accepted

I've ended up downloading the SharePoint Power Tools for Visual Studio. That allowed me to create a Sandbox Visual Web Part and has resolved the issue.

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.