{"id":941,"date":"2012-04-03T18:48:51","date_gmt":"2012-04-03T17:48:51","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=941"},"modified":"2012-04-03T18:48:51","modified_gmt":"2012-04-03T17:48:51","slug":"get-cas-ipermission-exception","status":"publish","type":"post","link":"https:\/\/www.stuartroberts.net\/index.php\/2012\/04\/03\/get-cas-ipermission-exception\/","title":{"rendered":"Get CAS IPermission for exception"},"content":{"rendered":"<p>If you&#8217;re working on a SharePoint solution that requires a custom code access security (CAS) policy, the following is an easy way of determining the permission(s) you need to add to the config.<\/p>\n<p>For this to work, you need to be able to debug the code, which should be a given, considering you&#8217;re creating a custom CAS for a solution you&#8217;re writing . \ud83d\ude42<\/p>\n<p>A basic CAS will look something like:<\/p>\n<pre lang=\"xml\">\r\n<CodeAccessSecurity>\r\n  <PolicyItem>\r\n    <PermissionSet class=\"NamedPermissionSet\" version=\"1\">\r\n      <IPermission class=\"SecurityPermission\" version=\"1\" Flags=\"Execution\" \/>\r\n      <IPermission class=\"AspNetHostingPermission\" version=\"1\" Level=\"Minimal\" \/>\r\n      <IPermission class=\"Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c\" version=\"1\" ObjectModel=\"True\" \/>\r\n    <\/PermissionSet>\r\n    <Assemblies>\r\n      <Assembly Name=\"$SharePoint.Project.AssemblyName$\" Version=\"$SharePoint.Project.AssemblyVersion$\" PublicKeyBlob=\"$SharePoint.Project.AssemblyPublicKeyBlob$\"\/>\r\n    <\/Assemblies>\r\n  <\/PolicyItem>\r\n<\/CodeAccessSecurity>\r\n<\/pre>\n<p><!--more--><br \/>\nAll the above does is effectively grant your custom assembly the ability to use the SharePoint object model.<\/p>\n<p>Now, say you added code that performs some reflection, you would receive a security exception but may be left unclear exactly what permission policy is required for the CAS config.  By debugging the code and setting a breakpoint on the catch block for the error:<\/p>\n<pre lang=\"csharp\">\r\npublic void GetListItem(SPList list)\r\ntry {\r\n  SPContext context = SPContext.GetContext(Context, 0, list.ID, list.ParentWeb);\r\n  SPListItem listItem = list.AddItem();\r\n  var contextItem = (typeof(SPContext)).GetField(\"m_item\", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);\r\n  contextItem.SetValue(context, listItem);\r\n  \r\n  \/\/ Do other stuff\r\n}\r\ncatch (SecurityException ex) {\r\n  \/\/ do something with the exception\r\n}\r\n<\/pre>\n<p>You will be able to get this information by typing the following into the Immediate window of Visual Studio (ctrl+i):<\/p>\n<pre lang=\"csharp\">\r\nex.m_demanded\r\n<\/pre>\n<p><em>m_demanded<\/em> is a private field of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.security.securityexception(v=vs.90).aspx\" title=\"SecurityException\" target=\"_blank\">SecurityException<\/a> class and can be drilled into from the Immediate window, QuickWatch, etc. while debugging.<\/p>\n<p>The output can then be copied and pasted into your CAS config:<\/p>\n<pre lang=\"xml\">\r\n<IPermission class=\"ReflectionPermission\" version=\"1\" Unrestricted=\"true\"\/>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re working on a SharePoint solution that requires a custom code access security (CAS) policy, the following is an easy way of determining the permission(s) you need to add to the config. For this to work, you need to &hellip; <a href=\"https:\/\/www.stuartroberts.net\/index.php\/2012\/04\/03\/get-cas-ipermission-exception\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[47,3],"tags":[48,81],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/plx2I-fb","_links":{"self":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/941"}],"collection":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=941"}],"version-history":[{"count":16,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/941\/revisions"}],"predecessor-version":[{"id":968,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/941\/revisions\/968"}],"wp:attachment":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=941"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}