csharp CRM 2015-2016 C ##Plugins #Metadata#CRM2016 #MoussaElAnnan

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp CRM 2015-2016 C ##Plugins #Metadata#CRM2016 #MoussaElAnnan相关的知识,希望对你有一定的参考价值。

enum CrmPluginStepDeployment
{
   ServerOnly = 0,
   OfflineOnly = 1,
   Both = 2
}
 
enum CrmPluginStepMode
{
   Asynchronous = 1,
   Synchronous = 0
}
 
enum CrmPluginStepStage
{
   PreValidation = 10,
   PreOperation = 20,
   PostOperation = 40,
}
 
enum SdkMessageName
{
   Create,
   Update,
   Delete,
   Retrieve,
   Assign,
   GrantAccess,
   ModifyAccess,
   RetrieveMultiple,
   RetrievePrincipalAccess,
   RetrieveSharedPrincipalsAndAccess,
   RevokeAccess,
   SetState,
   SetStateDynamicEntity,
}

var AssemblyName = "MyCompany.MyProject.Plugins";
var PluginTypeName = "MyCompany.MyProject.Plugins.AssignSequenceNumber";
var CrmPluginStepStage = CrmPluginStepStage.PreOperation;
var EntityName = "account";
var StepName = "Assign sequence number to newly create accounts";
var Rank = 1;
var SdkMessageName = SdkMessageName.Create;

SdkMessageProcessingStep step = newSdkMessageProcessingStep
{
   AsyncAutoDelete = false,
   Mode = newOptionSetValue((int)CrmPluginStepMode.Synchronous),
   Name = StepName,
   EventHandler = newEntityReference("plugintype", GetPluginTypeId()),
   Rank = Rank,
   SdkMessageId = newEntityReference("sdkmessage", GetMessageId()),
   Stage = newOptionSetValue((int)CrmPluginStepStage),
   SupportedDeployment = newOptionSetValue((int)CrmPluginStepDeployment.ServerOnly),
   SdkMessageFilterId = newEntityReference("sdkmessagefilter", GetSdkMessageFilterId())
};
OrganizationService.Create(step);

private Guid GetSdkMessageFilterId()
{
   using (CrmServiceContext context = newCrmServiceContext(OrganizationService))
   {
      var sdkMessageFilters = from s in context.SdkMessageFilterSet
      where s.PrimaryObjectTypeCode == EntityName
      where s.SdkMessageId.Id == GetMessageId()
      select s;
      return sdkMessageFilters.First().Id;
   }
}
 
private Guid GetMessageId()
{
   using (CrmServiceContext context = newCrmServiceContext(OrganizationService))
   {
      var sdkMessages = from s in context.SdkMessageSet
      where s.Name == SdkMessageName.ToString()
      select s;
      return sdkMessages.First().Id;
   }
}
 
private Guid GetPluginTypeId()
{
   using (CrmServiceContext context = newCrmServiceContext(OrganizationService))
   {
      var pluginAssemblies = from p in context.PluginAssemblySet
      where p.Name == AssemblyName
      select p;
      Guid assemblyId = pluginAssemblies.First().Id;
      var pluginTypes = from p in context.PluginTypeSet
      where p.PluginAssemblyId.Id == assemblyId
      where p.TypeName == PluginTypeName
      select p;
      return pluginTypes.First().Id;
   }
}
//Associate, Deassociate
public class ClassName: IPlugin {
 public void Execute(IServiceProvider serviceProvider) {
  var tracingService = (ITracingService) serviceProvider.GetService(typeof(ITracingService));

  var pluginExecutionContext = (IPluginExecutionContext) serviceProvider.GetService(typeof(IPluginExecutionContext));
  var orgService = CrmConnectionInstance.CreateOrganizationServiceInstance(serviceProvider, pluginExecutionContext);

  if (pluginExecutionContext.PrimaryEntityName != "entityname") return;
  try {
   if (pluginExecutionContext.MessageName == "Associate") {
    if (!pluginExecutionContext.InputParameters.Contains("RelationShip")) {
     return;
    }
    var relationship = (Relationship) pluginExecutionContext.InputParameters["Relationship"];
    if (relationship.SchemaName != "relationschemaname") {
     return;
    }

    if (!pluginExecutionContext.InputParameters.Contains("Target")) {
     return;
    }
    EntityReference target = (EntityReference) pluginExecutionContext.InputParameters["Target"];
    if (!pluginExecutionContext.InputParameters.Contains("RelatedEntities")) {
     return;
    }
    EntityReferenceCollection related = (EntityReferenceCollection) pluginExecutionContext.InputParameters["RelatedEntities"];
    throw new InvalidPluginExecutionException("looo");
   }
  } catch (FaultException < OrganizationServiceFault > ex) {
   tracingService.Trace(String.Format("Exception: {0}", ExceptionsHelper.GenerateExceptionMessage(ex, typeof(FaultException < OrganizationServiceFault > ))));
   throw new InvalidPluginExecutionException(ex.Message);
  }
 }
}
}
     public static string GetBoolText(IOrganizationService service, string entitySchemaName, string attributeSchemaName)
    {
        var retrieveAttributeRequest = new RetrieveAttributeRequest
        {
            EntityLogicalName = entitySchemaName,
            LogicalName = attributeSchemaName,
            RetrieveAsIfPublished = true
        };
        var retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest);
        var retrievedBooleanAttributeMetadata = (BooleanAttributeMetadata)retrieveAttributeResponse.AttributeMetadata;
        var boolText = retrievedBooleanAttributeMetadata.OptionSet.DisplayName.LocalizedLabels[0].Label;
        return boolText;
    }
public static object GetAttributeValue<T>(Entity entity, string attributeName)
{
    if (entity.Attributes.Contains(attributeName))
    {
        return entity.GetAttributeValue<T>(attributeName);
    }
    return null;
}
/* USAGE
*  var dateTimeField =  GetAttributeValue<DateTime>(entity, key, attributeName);
*/
      
/// <summary>
/// Method that get an attribute by it's value and return the value.
/// </summary>
/// <param name="entity">The entity.</param>
/// <param name="key">The attribute key.</param>
/// <param name="attributeName">Teh attribute name</param>
/// <returns>The string value.</returns>
private static T GetAttributeValue<T>(Entity entity, object key, string attributeName)
{
    if (key is OptionSetValue)
    {
        return entity.GetAttributeValue<T>(attributeName);
    }
    else if (key is string)
    {
        return entity.GetAttributeValue<T>(attributeName);
    }
    else if (key is Guid)
    {
        return entity.GetAttributeValue<T>(attributeName);
    }
    else if (key is int)
    {
        return entity.GetAttributeValue<T>(attributeName);
    }
    else if (key is bool)
    {
        return entity.GetAttributeValue<T>(attributeName);
    }
    return default(T);
}

以上是关于csharp CRM 2015-2016 C ##Plugins #Metadata#CRM2016 #MoussaElAnnan的主要内容,如果未能解决你的问题,请参考以下文章

csharp CRM 2016 #Emails#C#CRM2016 #MoussaElAnnan

csharp CRM 2016 C ## C ##OptionSets #MoussaElAnnan

Dynamics CRM 2015/2016 Web API:新的数据查询方式

Dynamics CRM 2015/2016 Web API:注册 APP(调用CRM Online Web API)

Dynamics CRM 2015/2016 Web API:聚合查询

csharp CRM 2016 #Workflows#CRM2016 #MoussaElAnnan