//Getting Service as happens in PLUGIN
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
//Configuring xaml to create workflow
string xamlWfPrefix = @"<?xml version=""1.0"" encoding=""utf-16""?>
<Activity x:Class=""XrmWorkflow""
xmlns=""http://schemas.microsoft.com/netfx/2009/xaml/activities""
xmlns:mva=""clr-namespace:Microsoft.VisualBasic.Activities;assembly=System.Activities, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35""
xmlns:mxs=""clr-namespace:Microsoft.Xrm.Sdk;assembly=Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35""
xmlns:mxswa=""clr-namespace:Microsoft.Xrm.Sdk.Workflow.Activities;assembly=Microsoft.Xrm.Sdk.Workflow, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35""
xmlns:s=""clr-namespace:System;assembly=mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089""
xmlns:scg=""clr-namespace:System.Collections.Generic;assembly=mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089""
xmlns:srs=""clr-namespace:System.Runtime.Serialization;assembly=System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089""
xmlns:this=""clr-namespace:"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
<x:Members>
<x:Property Name=""InputEntities"" Type=""InArgument(scg:IDictionary(x:String, mxs:Entity))"" />
<x:Property Name=""CreatedEntities"" Type=""InArgument(scg:IDictionary(x:String, mxs:Entity))"" />
</x:Members>
<this:XrmWorkflow.InputEntities>
<InArgument x:TypeArguments=""scg:IDictionary(x:String, mxs:Entity)"" />
</this:XrmWorkflow.InputEntities>
<this:XrmWorkflow.CreatedEntities>
<InArgument x:TypeArguments=""scg:IDictionary(x:String, mxs:Entity)"" />
</this:XrmWorkflow.CreatedEntities>
<mva:VisualBasic.Settings>Assembly references and imported namespaces for internal implementation</mva:VisualBasic.Settings>
<mxswa:Workflow>
<Sequence DisplayName=""CreateStep1"">
<Sequence.Variables>
<Variable x:TypeArguments=""x:Object"" Name=""CreateStep1_1"" />
<Variable x:TypeArguments=""x:Object"" Name=""CreateStep1_2"" />
</Sequence.Variables>
<Assign x:TypeArguments=""mxs:Entity"" To=""[CreatedEntities("CreateStep1_localParameter#Temp")]"" Value=""[New Entity("new_crm_new_sfeed")]"" />
<mxswa:ActivityReference AssemblyQualifiedName=""Microsoft.Crm.Workflow.Activities.EvaluateExpression, Microsoft.Crm.Workflow, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"" DisplayName=""EvaluateExpression"">
<mxswa:ActivityReference.Arguments>
<InArgument x:TypeArguments=""x:String"" x:Key=""ExpressionOperator"">CreateCrmType</InArgument>
<InArgument x:TypeArguments=""s:Object[]"" x:Key=""Parameters"">[New Object() { Microsoft.Xrm.Sdk.Workflow.WorkflowPropertyType.String, """;
xamlWfPrefix += displayname + @""", ""String"" }]</InArgument>
<InArgument x:TypeArguments=""s:Type"" x:Key=""TargetType"">
<mxswa:ReferenceLiteral x:TypeArguments=""s:Type"" Value=""x:String"" />
</InArgument>
<OutArgument x:TypeArguments=""x:Object"" x:Key=""Result"">[CreateStep1_1]</OutArgument>
</mxswa:ActivityReference.Arguments>
</mxswa:ActivityReference>
<mxswa:SetEntityProperty Attribute=""new_name"" Entity=""[CreatedEntities("CreateStep1_localParameter#Temp")]"" EntityName=""new_crm_new_sfeed"" Value=""[CreateStep1_1]"">
<mxswa:SetEntityProperty.TargetType>
<InArgument x:TypeArguments=""s:Type"">
<mxswa:ReferenceLiteral x:TypeArguments=""s:Type"" Value=""x:String"" />
</InArgument>
</mxswa:SetEntityProperty.TargetType>
</mxswa:SetEntityProperty>
<mxswa:ActivityReference AssemblyQualifiedName=""Microsoft.Crm.Workflow.Activities.EvaluateExpression, Microsoft.Crm.Workflow, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"" DisplayName=""EvaluateExpression"">
<mxswa:ActivityReference.Arguments>
<InArgument x:TypeArguments=""x:String"" x:Key=""ExpressionOperator"">CreateCrmType</InArgument>
<InArgument x:TypeArguments=""s:Object[]"" x:Key=""Parameters"">[New Object() { Microsoft.Xrm.Sdk.Workflow.WorkflowPropertyType.String, """;
xamlWfPrefix += displayname;
string xamlWfSuffix = @", ""String"" }]</InArgument>
<InArgument x:TypeArguments=""s:Type"" x:Key=""TargetType"">
<mxswa:ReferenceLiteral x:TypeArguments=""s:Type"" Value=""x:String"" />
</InArgument>
<OutArgument x:TypeArguments=""x:Object"" x:Key=""Result"">[CreateStep1_2]</OutArgument>
</mxswa:ActivityReference.Arguments>
</mxswa:ActivityReference>
<mxswa:SetEntityProperty Attribute=""new_description"" Entity=""[CreatedEntities("CreateStep1_localParameter#Temp")]"" EntityName=""new_crm_new_sfeed"" Value=""[CreateStep1_2]"">
<mxswa:SetEntityProperty.TargetType>
<InArgument x:TypeArguments=""s:Type"">
<mxswa:ReferenceLiteral x:TypeArguments=""s:Type"" Value=""x:String"" />
</InArgument>
</mxswa:SetEntityProperty.TargetType>
</mxswa:SetEntityProperty>
<mxswa:CreateEntity EntityId=""{x:Null}"" DisplayName=""CreateStep1"" Entity=""[CreatedEntities("CreateStep1_localParameter#Temp")]"" EntityName=""new_crm_new_sfeed"" />
<Assign x:TypeArguments=""mxs:Entity"" To=""[CreatedEntities("CreateStep1_localParameter")]"" Value=""[CreatedEntities("CreateStep1_localParameter#Temp")]"" />
<Persist />
</Sequence>
</mxswa:Workflow>
</Activity>";
//Calling method to create total xaml code that requires to create workflow
string xamlWf = BuildWorkflowXaml(xamlWfPrefix, xamlWfSuffix, recordCreated);
//Initiating workflow to be create
Workflow wfOnCreate = new Workflow()
{
Name = "CRM_DNNWorkflow_OnCreate",
Type = new OptionSetValue(1),
Category = new OptionSetValue(0),
Scope = new OptionSetValue(2),
LanguageCode = 1033, // U.S. English
TriggerOnCreate = true,
OnDemand = false,
PrimaryEntity = logicalname, //Name of entity on which WF will be fired
Description = @"Tracks Creation",
Xaml = xamlWf
};
xamlWf = BuildWorkflowXaml(xamlWfPrefix, xamlWfSuffix, recordDeleted);
//Another WF
Workflow wfOnDelete = new Workflow()
{
Name = "CRM_DNNWorkflow_OnDelete",
Type = new OptionSetValue(1),
Category = new OptionSetValue(0),
Scope = new OptionSetValue(2),
LanguageCode = 1033, // U.S. English
TriggerOnDelete = true,
OnDemand = false,
PrimaryEntity = logicalname,
Description = @"Tracks Deletion",
Xaml = xamlWf
};
xamlWf = BuildWorkflowXaml(xamlWfPrefix, xamlWfSuffix, recordModified);
//Another WF
Workflow wfOnModify = new Workflow()
{
Name = "CRM_DNNWorkflow_OnModify",
Type = new OptionSetValue(1),
Category = new OptionSetValue(0),
Scope = new OptionSetValue(2),
LanguageCode = 1033, // U.S. English
TriggerOnUpdateAttributeList = attributeList,
OnDemand = false,
PrimaryEntity = logicalname,
Description = @"Tracks Modification",
Xaml = xamlWf
};
workflowOnCreateId = service.Create(wfOnCreate);
workflowOnDeleteId = service.Create(wfOnDelete);
workflowOnModifyId = service.Create(wfOnModify);
//activate workflow (OnCreate)
ActivateWorkflow(workflowOnCreateId, service, serviceProvider);
//activate workflow (OnDelete)
ActivateWorkflow(workflowOnDeleteId, service, serviceProvider);
//activate workflow (OnModify)
ActivateWorkflow(workflowOnModifyId, service, serviceProvider);
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
tracingService.Trace("Exception: {0}", ex.ToString());
throw;
//throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}
}
/// <summary>
/// Builds the Xaml to define the function of a Workflow.
/// </summary>
/// <param name="prefix">Prefix to Xaml code</param>
/// <param name="suffix">Suffix to Xaml code</param>
/// <param name="status">Sets the description field of the new_crm_new_sfeed Entity</param>
/// <returns></returns>
private string BuildWorkflowXaml(string prefix,string suffix,string status)
{
return prefix + status + suffix;
}
/// <summary>
/// Activates a Workflow.
/// </summary>
/// <param name="workflowId">Guid of the workflow to be created.</param>
/// <param name="service">IOrganizationService Object.</param>
private void ActivateWorkflow(Guid workflowId,IOrganizationService service,IServiceProvider serviceProvider)
{
//Extract the tracing service for use in plug-in debugging.
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
try
{
SetStateRequest activateRequest = new SetStateRequest
{
EntityMoniker = new EntityReference(Workflow.EntityLogicalName, workflowId),
State = new OptionSetValue((int)WorkflowState.Activated),
Status = new OptionSetValue(2)
};
//**Error Occurs Here**
OrganizationResponse respActivation = service.Execute(activateRequest);
tracingService.Trace("ActivateResponse", respActivation);
workflowGUIDs.Add(workflowId);
}
catch (InvalidPluginExecutionException exe)
{
tracingService.Trace("Exception: {0}", exe.ToString());
throw;
}
}
**FROM MY C# APPLICATION**
// Form the RetrieveOrganization Request.
RetrieveOrganizationRequest request = new RetrieveOrganizationRequest();
request.UniqueName = organizationUniqueName;
RetrieveOrganizationResponse response = (RetrieveOrganizationResponse)discoveryServiceProxy.Execute(request);
// Form the URL.
Uri uri = new Uri(response.Detail.Endpoints[EndpointType.OrganizationService]);
//Form the OrganizationServiceProxy Object.
orgServiceProxy = new OrganizationServiceProxy(uri, null, discoveryServiceProxy.ClientCredentials, discoveryServiceProxy.DeviceCredentials);
Entity Dnn_selectedEntity = new Entity("new_dnn_selected_entity");
Dnn_selectedEntity["new_name"] = entityMetadataId.Value.ToString();
Dnn_selectedEntity["new_trackedentity"] = entityMetadataId.Key;
orgServiceProxy.Create(Dnn_selectedEntity);
//After Creting this record That Plugin fires....
Thanks.