Спасибо. Сам не понимаю, почему он может не подхватывать.
Ниже привожу код плагина
X++:
using System;
using System.ServiceModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Sdk.Query;
using System.Collections;
using Microsoft.Xrm.Sdk.Client;
using System.Data;
namespace WorkWithProject
{
public class Plugin : IPlugin
{
public OrganizationServiceContext orgContext;
public OrganizationServiceProxy _serviceProxy;
public IOrganizationService _service;
public int Nomer = 0;
public string ProjectPrefix = "";
public string StrNomer = "";
public string EntityPrefix = "";
public void Init()
{
_serviceProxy.EnableProxyTypes();
_service = (IOrganizationService)_serviceProxy;
// Create the OrganizationServiceContext object that will generate the IQueryable collections for LINQ calls.
orgContext = new OrganizationServiceContext(_service);
}
public string WorkWithNumber(int Number, string EntityPrefix, string PrefixProekta)
{
string result = null;
result = String.Format("{1}{2}-{0,5:00000}", Number, EntityPrefix, PrefixProekta);
return result;
}
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
_service = serviceFactory.CreateOrganizationService(context.UserId);
orgContext = new OrganizationServiceContext(_service);
ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
if (context.Depth > 1)
{
return;
}
Entity entity = null;
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
// Obtain the target business entity from the input parmameters.
entity = (Entity)context.InputParameters["Target"];
// TODO Test for an entity type and message supported by your plug-in.
}
else return;
if (entity != null)
{
try
{
Guid EntityId = ((Guid)entity.Id);
if (entity.LogicalName == "project_task")
{
#region
//вынимаем задачу проекта-------------------------------------------------------------
var ProjectTask = (from PT in orgContext.CreateQuery<project_task>()
where (PT.Id == EntityId)
select new project_task{ Id = PT.Id }).FirstOrDefault();
if (ProjectTask == null) return;
//------------------------------------------------------------------------------------
if (ProjectTask.project== null) return;
else
{
//вытягиваем проект-------------------------------------------------------
var Project = (from P in orgContext.CreateQuery<project>()
where (P.Id == ProjectTask.project.Id)
select new project{ Id = P.Id }).FirstOrDefault();
if (Project == null) return;
//-----------------------------------------------------------------------
if (Project.number== null) Nomer = 1;
else Nomer = (int)Project.task_number;
EntityPrefix = "T";
if (Project.prefix != null) ProjectPrefix = Project.prefix;
StrNomer = WorkWithNumber(Nomer, EntityPrefix, ProjectPrefix);
ProjectTask.number = StrNomer;
orgContext.UpdateObject(ProjectTask);
_service.Update(ProjectTask);
Project.number = Nomer + 1;
orgContext.UpdateObject(Project);
_service.Update(Project);
//orgContext.SaveChanges();
}
}
else if (entity.LogicalName == "out_task")
{
#region
//вынимаем задачу проекта-------------------------------------------------------------
var ProjectTaskOut = (from PTO in orgContext.CreateQuery<out_task>()
where (PTO.Id == EntityId)
select new out_task{ Id = PTO.Id }).FirstOrDefault();
if (ProjectTaskOut == null) return;
//------------------------------------------------------------------------------------
if (ProjectTaskOut.project== null) return;
else
{
//вытягиваем проект-------------------------------------------------------
var Project = (from P in orgContext.CreateQuery<project>()
where (P.Id == ProjectTaskOut.project.Id)
select new project{ Id = P.Id }).FirstOrDefault();
if (Project == null) return;
//-----------------------------------------------------------------------
if (Project.out_task_number == null) Nomer = 1;
else Nomer = (int)Project.out_task_number;
EntityPrefix = "E";
if (Project.prefix != null) ProjectPrefix = Project.prefix;
StrNomer = WorkWithNumber(Nomer, EntityPrefix, ProjectPrefix);
ProjectTaskOut.number = StrNomer;
orgContext.UpdateObject(ProjectTaskOut);
_service.Update(ProjectTaskOut);
Project.out_task_number = Nomer + 1;
orgContext.UpdateObject(Project);
_service.Update(Project);
}
#endregion
}
else if (entity.LogicalName == "defect")
{
#region
//вынимаем задачу проекта-------------------------------------------------------------
var Defect = (from D in orgContext.CreateQuery<defect>()
where (D.Id == EntityId)
select new defect{ Id = D.Id }).FirstOrDefault();
if (Defect == null) return;
//------------------------------------------------------------------------------------
if (Defect.project== null) return;
else
{
//вытягиваем проект-------------------------------------------------------
var Project = (from P in orgContext.CreateQuery<project>()
where (P.Id == Defect.project.Id)
select new project{ Id = P.Id }).FirstOrDefault();
if (Project == null) return;
//-----------------------------------------------------------------------
if (Project.new_nomer_defecta == null) Nomer = 1;
else Nomer = (int)Project.new_nomer_defecta;
EntityPrefix = "D";
if (Project.prefix!= null) ProjectPrefix = Project.new_prefix_numeracii;
StrNomer = WorkWithNumber(Nomer, EntityPrefix, ProjectPrefix);
Defect.number= StrNomer;
orgContext.UpdateObject(Defect);
_service.Update(Defect);
Project.number= Nomer + 1;
orgContext.UpdateObject(Project);
_service.Update(Project);
}
#endregion
}
else if (entity.LogicalName == "changes")
{
#region
//вынимаем задачу проекта-------------------------------------------------------------
var Izm = (from I in orgContext.CreateQuery<changes>()
where (I.Id == EntityId)
select new changes{ Id = I.Id }).FirstOrDefault();
if (Izm == null) return;
//------------------------------------------------------------------------------------
if (Izm.new_proekt == null) return;
else
{
//вытягиваем проект-------------------------------------------------------
var Project = (from P in orgContext.CreateQuery<project>()
where (P.Id == Izm.project.Id)
select new project{ Id = P.Id }).FirstOrDefault();
if (Project == null) return;
//-----------------------------------------------------------------------
if (Project.changes== null) Nomer = 1;
else Nomer = (int)Project.number;
EntityPrefix = "C";
if (Project.prefix != null) ProjectPrefix = Project.prefix;
StrNomer = WorkWithNumber(Nomer, EntityPrefix, ProjectPrefix);
Izm.new_nomer = StrNomer;
orgContext.UpdateObject(Izm);
_service.Update(Izm);
Project.new_nomer_izmeneniya = Nomer + 1;
orgContext.UpdateObject(Project);
_service.Update(Project);
}
#endregion
}
else if (entity.LogicalName == "risk")
{
#region
//вынимаем задачу проекта-------------------------------------------------------------
var Risk = (from R in orgContext.CreateQuery<risk>()
where (R.Id == EntityId)
select new risk{ Id = R.Id }).FirstOrDefault();
if (Risk == null) return;
//------------------------------------------------------------------------------------
if (Risk.project== null) return;
else
{
//вытягиваем проект-------------------------------------------------------
var Project = (from P in orgContext.CreateQuery<project>()
where (P.Id == Risk.new_proektid.Id)
select new project{ Id = P.Id }).FirstOrDefault();
if (Project == null) return;
//-----------------------------------------------------------------------
if (Project.number== null) Nomer = 1;
else Nomer = (int)Project.number;
EntityPrefix = "R";
if (Project.prefix!= null) ProjectPrefix = Project.prefix;
StrNomer = WorkWithNumber(Nomer, EntityPrefix, ProjectPrefix);
Risk.number= StrNomer;
orgContext.UpdateObject(Risk);
_service.Update(Risk);
Project.number= Nomer + 1;
orgContext.UpdateObject(Project);
_service.Update(Project);
}
#endregion
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException(ex.Message,ex);
}
}
}
}
}