locked
Visual Web Part with Partial Trust? RRS feed

  • Question

  • In Visual Studio 2010, I am attempting to see if I can get a visual web part to work when specifying permissions using Code Access Security.  I added permissions for the control itself in the package editor (i.e.: SharePoint, AspNet hosting, etc.), but I can't figure out the permissions for Page.LoadControl.  Is it even possible?  Seems like it is calling out to unmanaged code or something.  This is the exception I get when it hits LoadControl().

    [SecurityException: Request failed.] System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck) +0 System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache) +146 System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache) +298 System.Activator.CreateInstance(Type type, Boolean nonPublic) +79 System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) +10363067 System.Web.Configuration.PagesSection.CreateControlTypeFilter() +11052518 System.Web.UI.TemplateParser.ProcessConfigSettings() +349 System.Web.UI.TemplateControlParser.ProcessConfigSettings() +23 System.Web.UI.UserControlParser.ProcessConfigSettings() +16 System.Web.UI.TemplateParser.PrepareParse() +243 System.Web.UI.TemplateParser.Parse() +10936103 System.Web.Compilation.BaseTemplateBuildProvider.get_CodeCompilerType() +174 System.Web.Compilation.BuildProvider.GetCompilerTypeFromBuildProvider(BuildProvider buildProvider) +177 System.Web.Compilation.BuildProvidersCompiler.ProcessBuildProviders() +291 System.Web.Compilation.BuildProvidersCompiler.PerformBuild() +38 System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath) +591 System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile) +476 System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile) +116 System.Web.UI.TemplateControl.LoadControl(VirtualPath virtualPath) +69
    ...

    I just get a request failed, with no details of what permission is needed.  I was never able to figure this out in MOSS either, but I seemed to get a different exception.  Does anyone know what IPermission elements are needed in my manifest to make this work?  

    Thanks,
    Corey

    Corey Roth blog: www.dotnetmafia.com twitter: twitter.com/coreyroth
    Friday, November 20, 2009 4:33 PM

Answers

  • LoadControl cannot be called from partial trust code. This is actually the reason why Visual Web Parts can't be used in Sandboxed Solutions. The LoadControl call itself does some kind of check (can't remember what) and fails when called from partial trust code.

    I've seen a few clever ways to work around this, but all involve not making that LoadControl call from Partial Trust.

    Regards,
    Paul
    SharePoint Product Manager. Posting is provided "AS IS" with no warranties, and confers no rights
    • Marked as answer by Darrin Bishop Monday, November 23, 2009 4:02 PM
    Monday, November 23, 2009 1:55 AM

All replies

  • Is this a farm or sandboxed solution?

    If sandboxed, LoadControl() cannot be used and therefore Visual Web Parts cannot be used - you have to code-up the UI in code as previously. If this just a regular farm solution, it's definitely possible with CAS but afraid I don't have a sample - more just wanted to remind about the sandboxed solution restriction!

     


    Chris O'Brien | SharePoint MVP | www.sharepointnutsandbolts.com
    Friday, November 20, 2009 7:13 PM
  • Sorry, I should have specified.  This is with a farm solution.  Oh yeah, I definitely knew that was out of the question with Sandboxed solutions.
    Corey Roth blog: www.dotnetmafia.com twitter: twitter.com/coreyroth
    Friday, November 20, 2009 8:41 PM
  • LoadControl cannot be called from partial trust code. This is actually the reason why Visual Web Parts can't be used in Sandboxed Solutions. The LoadControl call itself does some kind of check (can't remember what) and fails when called from partial trust code.

    I've seen a few clever ways to work around this, but all involve not making that LoadControl call from Partial Trust.

    Regards,
    Paul
    SharePoint Product Manager. Posting is provided "AS IS" with no warranties, and confers no rights
    • Marked as answer by Darrin Bishop Monday, November 23, 2009 4:02 PM
    Monday, November 23, 2009 1:55 AM
  • Thanks Paul for the answer.  That explains a lot (and why I could never get it to work in WSS3 either).  Of course, this makes me wonder why LoadControl requires such a high level of permissions. :)
    Corey Roth blog: www.dotnetmafia.com twitter: twitter.com/coreyroth
    Monday, November 23, 2009 5:24 PM
  • LoadControl loads a file off the hard drive, compiles it and runs it. Hence you need a high level of permissions to use it.
    http://msdn.microsoft.com/en-us/library/t9ecy7tf.aspx

    Cheers,
    Paul
    SharePoint Product Manager. Posting is provided "AS IS" with no warranties, and confers no rights
    Tuesday, November 24, 2009 1:26 AM
  • But what if LoadControl was reading from ContentDB ie a Document Library.  That would be safer.

    Brian Bedard, MCTS - pioneeringsharepoint.blogspot.com - Twitter:@tigertoy

    Tuesday, April 20, 2010 9:00 PM
  • I would agree.  Honestly, I think they need to make Page.LoadControl just work.  Remove the check for partial trust and give me the CAS settings to make it work.
    Corey Roth blog: www.dotnetmafia.com twitter: @coreyroth
    Tuesday, April 20, 2010 9:06 PM
  • But what if LoadControl was reading from ContentDB ie a Document Library.  That would be safer.

    Brian Bedard, MCTS - pioneeringsharepoint.blogspot.com - Twitter:@tigertoy


    Hi Brian,

    Yes, that would be a good idea. Except that LoadControl is part of ASP.NET which doesn't know about SharePoint Document Libraries. So we can't do that.

    Hi Corey, we can't just make it work. Because it loads files from the filesystem it can't run in partial trust. That's one of the fundamental rules of CAS. \

    There are other options though. Check this one out: http://blogs.msdn.com/pandrew/archive/2010/01/05/try-out-wouters-sharepoint-2010-dev-tools.aspx

    Regards,
    Paul


    SharePoint Product Manager. Posting is provided "AS IS" with no warranties, and confers no rights
    Wednesday, April 21, 2010 1:49 AM
  • Hi,

    About loading User Controls from document library, it's working (at least in my environnement on SharePoint 2007).
    I think it's because internally asp.net use System.Web.Hosting.HostingEnvironment.VirtualPathProvider. In my case, it is a "Microsoft.SharePoint.ApplicationRuntime.SPVirtualPathProvider" : It does know the system of document libraries.

    But you will have troubles if you use Inline code...
    And I think you will still have troubles with CAS....

    However, you will have a better flexibility for updating user controls.

    Regards,

    Sylvain

    Thursday, June 3, 2010 3:36 PM