{"id":374,"date":"2010-05-23T11:46:40","date_gmt":"2010-05-23T09:46:40","guid":{"rendered":"http:\/\/www.itidea.nl\/?p=374"},"modified":"2015-09-08T20:32:04","modified_gmt":"2015-09-08T18:32:04","slug":"full-trust-proxies-in-sharepoint-2010","status":"publish","type":"post","link":"https:\/\/www.itidea.nl\/index.php\/full-trust-proxies-in-sharepoint-2010\/","title":{"rendered":"Full trust proxies in SharePoint 2010"},"content":{"rendered":"<p>Because a sandboxed solution is completely isolated to its site collection only a subset of the Microsoft.SharePoint object model can be used. Only objects that operate within the current site collection are available to you, when you are building a sandboxed solution.\u00a0<\/p>\n<p>Sandboxed solutions run within a special process, the Sandbox Worker Process (SPUCWorkerProcess.exe). The sandbox worker process makes sure the artifacts from the solution can be used as though they were deployed to the server itself and it will enforce the limits of a Code Access Security (CAS) policy on the contents of the solution. The following permissions will be granted to the solution by the CAS policy:\u00a0<\/p>\n<ul>\n<li>SharePointPermission.ObjectModel<\/li>\n<li>SecurityPermission.Execution<\/li>\n<li>AspNetHostingPermission.Level = Minimal<\/li>\n<\/ul>\n<p>Ofcourse you need some more functionality from time to time. Here the full trust proxy solution comes in handy.\u00a0<\/p>\n<p>A Proxy Class has to be deployed at Farm level and can be used by everybody within the farm.<br \/>\nThe full trust proxy solution exists of two classes:<br \/>\nThe first class inherits from SPProxyOperation, the second class inherits from SPProxyOperationArgs.<br \/>\nBoth of these are located in the Micorosft.SharePoint.UserCode namespace.<br \/>\nThe class which inherits from SPProxyOperation implements the actual operation the full trust proxy solution has to perform.<br \/>\nThe class which inherits from SPProxyOperationArgs defines the arguments which will be passed to the operation.\u00a0<\/p>\n<p>Let&#8217;s make an very simple application to write a message to an eventlog. First we&#8217;ll need a full trust proxy solution to write to the event log, second a sandboxed solution (webpart) which will tell what message the full trust proxy actually has to write.\u00a0<\/p>\n<h2>Full trust proxy solution<\/h2>\n<p>First define a class which inherits from\u00a0the SPProxyOperationArgs:\u00a0<\/p>\n<p>[assembly: AllowPartiallyTrustedCallers]<br \/>\nnamespace FullTrustProxyProject1<br \/>\n{<br \/>\n\u00a0\u00a0\u00a0 [Serializable]<br \/>\n\u00a0\u00a0\u00a0 public class EventLogArgs : SPProxyOperationArgs<br \/>\n\u00a0\u00a0\u00a0 {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 public string LogMessage { get; set; }<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 public string LogApplication { get; set; }<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 public string LogLevel { get; set; }\u00a0<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 public EventLogArgs(string logMessage, string logApplication, string logLevel)<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 this.LogMessage = logMessage;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 this.LogApplication = logApplication;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 this.LogLevel = logLevel;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<br \/>\n\u00a0\u00a0\u00a0 }<br \/>\n}\u00a0<\/p>\n<p>As you can see this is just a class to define arguments which will be passed to the proxy operation. The class has to be Serializable and AllowPartiallyTrustedCallers has to be set, because the class is going to be used between Trust Domains.\u00a0<\/p>\n<p>Next the operation of the full trust proxy, the receiver of the SPProxyOperationArgs:\u00a0<\/p>\n<p>namespace FullTrustProxyProject1<br \/>\n{<br \/>\n\u00a0\u00a0\u00a0 public class EventLogItemCreateOperation : SPProxyOperation<br \/>\n\u00a0\u00a0\u00a0 {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 public override object Execute(SPProxyOperationArgs args)<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (args != null)<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 try<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 EventLogArgs arguments = args as EventLogArgs;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 string result = SPLogger.AddEventLogEntry(arguments.LogApplication, arguments.LogLevel, arguments.LogMessage);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return result;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 catch (Exception ex)<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\u00a0<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return ex.ToString();<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 else<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return null;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<br \/>\n\u00a0\u00a0\u00a0 }<br \/>\n}\u00a0<\/p>\n<p>In this class the Execute method\u00a0is overridden and a check is performed\u00a0for the incoming args. The args are then parsed to EventLogArgs and you can access the public properties of the EventLogArgs. The Execute method returns an object.<br \/>\nThat&#8217;s all!\u00a0<\/p>\n<h2>Register the full trust proxy<\/h2>\n<p><span style=\"color: #ff0000;\">\u00a0<\/span>After implementation of these two classes the proxy has to be registered at the User Code Service in SharePoint. To do this you can use PowerShell or the Object Model.<br \/>\nI just made a simple Windows Forms application to register, unregister and list the registered proxies:<br \/>\nRegister:<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SPUserCodeService service = SPUserCodeService.Local;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (service != null)<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SPProxyOperationType getEventLogItemCreationOperation = new SPProxyOperationType(&#8220;FullTrustProxyProject1, version=1.0.0.0, Culture=neutral, PublicKeyToken=db3652199d4628cd&#8221;, &#8220;FullTrustProxyProject1.EventLogItemCreateOperation&#8221;);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 service.ProxyOperationTypes.Add(getEventLogItemCreationOperation);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 service.Update();<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 label1.Text = &#8220;Updated successfully!&#8221;;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 else<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 label1.Text = &#8220;Update failed!&#8221;;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\u00a0<\/p>\n<p>Unregister:<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SPUserCodeService service = SPUserCodeService.Local;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (service != null)<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SPProxyOperationType getEventLogItemCreationOperation = new SPProxyOperationType(&#8220;FullTrustProxyProject1, version=1.0.0.0, Culture=neutral, PublicKeyToken=db3652199d4628cd&#8221;, &#8220;FullTrustProxyProject1.EventLogItemCreateOperation&#8221;);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 service.ProxyOperationTypes.Remove(getEventLogItemCreationOperation);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 service.Update();<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 label3.Text = &#8220;Removed succesfully!&#8221;;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 else<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 label3.Text = &#8220;Remove failed!&#8221;;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\u00a0<\/p>\n<p>And list all registered proxies:<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 SPUserCodeService service = SPUserCodeService.Local;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (service != null)<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 int count = service.ProxyOperationTypes.Count;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 label2.Text = &#8220;Proxy count: &#8221; + count.ToString() + Environment.NewLine ;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 foreach (SPProxyOperationType item in service.ProxyOperationTypes)<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 label2.Text += &#8220;AssemblyName: &#8221; + item.AssemblyName + Environment.NewLine + &#8220;TypeName: &#8221; + item.TypeName + Environment.NewLine;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/05\/RegisterFullTrustProxyWindowsApp1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-413\" title=\"RegisterFullTrustProxyWindowsApp\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/05\/RegisterFullTrustProxyWindowsApp1.png\" alt=\"\" width=\"545\" height=\"203\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/05\/RegisterFullTrustProxyWindowsApp1.png 545w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/05\/RegisterFullTrustProxyWindowsApp1-300x111.png 300w\" sizes=\"auto, (max-width: 545px) 100vw, 545px\" \/><\/a><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/05\/RegisterFullTrustProxyWindowsApp.png\"><\/a>\u00a0<\/p>\n<h2>Create the sandboxed solution<\/h2>\n<p>After deploying and registering the full trust proxy it can be used in a sandboxed solution:<br \/>\nJust create a webpart as you normally do, make sure this is a sandboxed solution. Create some controls, e.g.:<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/05\/SandBoxedSolutionForFullTrustProxy.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-415\" title=\"SandBoxedSolutionForFullTrustProxy\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/05\/SandBoxedSolutionForFullTrustProxy.png\" alt=\"\" width=\"610\" height=\"35\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/05\/SandBoxedSolutionForFullTrustProxy.png 610w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2010\/05\/SandBoxedSolutionForFullTrustProxy-300x17.png 300w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><br \/>\nAt the button click event all you have to do is:<br \/>\nEventLogArgs args = new EventLogArgs(box.Text, app.Text, logLevels.SelectedValue);<br \/>\nresults.Text = SPUtility.ExecuteRegisteredProxyOperation(&#8220;FullTrustProxyProject1, version=1.0.0.0, Culture=neutral, PublicKeyToken=db3652199d4628cd&#8221;, &#8220;FullTrustProxyProject1.EventLogItemCreateOperation&#8221;, args).ToString();\u00a0<\/p>\n<h2>Keep in mind!<\/h2>\n<p><span style=\"color: #000000;\">Keep in mind\u00a0that the full trust proxy runs under another process than the sandboxed solution. So if you want to debug the full trust proxy attach the SPUCWorkerProcessProxy.exe, do you want to debug the sandboxed solution, attach the SPUCWorkerProcess.exe.<br \/>\nWhen you redeploy your\u00a0full trust proxy solution you have to restart the User Code Service because that&#8217;s where the full trust proxy is registered, don&#8217;t forget! I did&#8230;<br \/>\nTo restart go to the Central Administration, Application Management, Manage services on server and find the Microsoft SharePoint Foundation Sandboxed Code Service. Stop and start this service again. Another option is to use &#8220;net stop SPUserCodeV4&#8221;, and start it again by using &#8220;net start SPUserCodeV4&#8221;.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Because a sandboxed solution is completely isolated to its site collection only a subset of the Microsoft.SharePoint object model can be used. Only objects that operate within the current site collection are available to you, when you are building a &#8230; <a class=\"more-link\" href=\"https:\/\/www.itidea.nl\/index.php\/full-trust-proxies-in-sharepoint-2010\/\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[28,25,42,6],"class_list":["post-374","post","type-post","status-publish","format-standard","hentry","category-sharepoint-2010","tag-full-trust-proxy","tag-sandboxed-solutions","tag-sharepoint-2010","tag-visual-studio"],"_links":{"self":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/374","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/comments?post=374"}],"version-history":[{"count":25,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/374\/revisions"}],"predecessor-version":[{"id":421,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/374\/revisions\/421"}],"wp:attachment":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/media?parent=374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/categories?post=374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/tags?post=374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}