<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Chris Keyser's Blog</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/chriskeyser/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/chriskeyser/feed.xml</link>
    <description>Random thoughts on SharePoint and .NET topics.</description>
    <lastBuildDate>Wed, 17 Apr 2019 03:18:56 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Managing Document Templates with Content Types and Content Type Hub</title>
      <link>https://docs.microsoft.com/archive/blogs/chriskeyser/managing-document-templates-with-content-types-and-content-type-hub</link>
      <pubDate>Tue, 13 Dec 2011 13:37:00 GMT</pubDate>
      <dc:creator><![CDATA[Chris Keyser]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/chriskeyser/2011/12/13/managing-document-templates-with-content-types-and-content-type-hub/</guid>
      <description><![CDATA[Recently I wanted to manage a central set of document templates associated with content types across...]]></description>
      <content:encoded><![CDATA[<p>Recently I wanted to manage a central set of document templates associated with content types across a number of site collections.&nbsp; We also wanted to look at using content type syndication with content types that have a document template applied.&nbsp; Document templates need to either be stored in the site collection where the content type is defined, or in&nbsp;&lt;SharePoint root&gt;/Templates/Layouts (_layouts).&nbsp; By default when you upload a document template when you upload a template it gets stored to a hidden folder, _cts/&lt;content type name&gt;.&nbsp; You can also deploy a template to this location as part of feature that deploys content types.&nbsp; I did a quick experiment to figure out how these two models worked with content type deployment.</p>
<h2>Centrally Managed Document Templates</h2>
<p>In order to centrally manage a single instance of a document template, the template should be deployed to the _layouts folder.&nbsp; By mapping a folder for the feature to the layouts folder for your document templates, the template will be deployed to the file system when the solution is deployed, and&nbsp;available to any site collection within the farm.&nbsp;</p>
<ol>
<li>Setup the content type hub (see <a href="https://office.microsoft.com/en-us/sharepoint-server-help/manage-content-type-publishing-HA101631502.aspx">https://office.microsoft.com/en-us/sharepoint-server-help/manage-content-type-publishing-HA101631502.aspx)</a>.</li>
<li>Create a feature that contains a content type with a document template, and the document template itself.&nbsp; This <a href="https://blogs.msdn.com/b/vssharepointtoolsblog/archive/2010/05/26/deploying-documents-with-content-types-and-associating-content-types-with-libraries-part-i.aspx">SharePoint Developer Team Blog</a>&nbsp;describes how to create a content type programmatically, associate a document template, and place the document template under a _layouts subfolder.&nbsp;&nbsp;</li>
<li>&nbsp;Deploy the feature to the farm.</li>
<li>Activate the feature on the content type publishing hub site collection.&nbsp; Make sure the content type is setup to publish (see site settings-&gt;content type gallery, select the content type-&gt;Manage publishing for this content type, and make sure it is set to publish.</li>
</ol>
<p>A couple of caveats.&nbsp;</p>
<ul>
<li>There is a hidden feature for managed metadata featuures on a site collection.&nbsp; Some site definitions activate, others do not (for example, a blank site will not activate the feature).&nbsp; See <a href="https://pacsharepoint.com/2010/06/content-type-publishing-option-missing.html">Content type publishing option missing from Site Collection Administration</a> for more information on the feature to activate.&nbsp; If you don't see a entry in the manage site collection settings for content type publishing then you probably need to activate this feature.</li>
<li>The solution must be a farm solution since files are deployed to the layouts folder.</li>
<li>The content type publishing and subscription process is managed by two timer jobs.&nbsp; Both must run before a content type created on the publishing hub site is present on the subscribing sites. (content type hub, and content type subscriber (one for each web app)</li>
</ul>
<h2>Replicate Document Templates&nbsp;across site collections</h2>
<p>If the template is instead deployed to a location within the site collection (for example the default location of _cts/&lt;content type&gt;),&nbsp; it will be placed within the content database, and the document template will be replicated within each site collection.&nbsp; For a blog on this approach, see the blog on managing document templates and content types with Office 365, <a href="https://blogs.msdn.com/b/chaks/archive/2011/05/19/deploying-a-document-template-file-in-content-type-in-a-office365-sandboxed-solution.aspx">Deploying a Document Template file in Content Type in a (Office365) Sandboxed Solution</a>&nbsp;by <span class="user-name"><a class="internal-link view-user-profile" href="https://blogs.msdn.com/294075/ProfileUrlRedirect.ashx"><span></span>Chakkaradeep Chandran</a>.&nbsp; In the case of a no-code or Office 365 (or a sandboxed solution with an on-premise installation) you will need to deploy document templates to the site collection since you can't write to the file system.&nbsp; </span></p>
<p><span class="user-name">If the document template is deployed to the default "_cts/&lt;content type&gt;/" location then the document template will be replicated by the content type publishing logic with the content type. Since the document template is replicated, a new document can be created based upon the content type in the subscribing site.&nbsp; If an updated version of the template is uploaded, then the content type hub will re-publish the content type and document template to all subscribing sites. </span></p>
<p><span class="user-name">Another option is to specify an existing document (which must be a location in the site collection).&nbsp; If an existing document location is selected, then the content type will be replicated refering to the location in the content type hub.&nbsp; However creating a new document based upon the content type in this case will fail since the publishing logic will not replicate the template file, and the content type will contain a reference to a document template that doesn't exist in the subscribing site.(unless some other mechanism also replicated this library).&nbsp; </span></p>
<h2><span class="user-name">Conclusion</span></h2>
<p><span class="user-name">Generally I'd recommend avoiding using a library to contain the templates if you are using content type publishing.&nbsp;&nbsp;If you need a central repository and templates don't change often, then use layouts in which case a single copy of the template will be used by all site collections in the farm.&nbsp; If you can't deploy a farm feature, or you want to manage the templates through the UI rather than with a developed applications, then deploy the template to the default upload location (_cts/&lt;content type&gt;) rather than using a library to contain the templates, and the document template will be replicated by the content type publishing infrastructure.&nbsp; While I did not test the case of a cross-farm service, you'd clearly need to deploy the feature to all farms when using the layouts approach.&nbsp; I'd hypothesize that the content hub should still replicate templates in the case where they are stored to _cts location.&nbsp;</span></p>
<p><span class="user-name">If you want to deploy a content type across a number of site collections without using content publishing (recommended approach is using a site scoped feature), then the _layouts approach is the only solution to make it easy for maintaining a single document template used by all of the site collections&nbsp; using the content type.</span></p>
<p><span class="user-name">Thanks to Andrew Connell and Rob Bogue for input.</span></p>
<p></p>]]></content:encoded>
    </item>
    <item>
      <title>BCS - developing a .NET assembly connector presentation and demo</title>
      <link>https://docs.microsoft.com/archive/blogs/chriskeyser/bcs-developing-a-net-assembly-connector-presentation-and-demo</link>
      <pubDate>Wed, 19 Oct 2011 12:52:35 GMT</pubDate>
      <dc:creator><![CDATA[Chris Keyser]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/chriskeyser/2011/10/19/bcs-developing-a-net-assembly-connector-presentation-and-demo/</guid>
      <description><![CDATA[I'm attaching a presentation and code for developing an assembly connector for SharePoint 2010...]]></description>
      <content:encoded><![CDATA[<p>I'm attaching a presentation and code for developing an assembly connector for SharePoint 2010 BCS.&nbsp; I plan to do a series of blogs about different elements of the solution, but wanted to get this posted for those that attended my talk.&nbsp; It illustrates a connector that supports streaming, configuration, and secure store.&nbsp; Unfortunately the code won't actually work without setting up some additional sharepoint sites and a web service.&nbsp; I've included the code for the web service, but the SharePoint site i just setup manually.&nbsp; The following are included in the attached zip:</p>
<p>Code Camp - BCS.ppt - The presentation</p>
<p>AssemblyConnectorDemo.completed.zip - the connector implementation</p>
<p>Customer Service and Sales Site.zip - a wsp containing the SharePoint site that is being accessed through the connector, and a zip of the solution implementing the service being accessed by the connector in the demo.</p>
<p>&nbsp;</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/10/22/77/55/Code%20Camp%20Presentation%20and%20Demo.zip" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-22-77-55/Code-Camp-Presentation-and-Demo.zip">Code Camp Presentation and Demo.zip</a></p>]]></content:encoded>
    </item>
    <item>
      <title>SharePoint 2010 claims and Home Realm Discover - passing whr on the url to SharePoint</title>
      <link>https://docs.microsoft.com/archive/blogs/chriskeyser/sharepoint-2010-claims-and-home-realm-discover-passing-whr-on-the-url-to-sharepoint</link>
      <pubDate>Sun, 02 Oct 2011 05:21:00 GMT</pubDate>
      <dc:creator><![CDATA[Chris Keyser]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/chriskeyser/2011/10/02/sharepoint-2010-claims-and-home-realm-discover-passing-whr-on-the-url-to-sharepoint/</guid>
      <description><![CDATA[Home Realm Discovery is a process to select&nbsp;the trusted identity provider in a federated claims...]]></description>
      <content:encoded><![CDATA[<p><span style="font-family: verdana,geneva; font-size: small;">Home Realm Discovery is a process to select&nbsp;the trusted identity provider in a federated claims scenario where there is more than one provider that can authenticate users.&nbsp; The default experience is to have the user select which claims provider to use to authenticate.&nbsp; Often organizations would like to avoid making their users perform this decision.&nbsp; There are a few ways to automate this selection, one of which is through providing a hint on the url to the federation server (in the form of a whr parameter) that is then used by the federation server to pre-select the identity provider.&nbsp; The article <a href="https://social.technet.microsoft.com/wiki/contents/articles/windows-identity-foundation-wif-how-to-utilize-the-ws-federation-whr-parameter-to-bypass-home-realm-discovery-hrd.aspx" target="_blank">https://social.technet.microsoft.com/wiki/contents/articles/windows-identity-foundation-wif-how-to-utilize-the-ws-federation-whr-parameter-to-bypass-home-realm-discovery-hrd.aspx</a>&nbsp;describes the two different approaches for a relying party (in this case SharePoint) to implement the use of whr.</span></p>
<p><span style="font-family: verdana,geneva; font-size: small;">Steve Peschka has a bunch of great blogs on claims and SharePoint, including </span><a title="Using the WHR Parameter with SharePoint 2010 and SAML Auth" href="https://blogs.technet.com/b/speschka/archive/2011/09/14/using-the-whr-parameter-with-sharepoint-2010-and-saml-auth.aspx">Using the WHR Parameter with SharePoint 2010 and SAML Auth</a>.&nbsp;<span style="font-family: verdana,geneva; font-size: small;"> In this blog Steve describes how to use a http module &nbsp;in a SharePoint environment&nbsp; to insert a whr parameter based upon some decision logic.&nbsp; This insertion&nbsp;onto the url&nbsp; occurs when SharePoint recognizes the user is not authenticated, and goes through a series of redirects to a trusted claims provider to perform authentication.&nbsp; This solves the second, more sophisticated approach for using whr described in previously mentioned article.&nbsp;</span><span style="color: #1f497d; font-family: 'Calibri','sans-serif'; font-size: 11pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;">&nbsp; <span style="color: #000000; font-family: verdana,geneva; font-size: small;">This approach also requires that you write custom logic and maintain that logic for selecting the authentication provider.</span></span></p>
<p><span style="color: #000000; font-family: verdana,geneva; font-size: small; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;">Ideally SharePoint would just work with the first approach as well, which supports directly appending the whr parameter onto the SharePoint url you are attempting to access.&nbsp; Building off of Steve's blog, an example url accessing SharePoint may look like&nbsp;&nbsp;<a href="https://www.vbtoys.com/?whr=https://geneva.vbtoys.com/adfs/services/trust">https://www.vbtoys.com/?whr=https://geneva.vbtoys.com/adfs/services/trust.</a>&nbsp;&nbsp; The advantage of this approach is that it would not require&nbsp;the implementation of&nbsp;custom logic.&nbsp;&nbsp;Unfortunately SharePoint doesn't carry over the whr parameter from an incoming url over&nbsp;to the&nbsp;redirect url to the federation server.&nbsp; So it won't work.&nbsp;&nbsp;This approach is simpler but less powerful than the approach described by Steve.&nbsp; Using Steve's approach I'm not tied to a particular url.&nbsp; Rather any url I attempt to access in SharePoint that is being redirected to the federation server will have the whr inserted.&nbsp; But often customers don't want to write custom code, and the benefit of the simpler approach is that I can just hand out different urls for users to bookmark that will do the pre-selection for them.&nbsp; For example, if I had an extranet site for partners, I could hand out urls to the partners with the appropriate&nbsp;identity provider pre-selected for that partner.</span></p>
<p><span style="color: #000000; font-family: verdana,geneva; font-size: small; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;">There is&nbsp;simple implementation to carry the url over from the original SharePoint url.&nbsp; Extending from Steve's blog, instead of processing the begin request, process the end request event.&nbsp; In the end request event, check for the whr parameter on the request, and if it is present on the request, but not present on the redirect url, then add it.&nbsp; It's pretty much that simple.&nbsp; Below is example code:</span></p>
<p></p>
<p>public void EndRequest(object sender, EventArgs e)<br />{<br />&nbsp;&nbsp;&nbsp; try<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //get the app and context sources<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HttpApplication application = (HttpApplication)sender;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HttpContext context = application.Context;</p>
<p></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (context.Response.IsRequestBeingRedirected == true &amp;&amp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; context.Request.QueryString.AllKeys.Contains("whr"))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string whr = "whr=" + context.Request.QueryString["whr"];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string redir = context.Response.RedirectLocation;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!redir.Contains(whr))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; redir = redir + (redir.Contains("?") ? "&amp;" + whr : "?" + whr);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; context.Response.RedirectLocation = redir;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; catch(Exception ex)<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Debug.WriteLine(ex.Message);<br />&nbsp;&nbsp;&nbsp; }<br />}</p>
<p></p>
<p><span style="color: #000000; font-family: verdana,geneva; font-size: small; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;">I've attached a modified version of the project from Steve's original blog post that implements this approach.&nbsp; This is only a sample.&nbsp; I'd definitely take time to make it a bit more robust for a production implementation.&nbsp; Also I've put in some pre and post build steps to automatically register and unregister the assembly from the gac - if things aren't working, then the exe referenced may be in a different location.&nbsp; Finally make sure you follow the instructions in the project.&nbsp; Steve's original instructions still hold.</span></p>
<p><span style="color: #000000; font-family: verdana,geneva; font-size: small; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;">Hope this helps.</span></p>
<p></p>
<p><span style="color: #000000; font-family: verdana,geneva; font-size: small; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"></span>&nbsp;</p>
<p><span style="color: #000000; font-family: verdana,geneva; font-size: small; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;">&nbsp;</span></p>
<p></p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/10/21/89/31/AdfsHrd.zip" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-21-89-31/AdfsHrd.zip">AdfsHrd.zip</a></p>]]></content:encoded>
    </item>
    <item>
      <title>SharePoint Guidance Library Hands On Labs Released to Codeplex</title>
      <link>https://docs.microsoft.com/archive/blogs/chriskeyser/sharepoint-guidance-library-hands-on-labs-released-to-codeplex</link>
      <pubDate>Fri, 04 Feb 2011 10:57:59 GMT</pubDate>
      <dc:creator><![CDATA[Chris Keyser]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/chriskeyser/2011/02/04/sharepoint-guidance-library-hands-on-labs-released-to-codeplex/</guid>
      <description><![CDATA[The SharePoint Guidance library introduces several components that can be challenging to ramp up on...]]></description>
      <content:encoded><![CDATA[<p class="MsoNormal" style="margin: 0in 0in 1.55pt; mso-margin-top-alt: auto;"><b><span style="font-family: 'Segoe UI','sans-serif'; color: #30332d;"><span style="font-size: medium;">The SharePoint Guidance library introduces several components that can be challenging to ramp up on if you aren't familar with some SharePoint and design concepts like dependency injection.&nbsp; A great set of HOL's have been released as a step by step walk through using different components of the SharePoint Guidance Library.</span></span></b></p>
<p class="MsoNormal" style="margin: 0in 0in 1.55pt; mso-margin-top-alt: auto;"><b><span style="font-family: 'Segoe UI','sans-serif'; color: #30332d; font-size: 19pt;"></span></b></p>
<p class="MsoNormal" style="margin: 0in 0in 1.55pt; mso-margin-top-alt: auto;"><b><span style="font-family: 'Segoe UI','sans-serif'; color: #30332d; font-size: 19pt;"><a href="https://spg.codeplex.com/releases/view/60343"><span style="color: #0000ff;">SharePoint Guidance 2010 Hands On Lab</span></a> </span></b><span style="color: #30332d;"><span style="font-size: small;"><span style="font-family: Calibri;">has just been released to CodePlex. It consists of six labs: one for logging, one for service location, and four for application setting manager. Each lab takes about 20 minutes to walk through. Interested people please try one of those labs to give me feedback. Each lab consists of a PDF document. You can go through the steps in the doc to create solution and then build/deploy the solution and run the lab. For those of you who wants to save time, we included the final solution so you can just build/deploy the solution and run the lab. <o:p></o:p></span></span></span></p>
<p><span style="font-family: 'Calibri','sans-serif'; color: #30332d; font-size: 11pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;">For the next a few weeks, we are going to add two more labs: Logging for sandbox and installation-uninstallation of Service Locator in a Farm environment. That should cover all the basic scenarios for the SPG2010 reusables</span></p>]]></content:encoded>
    </item>
    <item>
      <title>Handling SharePointServiceLocator failures due to caching</title>
      <link>https://docs.microsoft.com/archive/blogs/chriskeyser/handling-sharepointservicelocator-failures-due-to-caching</link>
      <pubDate>Thu, 20 Jan 2011 14:04:00 GMT</pubDate>
      <dc:creator><![CDATA[Chris Keyser]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/chriskeyser/2011/01/20/handling-sharepointservicelocator-failures-due-to-caching/</guid>
      <description><![CDATA[The SharePointServiceLocator in the patterns and practices SharePoint library implements a...]]></description>
      <content:encoded><![CDATA[<p>The SharePointServiceLocator in the patterns and practices SharePoint library implements a dependency injection pattern for decoupling service providers from service consumers.&nbsp; It achieves this by mapping an interface to an implementing class.&nbsp; The consumer of the implementation only has a dependency on the interface, and the service locator has the responsiblity to construct an instance of a &nbsp;concrete type that implments the interface.&nbsp; This helps with unit testing (by making it easy to substitute a test implementation), and promotes loose coupling of code since a service consumer doesn't have to have any knowledge of the provider of that service.&nbsp; In more sophisticated cases the mapping can be looked up by key, and the serivce locator can provide different implementing classes depending upon the context.&nbsp; An additional feature is that the SharePointSerivceLocator will allow type mappings to be defined at a Farm or Site collection scope.&nbsp; A site collection scope will override a farm mapping.&nbsp; As a result different behavior for a specific site collection can be provided for situations where the business requires it for the service.&nbsp; For more inforation see <a href="https://msdn.microsoft.com/en-us/library/ff798484.aspx" title="The SharePoint Service Locator"><strong><span style="color: #e66a38;">The SharePoint Service Locator</span></strong></a>.</p>
<p>Since the service locator is called frequently to load types, the information mapping an interface to a type is cached in memory by the locator for both the farm and site collection level mappings.&nbsp; By default the site collection mappings are purged every minute (this is configurable) and the type mappings for the farm are not purged from the cache.&nbsp; If new type mappings are defined, then IIS must be reset to flush this cache.&nbsp; However the service locator also has a method, Reset, that will cause the cached values to be reset.&nbsp; We wanted to get a feature into the service locator to purge the cache and re-lookup the cached values when a type mapping can't be located, but unfortunately we ran out of time to get this feature implemented.&nbsp; The good news is that its relatively straightforward to create a wrapper class that will do this for you.</p>
<p>&nbsp;<code class="cplusplus">public class ServiceLocatorWrapper<br />{<br />&nbsp;&nbsp;&nbsp; public static TService GetInstance&lt;TService&gt;()<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TService instance;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; instance = SharePointServiceLocator.GetCurrent().GetInstance&lt;TService&gt;();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch (ActivationException e)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LogFailure&lt;TService&gt;(e);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SharePointServiceLocator.Reset();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; instance = SharePointServiceLocator.GetCurrent().GetInstance&lt;TService&gt;();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return instance;<br />&nbsp;&nbsp;&nbsp; }<br /><br /></code><code class="cplusplus"><br />&nbsp;&nbsp;&nbsp; private static void LogFailure&lt;TService&gt;(Exception e)<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ILogger logger = SharePointServiceLocator.GetCurrent().GetInstance&lt;ILogger&gt;();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; logger.TraceToDeveloper(e, string.Format("Failed to load type {0}, flushing service locator and retrying", typeof(TService).Name));<br />&nbsp;&nbsp;&nbsp; }<br />}</code></p>
<p><code class="cplusplus">&nbsp;The wrapper class implements a generic method for getting a type instance.&nbsp; The generic method just passes this through to service locator to create the instance.&nbsp;&nbsp;The wrapper also catches an ActivationException when thrown, which is thrown by the service locator if it can't find a type mapping.&nbsp;&nbsp;When this exception occurs, the wrapper invokes the Reset method which&nbsp;purges the&nbsp;cached settings, the re-attempts creating the instance.&nbsp; Since the mappings have been purged the settings are re-read from configuration and cached.&nbsp; Therefore any added type mappings will be picked up.</code></p>
<p><code class="cplusplus">I'd highly recommend using this wrapper approach to improve robustness if you are using the SharePointServiceLocator.</code></p>
<p><code class="cplusplus">The wrapper class can add a few additonal similar methods to handle all of the different ways service locator can create instances as the following code demonstrates.</code></p>
<p><code class="cplusplus"></code></p>
<p><code class="cplusplus">
<pre class="scroll"><code class="cplusplus">public class ServiceLocatorWrapper
<br />{
<br />    public static TService GetInstance&lt;TService&gt;()
<br />    {
<br />        TService instance;
<br />
<br />        try
<br />        {
<br />            instance = SharePointServiceLocator.GetCurrent().GetInstance&lt;TService&gt;();
<br />        }
<br />        catch (ActivationException e)
<br />        {
<br />            LogFailure&lt;TService&gt;(e);
<br />            SharePointServiceLocator.Reset();
<br />            instance = SharePointServiceLocator.GetCurrent().GetInstance&lt;TService&gt;();
<br />        }
<br />
<br />        return instance;
<br />    }
<br />
<br />    public static TService GetInstance&lt;TService&gt;(string key)
<br />    {
<br />        TService instance;
<br />
<br />        try
<br />        {
<br />            instance = SharePointServiceLocator.GetCurrent().GetInstance&lt;TService&gt;(key);
<br />        }
<br />        catch (ActivationException e)
<br />        {
<br />            LogFailure&lt;TService&gt;(e);
<br />                SharePointServiceLocator.Reset();
<br />            instance = SharePointServiceLocator.GetCurrent().GetInstance&lt;TService&gt;(key);
<br />        }
<br />
<br />        return instance;
<br />    }
<br />
<br />    public static TService GetInstance&lt;TService&gt;(SPSite site)
<br />    {
<br />        TService instance;
<br />
<br />        try
<br />        {
<br />            instance = SharePointServiceLocator.GetCurrent(site).GetInstance&lt;TService&gt;();
<br />        }
<br />        catch (ActivationException e)
<br />        {
<br />            LogFailure&lt;TService&gt;(e);
<br />            SharePointServiceLocator.Reset();
<br />            instance = SharePointServiceLocator.GetCurrent(site).GetInstance&lt;TService&gt;();
<br />        }
<br />
<br />        return instance;
<br />    }
<br />
<br />    public static TService GetInstance&lt;TService&gt;(SPSite site, string key)
<br />    {
<br />        TService instance;
<br />
<br />        try
<br />        {
<br />            instance = SharePointServiceLocator.GetCurrent(site).GetInstance&lt;TService&gt;(key);
<br />        }
<br />        catch (ActivationException e)
<br />        {
<br />            LogFailure&lt;TService&gt;(e);
<br />            SharePointServiceLocator.Reset();
<br />            instance = SharePointServiceLocator.GetCurrent(site).GetInstance&lt;TService&gt;(key);
<br />        }
<br />
<br />        return instance;
<br />    }
<br />
<br />    private static void LogFailure&lt;TService&gt;(Exception e)
<br />    {
<br />        ILogger logger = SharePointServiceLocator.GetCurrent().GetInstance&lt;ILogger&gt;();
<br />        logger.TraceToDeveloper(e, string.Format("Failed to load type {0}, flushing service locator and retrying", typeof(TService).Name));
<br />    }
<br />}</code></pre>
</code></p>]]></content:encoded>
    </item>
    <item>
      <title>Using a SharePoint Timer Job to deploy settings in a farm for registering event sources</title>
      <link>https://docs.microsoft.com/archive/blogs/chriskeyser/using-a-sharepoint-timer-job-to-deploy-settings-in-a-farm-for-registering-event-sources</link>
      <pubDate>Mon, 20 Dec 2010 11:39:00 GMT</pubDate>
      <dc:creator><![CDATA[Chris Keyser]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/chriskeyser/2010/12/20/using-a-sharepoint-timer-job-to-deploy-settings-in-a-farm-for-registering-event-sources/</guid>
      <description><![CDATA[Creating the timer job
Sometimes you need to deploy settings to every web front end, or have a...]]></description>
      <content:encoded><![CDATA[<h1></h1>
<h1>Creating the timer job</h1>
<p>Sometimes you need to deploy settings to every web front end, or have a piece of code execute on every web front end.&nbsp; Your choices in SharePoint are to either have an administrator run a script on each front end, or use a timer job.&nbsp; This blog post describes how to use a timer job to accomplish this.&nbsp; </p>
<p>When you log to the event log using the native event log APIs or using the new SharePoint 2010 logging capabilities the event source used for the event logged must already exist, or the code must be running in a context that has privileges to create the event source, which means permissions to write to the registry.&nbsp; If the code doesn't have those permissions, then a security exception will be thrown.&nbsp; This condition may also show up as an security exception for attempting to access logs to which you don't have access.&nbsp; This may occur as the Event Log searches for the event source registration in different event logs, in particular the security log, to which your code may also not have access to.&nbsp; In any case, if you are attempting to log an event using the SharePoint 2010 APIs and are receiving a security exception, there is a good chance that the event source being used hasn't been created on the machine.&nbsp; Since event sources require registration in the local machine registry, the registration process must take place on each physical machine (i.e., code must run in a high privileged account on each WFE to register the event sources).</p>
<p>&nbsp;</p>
<p>I'll state the obvious first.&nbsp; The timer job must be running under an account that can write to the registry.&nbsp; The timer job runs under the managed account called "farm account", and its pretty common to make this a relatively high permission account.&nbsp; By default it is running as NETWORK SERVICE, and NETWORK SERVICE will not have permission to write the settings to the registry, and therefore the process described in this blog will not work with default settings.&nbsp; This obvious one got me when I first developed this solution.&nbsp; You can added managed accounts in central admin-&gt;security-&gt;configure managed accounts, and you can set the farm account in central admin-&gt;security-&gt;configure service accounts.</p>
<p>There are two components that need to be developed; the timer job, and a feature to activate the timer job.&nbsp; We'll start with the timer job.&nbsp; A SharePoint timer job runs under a SharePoint managed process for timer execution (OWSTimer.exe, SharePoint 2010 Timer service).&nbsp; Timers run as one might expect periodically.&nbsp; The timer jobs that are scheduled, and the intervals at which they run are described here: <a href="https://technet.microsoft.com/en-us/library/cc678870.aspx">https://technet.microsoft.com/en-us/library/cc678870.aspx</a>.&nbsp; The type of timer job we'll create for this particular task will run only once, but will run on every web front end.</p>
<p>This example implementation uses the patterns and practices SharePoint Guidance Library that can be downloaded from <a href="https://www.microsoft.com/spg">https://www.microsoft.com/spg</a>.&nbsp; </p>
<p>The following listing shows the full source code for the timer job:</p>
<div id="codeSnippetWrapper" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 350px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">using</span> System;</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span> <span style="color: #0000ff">using</span> System.Collections.Generic;</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span> <span style="color: #0000ff">using</span> System.Linq;</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span> <span style="color: #0000ff">using</span> System.Text;</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum5" style="color: #606060">   5:</span> <span style="color: #0000ff">using</span> Microsoft.SharePoint.Administration;</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum6" style="color: #606060">   6:</span> <span style="color: #0000ff">using</span> Microsoft.Practices.SharePoint.Common.Logging;</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum7" style="color: #606060">   7:</span> <span style="color: #0000ff">using</span> Microsoft.SharePoint;</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum8" style="color: #606060">   8:</span> <span style="color: #0000ff">using</span> Microsoft.Practices.SharePoint.Common.ServiceLocation;</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum9" style="color: #606060">   9:</span>&nbsp; </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum10" style="color: #606060">  10:</span> <span style="color: #0000ff">namespace</span> CreateEventSourcesJob</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum11" style="color: #606060">  11:</span> {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum12" style="color: #606060">  12:</span>     <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum13" style="color: #606060">  13:</span>     <span style="color: #008000">/// This class defines a timer job that will run on each WFE once.  The timer job will create the event </span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum14" style="color: #606060">  14:</span>     <span style="color: #008000">/// sources for the logger if the event sources do not already exist.  Note that the account the timer </span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum15" style="color: #606060">  15:</span>     <span style="color: #008000">/// job runs under must have sufficient permissions to write to the registry.  The default account </span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum16" style="color: #606060">  16:</span>     <span style="color: #008000">/// is NetworkService, which does not have permission to write to the directory.  This account is </span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum17" style="color: #606060">  17:</span>     <span style="color: #008000">/// changed through central admin.  You will first need to create a managed account </span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum18" style="color: #606060">  18:</span>     <span style="color: #008000">/// (CentralAdmin-&gt;Security-&gt;Configure Managed Accounts) with an account that has sufficent permission.</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum19" style="color: #606060">  19:</span>     <span style="color: #008000">/// You then assign the account (CentralAdmin-&gt;Security-&gt;Configure Service Accounts) to the Farm Account, </span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum20" style="color: #606060">  20:</span>     <span style="color: #008000">/// which is used by the timer.  At this point this task will work.</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum21" style="color: #606060">  21:</span>     <span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum22" style="color: #606060">  22:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> CreateLoggingEventSourceJob: SPJobDefinition</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum23" style="color: #606060">  23:</span>     {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum24" style="color: #606060">  24:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">string</span> JobName { get { <span style="color: #0000ff">return</span> <span style="color: #006080">"CreateLoggingEventSources"</span>; } }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum25" style="color: #606060">  25:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">string</span> JobTitle { get { <span style="color: #0000ff">return</span> <span style="color: #006080">"Create Logging Event Sources"</span>; } }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum26" style="color: #606060">  26:</span>&nbsp; </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum27" style="color: #606060">  27:</span>         <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum28" style="color: #606060">  28:</span>         <span style="color: #008000">/// Creates the job instance</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum29" style="color: #606060">  29:</span>         <span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum30" style="color: #606060">  30:</span>         <span style="color: #0000ff">public</span> CreateLoggingEventSourceJob()</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum31" style="color: #606060">  31:</span>             : <span style="color: #0000ff">base</span>(JobName, SPFarm.Local.TimerService, <span style="color: #0000ff">null</span>, SPJobLockType.None)</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum32" style="color: #606060">  32:</span>         {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum33" style="color: #606060">  33:</span>         }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum34" style="color: #606060">  34:</span>&nbsp; </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum35" style="color: #606060">  35:</span>         <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum36" style="color: #606060">  36:</span>         <span style="color: #008000">/// A description of the job being ran</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum37" style="color: #606060">  37:</span>         <span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum38" style="color: #606060">  38:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">string</span> Description</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum39" style="color: #606060">  39:</span>         {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum40" style="color: #606060">  40:</span>             get</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum41" style="color: #606060">  41:</span>             {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum42" style="color: #606060">  42:</span>                 <span style="color: #0000ff">return</span> <span style="color: #006080">"Registers the event sources on WFE's for patterns and practices Logger"</span>;</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum43" style="color: #606060">  43:</span>             }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum44" style="color: #606060">  44:</span>         }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum45" style="color: #606060">  45:</span>&nbsp; </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum46" style="color: #606060">  46:</span>         <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum47" style="color: #606060">  47:</span>         <span style="color: #008000">/// The name to display for the job.</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum48" style="color: #606060">  48:</span>         <span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum49" style="color: #606060">  49:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">string</span> DisplayName</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum50" style="color: #606060">  50:</span>         {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum51" style="color: #606060">  51:</span>             get</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum52" style="color: #606060">  52:</span>             {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum53" style="color: #606060">  53:</span>                 <span style="color: #0000ff">return</span> <span style="color: #006080">"patterns and practices Event Source Registration job"</span>;</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum54" style="color: #606060">  54:</span>             }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum55" style="color: #606060">  55:</span>         }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum56" style="color: #606060">  56:</span>&nbsp; </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum57" style="color: #606060">  57:</span>         <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum58" style="color: #606060">  58:</span>         <span style="color: #008000">/// Called by SharePoint when this job is ran.  This is where the work is done, whihc is simply</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum59" style="color: #606060">  59:</span>         <span style="color: #008000">/// calling EnsureConfiguredAreasRegistered.  This method reads the areas from configuration, and</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum60" style="color: #606060">  60:</span>         <span style="color: #008000">/// creates an event source for each if it doesn't exist.  It will also create the event source</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum61" style="color: #606060">  61:</span>         <span style="color: #008000">/// for the default category if it does not exist.</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum62" style="color: #606060">  62:</span>         <span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum63" style="color: #606060">  63:</span>         <span style="color: #008000">/// &lt;param name="targetInstance"&gt;The target instance IDfor the job&lt;/param&gt;</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum64" style="color: #606060">  64:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Execute(Guid targetInstance)</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum65" style="color: #606060">  65:</span>         {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum66" style="color: #606060">  66:</span>             <span style="color: #0000ff">try</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum67" style="color: #606060">  67:</span>             {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum68" style="color: #606060">  68:</span>                 DiagnosticsAreaEventSource.EnsureConfiguredAreasRegistered();</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum69" style="color: #606060">  69:</span>             }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum70" style="color: #606060">  70:</span>             <span style="color: #0000ff">catch</span> (Exception e)</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum71" style="color: #606060">  71:</span>             {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum72" style="color: #606060">  72:</span>                 var logger = SharePointServiceLocator.GetCurrent().GetInstance&lt;ILogger&gt;();</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum73" style="color: #606060">  73:</span>                 logger.TraceToDeveloper(e);</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum74" style="color: #606060">  74:</span>                 logger.LogToOperations(e.ToString() + <span style="color: #006080">" INNER EXCEPTION: e.InnerException.ToString()"</span>, </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum75" style="color: #606060">  75:</span>                     EventSeverity.ErrorCritical);</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum76" style="color: #606060">  76:</span>                 <span style="color: #0000ff">throw</span>;</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum77" style="color: #606060">  77:</span>             }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum78" style="color: #606060">  78:</span>         }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum79" style="color: #606060">  79:</span>&nbsp; </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum80" style="color: #606060">  80:</span>         <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum81" style="color: #606060">  81:</span>         <span style="color: #008000">/// This method sets up the timer to fire.  This is the method after the logging categories are </span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum82" style="color: #606060">  82:</span>         <span style="color: #008000">/// setup that an application can call to register the event sources.</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum83" style="color: #606060">  83:</span>         <span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum84" style="color: #606060">  84:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> ScheduleJob()</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum85" style="color: #606060">  85:</span>         {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum86" style="color: #606060">  86:</span>             CleanUpJobs(SPFarm.Local.TimerService.JobDefinitions);</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum87" style="color: #606060">  87:</span>             var job = <span style="color: #0000ff">new</span> CreateLoggingEventSourceJob();</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum88" style="color: #606060">  88:</span>             job.Schedule = <span style="color: #0000ff">new</span> SPOneTimeSchedule(DateTime.Now);</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum89" style="color: #606060">  89:</span>              job.Update();</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum90" style="color: #606060">  90:</span>         }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum91" style="color: #606060">  91:</span>&nbsp; </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum92" style="color: #606060">  92:</span>         <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum93" style="color: #606060">  93:</span>         <span style="color: #008000">/// Cleans up and old versions found of the job.</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum94" style="color: #606060">  94:</span>         <span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum95" style="color: #606060">  95:</span>         <span style="color: #008000">/// &lt;param name="jobs"&gt;The job list.&lt;/param&gt;</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum96" style="color: #606060">  96:</span>         <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> CleanUpJobs(SPJobDefinitionCollection jobs)</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum97" style="color: #606060">  97:</span>         {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum98" style="color: #606060">  98:</span>             <span style="color: #0000ff">foreach</span> (SPJobDefinition job <span style="color: #0000ff">in</span> jobs)</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum99" style="color: #606060">  99:</span>             {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum100" style="color: #606060"> 100:</span>                 <span style="color: #0000ff">if</span> (job.Name.Equals(CreateLoggingEventSourceJob.JobName,</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum101" style="color: #606060"> 101:</span>                                         StringComparison.OrdinalIgnoreCase))</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum102" style="color: #606060"> 102:</span>                 {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum103" style="color: #606060"> 103:</span>                     job.Delete();</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum104" style="color: #606060"> 104:</span>                 }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum105" style="color: #606060"> 105:</span>             }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum106" style="color: #606060"> 106:</span>         }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum107" style="color: #606060"> 107:</span>     }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum108" style="color: #606060"> 108:</span> }</pre>
<!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></div>
</div>
<p>We&rsquo;ll take a look now at specific portions of the timer job.&nbsp; First every timer job will derive from SPJobDefinition.&nbsp; When the timer job is constructed, the base class is initialized with a few parameters:</p>
<ul>
<li>JobName &ndash; the name of our job.</li>
<li>SPFarm.Logal.TimerService &ndash; We need to associate either a service or a web application with the timer job we want to run.&nbsp; In this case we specify the the timer service as the associated service.</li>
<li>null &ndash; this specifies the SharePoint server to run the job on.&nbsp; By specifying null it will be ran on all servers.</li>
<li>SPJobLockType.None &ndash; this isn&rsquo;t obvious, but by defining a lock job type of none, we are telling SharePoint to run this on every machine.&nbsp; For more information, see <a href="https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spjoblocktype.aspx" title="https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spjoblocktype.aspx">https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spjoblocktype.aspx</a>.</li>
</ul>
<p>There are two other interesting pieces to the timer job.&nbsp; First we need to schedule the job to run.&nbsp; The class provides a convenience function to schedule this timer job for code that has registered a new logging category for the logger to use.&nbsp; The convenience function is a static function that registers an instance of the timer to execute.&nbsp; The timer is a single shot timer that will only run once.&nbsp; It is scheduled to run immediately with the timer service.</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span> <span style="color: #008000">/// This method sets up the timer to fire.  This is the method after the logging categories are </span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span> <span style="color: #008000">/// setup that an application can call to register the event sources.</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span> <span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum5" style="color: #606060">   5:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> ScheduleJob()</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum6" style="color: #606060">   6:</span> {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum7" style="color: #606060">   7:</span>     CleanUpJobs(SPFarm.Local.TimerService.JobDefinitions);</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum8" style="color: #606060">   8:</span>     var job = <span style="color: #0000ff">new</span> CreateLoggingEventSourceJob();</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum9" style="color: #606060">   9:</span>     job.Schedule = <span style="color: #0000ff">new</span> SPOneTimeSchedule(DateTime.Now);</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum10" style="color: #606060">  10:</span>      job.Update();</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum11" style="color: #606060">  11:</span> }</pre>
<!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></div>
</div>
<p>When the job is registered we need to clean up any previous jobs otherwise the job will fail due to a duplicate definition. <strong>CleanUpJobs </strong>will clean out any previous instances for us before the current job is scheduled.&nbsp; <strong>CleanupJobs </strong>will construct an instance of the job, so you can&rsquo;t do this logic easily in the constructor, rather it&rsquo;s done before the timer is rescheduled as shown above.</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span> <span style="color: #008000">/// Cleans up and old versions found of the job.</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span> <span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span> <span style="color: #008000">/// &lt;param name="jobs"&gt;The job list.&lt;/param&gt;</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum5" style="color: #606060">   5:</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> CleanUpJobs(SPJobDefinitionCollection jobs)</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum6" style="color: #606060">   6:</span> {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum7" style="color: #606060">   7:</span>     <span style="color: #0000ff">foreach</span> (SPJobDefinition job <span style="color: #0000ff">in</span> jobs)</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum8" style="color: #606060">   8:</span>     {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum9" style="color: #606060">   9:</span>         <span style="color: #0000ff">if</span> (job.Name.Equals(CreateLoggingEventSourceJob.JobName,</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum10" style="color: #606060">  10:</span>                                 StringComparison.OrdinalIgnoreCase))</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum11" style="color: #606060">  11:</span>         {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum12" style="color: #606060">  12:</span>             job.Delete();</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum13" style="color: #606060">  13:</span>         }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum14" style="color: #606060">  14:</span>     }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum15" style="color: #606060">  15:</span> }</pre>
<!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></div>
</div>
<p>The final piece is where the work is actually done.&nbsp; The timer will construct this class to run the job, then call the Execute method.&nbsp; The work is actually done in the Execute method.</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span> <span style="color: #008000">/// Called by SharePoint when this job is ran.  This is where the work is done, whihc is simply</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span> <span style="color: #008000">/// calling EnsureConfiguredAreasRegistered.  This method reads the areas from configuration, and</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span> <span style="color: #008000">/// creates an event source for each if it doesn't exist.  It will also create the event source</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum5" style="color: #606060">   5:</span> <span style="color: #008000">/// for the default category if it does not exist.</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum6" style="color: #606060">   6:</span> <span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum7" style="color: #606060">   7:</span> <span style="color: #008000">/// &lt;param name="targetInstance"&gt;The target instance IDfor the job&lt;/param&gt;</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum8" style="color: #606060">   8:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Execute(Guid targetInstance)</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum9" style="color: #606060">   9:</span> {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum10" style="color: #606060">  10:</span>     <span style="color: #0000ff">try</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum11" style="color: #606060">  11:</span>     {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum12" style="color: #606060">  12:</span>         DiagnosticsAreaEventSource.EnsureConfiguredAreasRegistered();</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum13" style="color: #606060">  13:</span>     }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum14" style="color: #606060">  14:</span>     <span style="color: #0000ff">catch</span> (Exception e)</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum15" style="color: #606060">  15:</span>     {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum16" style="color: #606060">  16:</span>         var logger = SharePointServiceLocator.GetCurrent().GetInstance&lt;ILogger&gt;();</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum17" style="color: #606060">  17:</span>         logger.TraceToDeveloper(e);</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum18" style="color: #606060">  18:</span>         logger.LogToOperations(e.ToString() + <span style="color: #006080">" INNER EXCEPTION: e.InnerException.ToString()"</span>, </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum19" style="color: #606060">  19:</span>             EventSeverity.ErrorCritical);</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum20" style="color: #606060">  20:</span>         <span style="color: #0000ff">throw</span>;</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum21" style="color: #606060">  21:</span>     }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum22" style="color: #606060">  22:</span> }</pre>
<!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></div>
</div>
<p>In this method, a convenience method is called in the SharePoint Guidance Library that will read all of the diagnostic areas, and register an event source for each (SharePoint logging has diagnostic areas, and each diagnostic area has one or more diagnostic category.&nbsp; An example of a diagnostic area would be search or taxonomy).&nbsp; The following code is provided by the guidance library &ndash; if you had your own implementation based upon the <strong>SPDiagnosticsServiceBase</strong> class, you would need to implement similar logic:</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span>  <span style="color: #008000">/// Ensures that all configured DiagnosticAreas are registered as event sources.</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span>  <span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span>  [SharePointPermission(SecurityAction.InheritanceDemand, ObjectModel = <span style="color: #0000ff">true</span>)]</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum5" style="color: #606060">   5:</span>  [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = <span style="color: #0000ff">true</span>)]</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum6" style="color: #606060">   6:</span>  <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> EnsureConfiguredAreasRegistered()</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum7" style="color: #606060">   7:</span>  {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum8" style="color: #606060">   8:</span>      var mgr = SharePointServiceLocator.GetCurrent().GetInstance&lt;IConfigManager&gt;();</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum9" style="color: #606060">   9:</span>      var areas = <span style="color: #0000ff">new</span> DiagnosticsAreaCollection(mgr);</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum10" style="color: #606060">  10:</span>      RegisterAreas(areas);</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum11" style="color: #606060">  11:</span>  }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum12" style="color: #606060">  12:</span>&nbsp; </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum13" style="color: #606060">  13:</span>  <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum14" style="color: #606060">  14:</span>  <span style="color: #008000">/// Takes all the configured areas and set them as event sources.</span></pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum15" style="color: #606060">  15:</span>  <span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<!--crlf--><!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum17" style="color: #606060">  17:</span>  <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> RegisterAreas(DiagnosticsAreaCollection areas)</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum18" style="color: #606060">  18:</span>  {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum19" style="color: #606060">  19:</span>      Validation.ArgumentNotNull(areas, <span style="color: #006080">"areas"</span>);</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum20" style="color: #606060">  20:</span>&nbsp; </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum21" style="color: #606060">  21:</span>      <span style="color: #0000ff">foreach</span> (DiagnosticsArea area <span style="color: #0000ff">in</span> areas)</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum22" style="color: #606060">  22:</span>      {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum23" style="color: #606060">  23:</span>          <span style="color: #0000ff">if</span> (!EventLog.SourceExists(area.Name))</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum24" style="color: #606060">  24:</span>          {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum25" style="color: #606060">  25:</span>              EventLog.CreateEventSource(area.Name, Constants.EventLogName);</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum26" style="color: #606060">  26:</span>          }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum27" style="color: #606060">  27:</span>      }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum28" style="color: #606060">  28:</span>&nbsp; </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum29" style="color: #606060">  29:</span>      <span style="color: #0000ff">if</span> (!EventLog.SourceExists(DiagnosticsArea.DefaultSPDiagnosticsArea.Name))</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum30" style="color: #606060">  30:</span>      {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum31" style="color: #606060">  31:</span>          EventLog.CreateEventSource(DiagnosticsArea.DefaultSPDiagnosticsArea.Name, Constants.EventLogName);</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum32" style="color: #606060">  32:</span>      }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum33" style="color: #606060">  33:</span>  }</pre>
<!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></div>
</div>
<p>At this point all of the logic has been demonstrated for registering the events.&nbsp; </p>
<h1><strong><em>Registering Diagnostic Areas and Categories with SharePoint Logger</em></strong></h1>
<p>The <strong>SharePointLogger </strong>supports adding new diagnostic areas and categories through configuration rather than implementation.&nbsp; Typically this will be done through a feature receiver.&nbsp; These configuration values are stored using the ConfigurationManager in farm level configuration.&nbsp; SharePoint prevents writing to Farm Level configuration from a content web.&nbsp; FeatureActivated for site and web scoped features will run in the context of the content web (unless activated from powershell/stsadm).&nbsp; Therefore you should not register this information in the FeatureActivated event of a web or site scoped feature.&nbsp; There are two approaches that can be taken.&nbsp; </p>
<ul>
<li>The first, and recommended approach is to scope the feature as Farm.&nbsp; Since the configuration settings apply across the farm, its reasonable to use a farm scoped feature to set the values.&nbsp; A farm scoped feature will run at a sufficiently high permission level, outside of the content web, to write to farm level configuration.&nbsp; In the case of a farm scoped feature, the logic may either be in the FeatureActivated or FeatureInstalled event, and by default SharePoint will activate a farm scoped feature when it is installed.&nbsp; </li>
<li>The second choice is to put the logic in the FeatureInstalled event of a Site or Web scoped feature.&nbsp; This method is less preferred, but still reasonable since the feature must be deactivated on all webs or sites in the farm before it can be removed.&nbsp; </li>
</ul>
<p>The following example shows how to register the events from a farm scoped event receiver:</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> FeatureActivated(SPFeatureReceiverProperties properties)</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span>     var mgr = SharePointServiceLocator.GetCurrent().GetInstance&lt;IConfigManager&gt;();</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span>     DiagnosticsAreaCollection areas = <span style="color: #0000ff">new</span> DiagnosticsAreaCollection(mgr);</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum5" style="color: #606060">   5:</span>&nbsp; </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum6" style="color: #606060">   6:</span>&nbsp; </pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum7" style="color: #606060">   7:</span>     <span style="color: #0000ff">if</span> (areas[<span style="color: #006080">"loonytunes"</span>] == <span style="color: #0000ff">null</span>)</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum8" style="color: #606060">   8:</span>     {</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum9" style="color: #606060">   9:</span>         var area = <span style="color: #0000ff">new</span> DiagnosticsArea(<span style="color: #006080">"loonytunes"</span>);</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum10" style="color: #606060">  10:</span>         area.DiagnosticsCategories.Add(<span style="color: #0000ff">new</span> DiagnosticsCategory(<span style="color: #006080">"bugs bunny"</span>));</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum11" style="color: #606060">  11:</span>         areas.Add(area);</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum12" style="color: #606060">  12:</span>         areas.SaveConfiguration();</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum13" style="color: #606060">  13:</span>     }</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum14" style="color: #606060">  14:</span>     CreateLoggingEventSourceJob.ScheduleJob();</pre>
<!--crlf-->
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum15" style="color: #606060">  15:</span> }</pre>
<!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></!--crlf--></div>
</div>
<p>In this example the areas are added for the logger, and then the job is scheduled to create the event sources. </p>
<p>One more caution.&nbsp; The timer will tend to lock the assembly.&nbsp; You should recycle the timer service whenever you are rebuilding to ensure the assembly isn&rsquo;t locked and therefore not updated in the GAC.</p>
<p>I have attached the full source as well.&nbsp; Hope this is helpful.</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/10/10/73/50/CreateEventSourcesJob.zip" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-10-73-50/CreateEventSourcesJob.zip">CreateEventSourcesJob.zip</a></p>]]></content:encoded>
    </item>
    <item>
      <title>Debugging a FeatureInstalled event handler in SharePoint 2010</title>
      <link>https://docs.microsoft.com/archive/blogs/chriskeyser/debugging-a-featureinstalled-event-handler-in-sharepoint-2010</link>
      <pubDate>Mon, 26 Jul 2010 07:37:00 GMT</pubDate>
      <dc:creator><![CDATA[Chris Keyser]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/chriskeyser/2010/07/26/debugging-a-featureinstalled-event-handler-in-sharepoint-2010/</guid>
      <description><![CDATA[Updated: An even easier method to do this is to simply launch the debugger by adding a statement to...]]></description>
      <content:encoded><![CDATA[<p><strong>Updated</strong>: An even easier method to do this is to simply launch the debugger by adding a statement to the beginning of the method for Systems.Diagnostics.Debugger.Launch.&nbsp; Thanks to the suggestion I received on the topic - I misplaced the email, so the input will need to remain anonymous...</p>
<p><strong>Updated: Debugging Feature Activated</strong> - so I said below that debugging feature activated is easy, which is true especially for site and web scoped features, but may not be obvious if you haven't done it before.&nbsp; To debug FeatureActivated,</p>
<p class="MsoListParagraph"><span style="color: #000000;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt; mso-fareast-font-family: Calibri;"><span style="mso-list: Ignore;">1)<span style="font: 7pt 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">Properties-&gt;SharePoint, change Active Deployment Configuration to <i>No Activation</i><o:p></o:p></span></span></p>
<p class="MsoListParagraph"><span style="color: #000000;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt; mso-fareast-font-family: Calibri;"><span style="mso-list: Ignore;">2)<span style="font: 7pt 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">Start the project in the debugger.<o:p></o:p></span></span></p>
<p class="MsoListParagraph"><span style="color: #000000;"><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt; mso-fareast-font-family: Calibri;"><span style="mso-list: Ignore;">3)<span style="font: 7pt 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span style="font-family: 'Calibri','sans-serif'; font-size: 11pt;">Activate the feature through site settings (assuming it&rsquo;s a site or web scoped feature).</span></span></p>
<p class="MsoListParagraph">&nbsp;</p>
<p><strong><span style="font-size: medium;">Original Post</span></strong></p>
<hr width="1024" style="width: 1024px;" />
<p>&nbsp;</p>
<p>Recently I had some code that&nbsp;I had to put in feature installed.&nbsp; Debugging FeatureActivated is no problem in VS2010, but FeatureInstalled isn't so simple.&nbsp; The feature installed event gets fired&nbsp;when a feature is installed to the farm.&nbsp; It runs under a higher security context than feature activate, therefore you can do things like write settings to the farm or web application.&nbsp; In my case my feature installed code had a bug, but I wasn't sure how I could hook it.&nbsp; I found one approach at least that did work, so I figured I'd blog about it.</p>
<p>There are three possibilities for debugging the FeatureInstalled (this is for a Farm Solution):</p>
<p>1. The FeatureInstalled is throwing an exception and deploy fails in VS.&nbsp; It's OK that the FeatureInstalled ran partially, and you can rerun to debug.</p>
<p>2. The FeatureInstalled is not throwing an exception, but its OK that it ran once and you can run it again.</p>
<p>3. You can't have the FeatureInstalled event run partially or completely prior to debugging.</p>
<p>The first 2 cases are actually pretty simple.&nbsp; First deploy the project from VS.&nbsp; This will put everything in the right place, even if an exception is thrown in your FeatureInstalled, and it looks like the deploy failed.&nbsp; Then follow the following process.</p>
<p>One way that features are commonly installed is from the command line using STSADM or powershell.&nbsp; You can use this to hook into the debugging process.&nbsp; In this case I will use the "TestFarmSettings" project as an example.&nbsp; I did the following in order to debug the feature installed:</p>
<p>&nbsp;</p>
<ol>
<li>&nbsp;Added a command line&nbsp;project to my solution. <ol>
<li>set build, platform target to any cpu (under build tab), and set project to the 3.5 framework (under application tab)</li>
<li>On the debug tab, set start external program, and set the external program to "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN\stsadm.EXE"</li>
<li>Add a reference to my sharepoint project that has the feature installed you want to debug.</li>
</ol></li>
<li>Set the breakpoint on myFeatureInstalled event handler.</li>
<li>Deploy the SharePoint project from VS.&nbsp; It is OK if the deploy fails due to an exception thrown in the feature receiver.</li>
<li>Add the following for the command line options for the console application in the debug tab: -o installfeature -filename &lt;your feature name&gt;\feature.xml -force.&nbsp; For example, I used -o installfeature -filename TestFarmSettingStore_Feature1\feature.xml -force.&nbsp; </li>
<li>Set the console application as the startup project</li>
<li>Hit F5.&nbsp; Your breakpoint will be hit.</li>
</ol>
<p>Case 3 its more complicated since you need to get the files into the right places yourself.&nbsp; As a little background, the reason why you run the deploy is to get the solution added and installed, and get the debug information deployed for the gac'd assembly.&nbsp; If you do the add and install solution methods manually, then run the debug files don't get into the right place for the assembly.&nbsp;</p>
<p>You will need to run some manual steps to setup the conditions:</p>
<ol>
<li>You will need to add the solution, which should be in the debug\bin output of your project.&nbsp; stsadm.exe is located in&nbsp;"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN\".</li>
<li>Add the solution, for example stsadm -o addsolution -filename TestFarmSettingStore.wsp</li>
<li>Deploy the solution, for example, &gt;stsadm -o deploysolution -name testfarmsettingstore.wsp -url <a href="https://localhost">https://localhost</a> -immediate -allowGacDeployment</li>
</ol>
<p>Now you are ready to actually install a feature.&nbsp;Remember that by default farm and web app scoped features will install automatically when the deploy solution step&nbsp;is ran,&nbsp;and so you&nbsp;will need to change this behavior for those solution types.&nbsp; This is controlled by the <span class="input"><strong>ActivateOnDefault </strong>property for the feature (see<a href="https://msdn.microsoft.com/en-us/library/ms436075.aspx" title="Feature Element (Feature)"> Feature Element (Feature)</a>)<!----></span> </p>
<p>You will need to copy the debug file (.pdb) into the location that your gac'd assembly gets installed.&nbsp; You can find that location by running dir /s c:\windows\assembly\&lt;your assembly name&gt;.dll.&nbsp;&nbsp; Once the debug file is located beside the assembly, you can then perform the steps outlined with the console app.&nbsp; You will be able to break on the FeatureInstalled event the first time you run it.</p>
<p>&nbsp;If anyone has an easier way, please let me know!</p>
<p>&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>PDF version of Developing Applications for SharePoint 2010 available</title>
      <link>https://docs.microsoft.com/archive/blogs/chriskeyser/pdf-version-of-developing-applications-for-sharepoint-2010-available</link>
      <pubDate>Fri, 23 Jul 2010 14:36:47 GMT</pubDate>
      <dc:creator><![CDATA[Chris Keyser]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/chriskeyser/2010/07/23/pdf-version-of-developing-applications-for-sharepoint-2010-available/</guid>
      <description><![CDATA[We posted a PDF version of the patterns and practices guidance release, Developing Applications for...]]></description>
      <content:encoded><![CDATA[<p>We posted a PDF version of the patterns and practices guidance release, Developing Applications for SharePoint 2010.&nbsp; This guidance will really help you understand the architecture and development decisions for SharePoint 2010 custom application development, and has rich code examples as well as a reusable library.&nbsp; The MSDN release is available at <a href="https://www.microsoft.com/spg">www.microsoft.com/spg</a>, and the PDF version can be downaloed from codeplex at <a href="https://spg.codeplex.com">https://spg.codeplex.com</a>.&nbsp; Please provide feedback either through my blog, on the discussion forums of the Codeplex site, or you can directly email me at <a href="mailto:ckeyser@microsoft.com">ckeyser@microsoft.com</a>.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>Adding a central admin page for managing applications settings</title>
      <link>https://docs.microsoft.com/archive/blogs/chriskeyser/adding-a-central-admin-page-for-managing-applications-settings</link>
      <pubDate>Wed, 14 Jul 2010 06:47:36 GMT</pubDate>
      <dc:creator><![CDATA[Chris Keyser]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/chriskeyser/2010/07/14/adding-a-central-admin-page-for-managing-applications-settings/</guid>
      <description><![CDATA[&nbsp;
You can add a page to central administration and add an entry to the central administration...]]></description>
      <content:encoded><![CDATA[<p>&nbsp;</p>
<p>You can add a page to central administration and add an entry to the central administration menu to manage application settings for the farm or web application.&nbsp; As I reviewed in my previous post, <a href="https://blogs.msdn.com/b/chriskeyser/archive/2010/07/12/using-the-applications-setting-manager-with-web-app-config-developing-applications-for-sharepoint-2010.aspx">Using the Applications Setting Manager with Web App Config (Developing Applications for SharePoint 2010)</a>, the settings for a farm or web application cannot be written to from a content web.&nbsp; However they can be written to from a page hosted in central administration.&nbsp; For information on how to add a page and the menu entry to central admin, see <a target="_blank" href="https://msdn.microsoft.com/en-us/library/ff798467.aspx" title="How to: Deploy an Application Page to Central Administration">How to: Deploy an Application Page to Central Administration</a>. </p>
<p>Once the page is deployed, you'll need to actually add the code to update the settings to central admin.&nbsp; This example demonstrates a simple interface for configuring settings.&nbsp; One thing I found is that when developing a page for central admin, you'll often need to reset IIS between each debugging session since it appears things are being cached.&nbsp; I'd start off getting the administration page visible following the above how-to, and then you can perform the following to actually build out updating settings.</p>
<p>The page I built is pretty simple.&nbsp; It updates a single known setting as described below:</p>
<pre class="scroll"><code class="csharp">&lt;asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server"&gt;
<br />            Select Web App:&lt;asp:DropDownList ID="DDWebApplications" runat="server" /&gt;
<br />            &lt;br /&gt;Enter Setting Value:
<br />            &lt;asp:TextBox ID="TextBoxSettingValue" runat="server"&gt;&lt;/asp:TextBox&gt;    
<br />            &lt;asp:Button Text="Save" ID="ButtonSave" runat="server" OnClick="OnSave" /&gt;
<br />&lt;/asp:Content&gt;
<br /></code></pre>
<p>&nbsp;</p>
<p>The approach I'll outline works well for simple types.&nbsp; The application setting manager can also store serialized complex types.&nbsp; In order to administer these you will need to be able to load the assembly that contains the classes so they can be properly serialized and deserialized.&nbsp; This post won't go into that detail.</p>
<p>As you can see, I'm a master of UI from the following screen shot of my custom page:</p>
<p>&nbsp;<img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/00/39/60/4061.Central%20Admin%20Custom%20Page.PNG" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-39-60/4061.Central-Admin-Custom-Page.PNG" border="0" /></p>
<p>Often the value you want to set is on a content web application.&nbsp; The dropdown list is populated when the page is loaded:</p>
<pre class="scroll"><code class="csharp">protected void Page_Load(object sender, EventArgs e)
<br />{
<br />    if (!IsPostBack)
<br />    {
<br />        SetWebApps();
<br />    }
<br />}
<br />
<br /> 
<br />protected void SetWebApps()
<br />{
<br />    DDWebApplications.Items.Clear();
<br />
<br />    var names = new List&lt;string&gt;();
<br />    var ids = new List&lt;Guid&gt;();
<br />
<br />    foreach (SPWebApplication webApp in SPWebService.ContentService.WebApplications)
<br />    {
        DDWebApplications.Items.Add(new ListItem(webApp.Name, webApp.Id.ToString()));
<br />    }
<br />}
<br />
<br /> 
<br /></code></pre>
<p>In order to set the value, a user selects the content web they want to set the value for, then enters the value and saves. &nbsp;The save logic is pretty simple:</p>
<pre class="scroll"><code class="csharp">protected void OnSave(object sender, EventArgs args)
<br />{
<br />    try
<br />    {
<br />        var locator = SharePointServiceLocator.GetCurrent();
<br />        var configMgr = locator.GetInstance&lt;IConfigManager&gt;();
<br />        string id = DDWebApplications.SelectedValue;
<br />        var webApp = SPWebService.ContentService.WebApplications[new Guid(id)];
<br />
<br />        IPropertyBag bag = new SPWebAppPropertyBag(webApp);
<br />        int valueToSet = int.Parse(TextBoxSettingValue.Text);
<br />
<br />        configMgr.SetInPropertyBag(settingName, valueToSet, bag);
<br />    }
<br />    catch (Exception ex)
<br />    {
<br />        SharePointServiceLocator.GetCurrent().GetInstance&lt;ILogger&gt;().LogToOperations(ex);
<br />    }
<br />}</code></pre>
<p>First the code gets the web app ID from the drop down list, creates an instance of that web app, and then creates a property bag based upon the web app.&nbsp; Under the covers, the property bag uses a <b>SPPersistedObject</b> derived class called <b>WebAppSettingStore </b>to store settings in.&nbsp; An instance of this class is a child of the web application.&nbsp; If the persisted doesn't exist and can't be loaded, then one is created and saved when the property bag is constructed.&nbsp; The setting value is then simply set into the property bag, which will under the covers store the value in the <b>WebAppSettingStore </b>instance.</p>
<p>That's it for building custom properties.&nbsp; Of course typically there are more settings, and they are more complex to manage, but this blog provides all of the pieces in terms of creating the admin page, getting the objects, and storing the objects.</p>
<p>&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>Using the Applications Setting Manager with Web App Config (Developing Applications for SharePoint 2010)</title>
      <link>https://docs.microsoft.com/archive/blogs/chriskeyser/using-the-applications-setting-manager-with-web-app-config-developing-applications-for-sharepoint-2010</link>
      <pubDate>Mon, 12 Jul 2010 07:47:00 GMT</pubDate>
      <dc:creator><![CDATA[Chris Keyser]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/chriskeyser/2010/07/12/using-the-applications-setting-manager-with-web-app-config-developing-applications-for-sharepoint-2010/</guid>
      <description><![CDATA[We got a recent post to our discussion forums about managing settings for a web application from a...]]></description>
      <content:encoded><![CDATA[<p>We got a recent post to our discussion forums about managing settings for a web application from a web part.&nbsp;&nbsp;I thought this would be a good opportunity to go into some more detail about some of the special handling required for Farm and web application settings, with some example code.&nbsp; </p>
<h2>Explanation&nbsp;</h2>
<p>Web application and Farm settings can't be written from a content web application.&nbsp; A security exception will be thrown when writing to either of these locations a content web site.&nbsp; A content web site is essentially sites that end users access, either internally or externally, and will typically be most of the web apps in your farm.&nbsp; This security feature ensures that settings at these levels can't be compromised due to a security flaw on a page being displayed to end users.&nbsp; Settings at these levels can be read without any issues.&nbsp; </p>
<p>The SharePoint Guidance Library included in the <a href="https://msdn.microsoft.com/en-us/library/ff770300.aspx" title="&lt;em&gt;Developing Applications for SharePoint 2010&lt;/em&gt; "><em>Developing Applications for SharePoint 2010</em> </a>includes an Application Settings Manager that can read and write settings at different levels, including the farm and the web application.&nbsp; The application settings manager classes help manage settings, and provide a feature for hierarchical lookup of settings (web-&gt;site-&gt;web application-&gt;farm).&nbsp; At the web application and farm levels the application settings manager uses a class derived from SPPersistedObject that is a child of the farm or web application.&nbsp; For more info on the design you can see&nbsp;the <a href="https://msdn.microsoft.com/en-us/library/ff798488.aspx" title="Application Settings Manager">Application Settings Manager</a>&nbsp;detailed documentation.&nbsp; </p>
<p>Since these classes are a child of the web application or farm, the same security rules apply to them as apply to the parent object.&nbsp; Therefore you can't write the settings to&nbsp; the farm level or the web app level from a web part hosted in a content web app.&nbsp; I wrote a quick spike to demonstrate.&nbsp; </p>
<p>Here is a simple web part:</p>
<p><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/00/39/60/8546.test%20web%20app%20settings.PNG" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-39-60/8546.test-web-app-settings.PNG" border="0" /></p>
<p>&nbsp;</p>
<p>The code that is behind both of these buttons is as follows:&nbsp;</p>
<pre class="scroll"><code class="cplusplus"><span style="font-size: x-small;">protected void OnWriteWebAppSettings(object sender, EventArgs e)
<br />{
<br />    var locator = SharePointServiceLocator.GetCurrent();
<br />    var configMgr = locator.GetInstance&lt;IConfigManager&gt;();
<br />
<br />    var webAppPropBag = configMgr.GetPropertyBag(ConfigLevel.CurrentSPWebApplication);
<br />    var setting = 1;
<br />    configMgr.SetInPropertyBag(settingName, setting, webAppPropBag);
<br />    LiteralResults.Text = "saved setting: " + settingName + " Value: '" + setting + "'";
<br />}
<br />
<br /></span><span style="font-size: x-small;">protected void OnReadWebAppSettings(object sender, EventArgs e)
<br />{
<br />    ReadWebAppSettings();
<br />}
<br />
<br />protected void ReadWebAppSettings()
<br />{
<br />    var locator = SharePointServiceLocator.GetCurrent();
<br />    var configMgr = locator.GetInstance&lt;IConfigManager&gt;();
<br />
<br />    var webAppPropBag = configMgr.GetPropertyBag(ConfigLevel.CurrentSPWebApplication);
<br />    var setting = 1;
<br />    setting = configMgr.GetFromPropertyBag&lt;int&gt;(settingName, webAppPropBag);
<br />    LiteralResults.Text = "retrieved setting: " + settingName + " Value: '" + setting + "'";
<br />}</span></code></pre>
<p>When the read button is pressed, then a value of 0 is returned (in this case the value has not been defined, and 0&nbsp;is the default value for an integer, which is returned since the setting is not defined.&nbsp; For a complex object, a null would be returned instead).&nbsp; When the write button is pressed, then a security exception is thrown:</p>
<pre class="scroll"><code class="cplusplus"><span style="font-size: x-small;">System.Security.SecurityException was caught
<br />  Message=Access denied.
<br />  Source=Microsoft.SharePoint
<br />  StackTrace:
<br />       at Microsoft.SharePoint.Administration.SPPersistedObject.BaseUpdate()
<br />       at Microsoft.Practices.SharePoint.Common.Configuration.SPWebAppPropertyBag.set_Item(String key, String value)
<br />       at Microsoft.Practices.SharePoint.Common.Configuration.ConfigManager.SetInPropertyBag(String key, Object value, IPropertyBag propertyBag)
<br />  InnerException: </span></code></pre>
<p>You can see that when the update is attempted, a security exception gets thrown.&nbsp; This exception will be re-thrown by the ConfigManager as a ConfigurationException.&nbsp; </p>
<h2>&nbsp;Option 1: Add/Update the values in a feature receiver</h2>
<p>One way to address this is to update or set any values in a feature receiver.&nbsp; If you deploy this as a site or web scoped feature, then there is some special handling involved.&nbsp; A site or web scoped feature is activated from a content web site.&nbsp; Therefore if you put the logic into the&nbsp;<strong>FeatureActivated</strong> event, you will again get a security exception.&nbsp; You can put this logic instead into the <strong>FeatureInstalled</strong> event.&nbsp; <strong>FeatureInstalled</strong> gets fired when the feature is installed, which typically occurs through a higher privileged account like the command line, or a timer service.&nbsp; However <strong>FeatureInstalled</strong> has another issue you'll need to contend with.&nbsp; There is no site or web context passed into the <strong>FeatureInstalled </strong>event (since at installation it's not being associated to a web or site - that happens when the feature is activated).&nbsp; As a result you will need to generate the site context yourself as in the following example:</p>
<pre class="scroll"><code class="cplusplus"><span style="font-size: x-small;">public override void FeatureInstalled(SPFeatureReceiverProperties properties)
<br />{
<br />    var locator = SharePointServiceLocator.GetCurrent();
<br />    var configMgr = locator.GetInstance&lt;IConfigManager&gt;();
<br />
<br />    using (SPSite site = new SPSite("https://localhost/sites/test"))
<br />    {
<br />        configMgr.SetWeb(site.RootWeb);
<br />        var webAppPropBag = configMgr.GetPropertyBag(ConfigLevel.CurrentSPWebApplication);
<br />        var setting = 1;
<br />        configMgr.SetInPropertyBag(settingName, setting, webAppPropBag);
<br />    }
<br />}</span></code></pre>
<p>In general though its best to deploy a setting at the same level it is stored if you are deploying with a feature.&nbsp; This way you can be assured that the feature is only installed/activated once at the appropriate scope, and don't need to worry about conflicts or reference counting (if for example you figured out a way to set the web application setting in a site FeatureActivated event, then how do you decide when to remove it since the feature may have been activated muliple times within the web application?).&nbsp; If you deploy this instead as a web application feature, then you can write and remove the setting in the FeatureActivated/FeatureDeactivating code since a web app feature can't be activated from a content web app.&nbsp; The below example shows a FeatureActivated and FeatureDeactivating code for a web application scoped feature for adding settings to the web application.&nbsp; Remember that web application scoped features are automatically activated and deactivated by default when a feature is installed/uninstalled.&nbsp; This behavior is controlled by the attribute <em>ActivateOnDefault </em>on the Feature element.</p>
<pre class="scroll"><code class="cplusplus"><span style="font-size: x-small;">public override void FeatureActivated(SPFeatureReceiverProperties properties)
<br />{
<br />    var locator = SharePointServiceLocator.GetCurrent();
<br />    var configMgr = locator.GetInstance&lt;IConfigManager&gt;();
<br />    SPWebApplication webapp = properties.Feature.Parent as SPWebApplication;
<br />    IPropertyBag bag = new SPWebAppPropertyBag(webapp);
<br />
<br />    configMgr.SetInPropertyBag(settingName, 2, bag);
<br />}
<br />
<br />
<br />
<br /></span><span style="font-size: x-small;">public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
<br />{
<br />    var locator = SharePointServiceLocator.GetCurrent();
<br />    var configMgr = locator.GetInstance&lt;IConfigManager&gt;();
<br />    SPWebApplication webapp = properties.Feature.Parent as SPWebApplication; 
<br />    IPropertyBag bag = new SPWebAppPropertyBag(webapp);
<br />
<br />    configMgr.RemoveKeyFromPropertyBag(settingName, bag);
<br />}</span></code></pre>
<h2>&nbsp;Option 2: Add a configuration page to central admin</h2>
<p>&nbsp;Central admin runs in a different application pool from the content web apps.&nbsp; From central admin you have permissions to change any farm or web app level setting.&nbsp; A how-to provide in the guidence describes the task of adding a custom page to central admin called <a target="_blank" href="https://msdn.microsoft.com/en-us/library/ff798467.aspx" title="How to: Deploy an Application Page to Central Administration">How to: Deploy an Application Page to Central Administration</a>.&nbsp; In my next post I will walk through a simple example page for editing web application settings in central admin.</p>]]></content:encoded>
    </item>
  </channel>
</rss>