title: How do I get the current year in c#?
body: <p>I have a plugin for auto numbering in my CRM instance. Basically, the prefix is the current year. So I want it to work like this, if the year is not current, update it with the current year.</p>
  
  <p><code>new_prefix</code> is the field that has the year in it that I need to change if different than current year. The plug-in currently works I just wanna change the year if it's different. Also this keeps telling me it's too much code and too little details.</p>
  
  <pre class="lang-csh prettyprint-override"><code>using Microsoft.Xrm.Sdk;
  using Microsoft.Xrm.Sdk.Client;
  using System.Xml.Linq;
  using System;
  using System.Linq;
  using System.Globalization;
  
  namespace UGKCK.CRM.PlugIns
  {
      public class AutoNumbering : IPlugin
      {
          #region Secure/Unsecure Configuration Setup
          private string _secureConfig = null;
          private string _unsecureConfig = null;
  
          public AutoNumbering(string unsecureConfig, string secureConfig)
          {
              _secureConfig = secureConfig;
              _unsecureConfig = unsecureConfig;
          }
          #endregion
          public void Execute(IServiceProvider serviceProvider)
          {
              ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
              IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
              IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
              IOrganizationService service = factory.CreateOrganizationService(context.UserId);
              OrganizationServiceContext orgService = new OrganizationServiceContext(service);
  
              try
              {
                  Entity entity = (Entity)context.InputParameters["Target"];
  
                  Entity autoNumberingRecord = RetrieveAutoNumbering(orgService, entity);
                  UpdateNumber(service, autoNumberingRecord, entity);
                  UpdateNextNumber(service, autoNumberingRecord);
              }
              catch (Exception e)
              {
                  throw new InvalidPluginExecutionException(e.Message);
              }
          }
  
          private Entity RetrieveAutoNumbering(OrganizationServiceContext orgService, Entity entity)
          {
              Entity retrievedAutoNumber = (from an in orgService.CreateQuery("new_autonumbering")
                                            where (String)an["new_entity"] == entity.LogicalName
                                            select an).FirstOrDefault();
  
              Entity autoNumber = new Entity("new_autonumbering");
              autoNumber.Id = retrievedAutoNumber.Id;
              autoNumber["new_prefix"] = retrievedAutoNumber["new_prefix"];
              autoNumber["new_nextnumber"] = retrievedAutoNumber["new_nextnumber"];
  
              return autoNumber;
          }
  
          private void UpdateNumber(IOrganizationService service, Entity autoNumberingRecord, Entity target)
          {
              var prefix = autoNumberingRecord.Attributes["new_prefix"].ToString();
              var number = autoNumberingRecord.Attributes["new_nextnumber"].ToString();
  
              if (target.LogicalName == "new_servicerequest")
              {
  
                  target["new_servicerequestnumber"] = prefix + "-" + number;
              }
              else if (target.LogicalName == "contact")
              {
  
                  target["new_contactnumber"] = prefix + "-" + number;
              }
              service.Update(target);
          }
  
          private void UpdateNextNumber(IOrganizationService service, Entity autoNumberingRecord)
          {
              var currentNumber = (int)autoNumberingRecord.Attributes["new_nextnumber"];
              var nextNumber = currentNumber + 1;
  
              autoNumberingRecord["new_nextnumber"] = nextNumber;
              service.Update(autoNumberingRecord);
          }
      }
  
  }
  </code></pre>