Да, Microsoft настоятельно рекомендует использовать в плагинах исключительно Dynamic Entity. Но...
Короче, я сделал так:
Разбил плагин на два класса.
В одном - референсы на Sdk, в другом - на CrmService.
LeadAutoNumeration.cs
Код:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
namespace My.LeadAutoNumeration
{
public class LeadAutoNumeration : IPlugin
{
public void Execute(IPluginExecutionContext context)
{
string contextOrgName = context.OrganizationName;
Guid leadId = (Guid)context.OutputParameters[ParameterName.Id];
AutoNumeration.SetLeadNumber(leadId, contextOrgName);
}
}
}
Num.cs
Код:
using System;
using System.Collections.Generic;
using System.Text;
using LeadAutoNumeration.CrmData;
namespace My.LeadAutoNumeration
{
class AutoNumeration
{
public static void SetLeadNumber(Guid leadId, string orgName)
{
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = orgName;
CrmService serv = new CrmService();
serv.Url = (string)Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("Microsoft").OpenSubKey("MSCRM").GetValue("ServerUrl") + "/2007/CrmService.asmx";
serv.CrmAuthenticationTokenValue = token;
serv.Credentials = System.Net.CredentialCache.DefaultCredentials;
int leadNumber = 1;
// загружаем системные параметры
QueryExpression qeParams = new QueryExpression();
qeParams.ColumnSet = new AllColumns();
qeParams.EntityName = EntityName.new_customparams.ToString();
BusinessEntityCollection customParams = serv.RetrieveMultiple(qeParams);
if ((customParams.BusinessEntities != null) && (customParams.BusinessEntities.Length > 0))
{
new_customparams currentParams = (new_customparams)customParams.BusinessEntities[0];
DateTime leadlastdate = DateTime.Parse(currentParams.new_leadlastdate.Value);
if (leadlastdate.ToString("d") == DateTime.Today.ToString("d"))
{
leadNumber = currentParams.new_leadcounter.Value + 1;
}
else
{
currentParams.new_leadlastdate = new CrmDateTime();
currentParams.new_leadlastdate.Value = DateTime.Today.ToString("s");
}
currentParams.new_leadcounter.Value = leadNumber;
serv.Update(currentParams);
}
// присваиваем номер интересу
lead currentLead = (lead)serv.Retrieve(EntityName.lead.ToString(), leadId, new AllColumns());
currentLead.new_number = "L-" + DateTime.Now.ToString("yyyyMMdd") + "-" + leadNumber.ToString("000");
serv.Update(currentLead);
}
}
}