Usually when your SharePoint code runs in “WSS_Medium“, and there is a permission problem (or some unexplainable exception), its because your code doesn't have the right CAS permission to run. Try switching the trust mode to “Full” and see if your code works. If it does, then you can play with a custom CAS file to fix the problem (Yuck)... or you can use something like WSPBuilder that looks at the evidence in your assembly to determine what Demands and Assertions your code makes, and automatically creates and installs a custom CAS file. So the right thing to start with is to place Demand or Assert statements in your code as needed.
Once you find your code runs in “Full“ trust, then you get to try and figure out what EXACT permissions your code needs. This can take so long to determine, sometimes coming close is satisfactory. In my example, I figured out which permission, but not which attributes within the permission, leaving the permission's granularity at Unrestricted for the moment.
Example:
SharePoint (MOSS 2007) code contained:
pageLayouts = pubWeb.GetAvailablePageLayouts();
publishingWeb.GetPublishingPages().Add(pageName, pageLayout);
Code threw exceptions with Web.config entry (deployed with WSPBuilder):
<trust level="WSS_Medium" originUrl="" />
The Exceptions:
(NOTE: This was when missing SharePointPermission)
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.SPSite..ctor(String requestUrl, SPUserToken userToken)
at Microsoft.SharePoint.Publishing.CachedObjectFactory.get_SuperReaderSite()
at Microsoft.SharePoint.Publishing.CachedArea.GetEffectiveAvailablePageLayoutsString(CachedObjectFactory factory)
at Microsoft.SharePoint.Publishing.PublishingWeb.GetEffectiveAvailablePageLayoutsAsString()
at Microsoft.SharePoint.Publishing.PublishingWeb.GetAvailablePageLayouts() [...]
(NOTE: This was when missing ReflectionPermission)
Attempt to access the method failed. at System.Delegate.BindToMethodInfo(Object target, RuntimeMethodHandle method, RuntimeTypeHandle methodType, DelegateBindingFlags flags)
at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method, Boolean throwOnBindFailure)
at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method)
at Microsoft.Office.Server.Diagnostics.ULS.SendWatsonOnExceptionTag(ULSTagID tagID, ULSCat categoryID, String output, Boolean fRethrowException, TryBlock tryBlock, CatchBlock catchBlock, FinallyBlock finallyBlock)
at Microsoft.SharePoint.Publishing.PublishingPageCollection.Add(String name, PageLayout layout) [...]
(NOTE: This was when missing ConfigurationPermission)
Request for the permission of type 'System.Configuration.ConfigurationPermission, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' failed. at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
at System.Security.CodeAccessPermission.Demand()
at System.Configuration.BaseConfigurationRecord.CheckPermissionAllowed(String configKey, Boolean requirePermission, Boolean isTrustedWithoutAptca)
Code worked with Web.config entry:
<trust level="Full" originUrl="" />
I added Asserts to the code and deployed with WSPBuilder:
[SharePointPermission(SecurityAction.Assert, Unrestricted = true)]
[ReflectionPermission(SecurityAction.Assert, Unrestricted = true)]
[ConfigurationPermission(SecurityAction.Assert, Unrestricted = true)]
Code now works in WSS_Medium (note: WSPBuilder auto created WSS_Custom based on wss_medium):
<trustLevel name="WSS_Custom" policyFile="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\config\wss_custom_wss_mediumtrust_6d59f8b4-9688-4a2c-bf0c-ab4db786ed08.config" />
<trust level="WSS_Custom" originUrl="" />