Hi Bas van de Sande
This is my whole code which I use in my custom work flow:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using Newtonsoft.Json;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using System.Activities;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;//RetrieveRequest naemspace
namespace Integration
{
public class CRMIntegration : CodeActivity
{
protected override void Execute(CodeActivityContext context)
{
ITracingService tracingService = context.GetExtension<ITracingService>();
IWorkflowContext workflowContext = context.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = context.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(workflowContext.UserId);
Guid Id = workflowContext.PrimaryEntityId;
// Create the request object and set the monikers with the
//Create the request
RetrieveRequest request = new RetrieveRequest();
request.ColumnSet = new ColumnSet(new string[] { aw__Attributes.name, aw__Attributes.FromName, aw__Attributes.Subject, aw__Attributes.Fromemail });
request.Target = new EntityReference(EntityName.aw_, Id);
Entity entity = (Entity)((RetrieveResponse)service.Execute(request)).Entity;
EntityCollection ec = null;
var userid = workflowContext.UserId.ToString();
ec = GetEntityCollection(service, EntityName.aw__User, aw__User_Attributes.User, userid, new ColumnSet(aw__User_Attributes.Address, aw__User_Attributes.BusinessUnitId, aw__User_Attributes.City, aw__User_Attributes.PostalCode, aw__User_Attributes.State, aw__User_Attributes.Country));
if (entity.Contains(aw__Attributes.name) && entity.Contains(aw__Attributes.FromName) && entity.Contains(aw__Attributes.Subject) && entity.Contains(aw__Attributes.Fromemail) && ec.Entities[0].Contains(aw__User_Attributes.Address) && ec.Entities[0].Contains(aw__User_Attributes.BusinessUnitId) && ec.Entities[0].Contains(aw__User_Attributes.City) && ec.Entities[0].Contains(aw__User_Attributes.Country) && ec.Entities[0].Contains(aw__User_Attributes.PostalCode) && ec.Entities[0].Contains(aw__User_Attributes.State))
{
//</Creating error log for list entity>
string ListName = string.Empty;
ListName = entity.Attributes[aw__Attributes.name].ToString();
//</Creating error log for list entity>
var sampleList = JsonConvert.SerializeObject(
new
{
name = ListName,
contact = new
{
company = ec.Entities[0].Attributes[aw__User_Attributes.BusinessUnitId],
address1 = ec.Entities[0].Attributes[aw__User_Attributes.Address],
city = ec.Entities[0].Attributes[aw__User_Attributes.City],
state = ec.Entities[0].Attributes[aw__User_Attributes.State],
zip = ec.Entities[0].Attributes[aw__User_Attributes.PostalCode],
country = ec.Entities[0].Attributes[aw__User_Attributes.Country],
},
permission_reminder = "You'\''re receiving this email because you signed up for updates about Freddie'\''s newest hats.",
campaign_defaults = new
{
from_name = entity.Attributes[aw__Attributes.FromName],
from_email = entity.Attributes[aw__Attributes.Fromemail],
subject = entity.Attributes[aw__Attributes.Subject],
language = "en",
},
email_type_option = true
});
var uri = string.Format("https://{0}.api..com/3.0/lists", "u4");
try
{
using (var webClient = new WebClient())
{
webClient.Headers.Add("Accept", "application/json");
webClient.Headers.Add("Authorization", "apikey " + "ab15c4d1");
string Response = string.Empty;
Response = webClient.UploadString(uri, "POST", sampleList);
string listId = string.Empty;
listId = Response.Substring(7, 10);
Entity updateEntity = new Entity(EntityName.aw_);
updateEntity.Id = Id;
updateEntity[aw__Attributes.CreateListStatus] = "Sent";
updateEntity[aw__Attributes.ListId] = listId;
service.Update(updateEntity);
//<Creating error log for list entity>
Entity Entity = new Entity(EntityName.ListError);
Entity[ListError_Attributes.ResponseDetail] = Response;
Entity[ListError_Attributes.Response] = "successful";
Entity[ListError_Attributes.IntegrationType] = "Create List";
Entity[ListError_Attributes.DateTime] = DateTime.Now;
Entity[ListError_Attributes.IntegrationDescription] = ListName;
Guid ListErrorID = service.Create(Entity);
//</Creating error log for list entity>
AssociateRequest ListToListError = new AssociateRequest
{
//The contact record
Target = new EntityReference(updateEntity.LogicalName, Id),
//The list of account you would like the contact to be linked with (in this case we have only one account)
RelatedEntities = new EntityReferenceCollection
{
new EntityReference(Entity.LogicalName, ListErrorID)
},
Relationship = new Relationship("aw_listerror")
};
service.Execute(ListToListError);
}
}
catch (WebException we)
{
if (we.Response!=null)
{
using (var sr = new StreamReader(we.Response.GetResponseStream()))
{
Entity updateEntityA = new Entity(EntityName.aw_);
updateEntityA.Id = Id;
updateEntityA[aw__Attributes.CreateListStatus] = "Pending Sent";
service.Update(updateEntityA);
//<Creating error log for list entity>
Entity EntityB = new Entity(EntityName.ListError);
EntityB[ListError_Attributes.ResponseDetail] = sr.ReadToEnd();
EntityB[ListError_Attributes.Response] = "unsuccessful";
EntityB[ListError_Attributes.IntegrationType] = "Create List";
EntityB[ListError_Attributes.DateTime] = DateTime.Now;
EntityB[ListError_Attributes.IntegrationDescription] = ListName;
Guid ListErrorIDB = service.Create(EntityB);
AssociateRequest ListToListErrorr = new AssociateRequest
{
//The contact record
Target = new EntityReference(updateEntityA.LogicalName, Id),
//The list of account you would like the contact to be linked with (in this case we have only one account)
RelatedEntities = new EntityReferenceCollection
{
new EntityReference(EntityB.LogicalName, ListErrorIDB)
},
Relationship = new Relationship("aw_listerror")
};
service.Execute(ListToListErrorr);
//</Creating error log for list entity>
}
}
else
{
Entity updateEntity = new Entity(EntityName.aw_);
updateEntity.Id = Id;
updateEntity[aw__Attributes.CreateListStatus] = "Pending Sent";
service.Update(updateEntity);
//<Creating error log for list entity>
Entity Entity = new Entity(EntityName.ListError);
Entity[ListError_Attributes.ResponseDetail] = we.Response;
Entity[ListError_Attributes.Response] = "unsuccessful";
Entity[ListError_Attributes.IntegrationType] = "Create List";
Entity[ListError_Attributes.IntegrationDescription] = ListName;
Entity[ListError_Attributes.DateTime] = DateTime.Now;
Guid ListErrorID = service.Create(Entity);
AssociateRequest ListToListError = new AssociateRequest
{
//The contact record
Target = new EntityReference(updateEntity.LogicalName, Id),
//The list of account you would like the contact to be linked with (in this case we have only one account)
RelatedEntities = new EntityReferenceCollection
{
new EntityReference(Entity.LogicalName, ListErrorID)
},
Relationship = new Relationship("aw_listerror")
};
service.Execute(ListToListError);
//</Creating error log for list entity>
}
}
}
}
private static EntityCollection GetEntityCollection(IOrganizationService service, string entityName, string attributeName, string attributeValue, ColumnSet cols)
{
QueryExpression query = new QueryExpression
{
EntityName = entityName,
ColumnSet = cols,
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression
{
AttributeName = attributeName,
Operator = ConditionOperator.Equal,
Values = { attributeValue }
}
}
}
};
return service.RetrieveMultiple(query);
}
}
public static class EntityName
{
public const string aw_ = "aw_";
public const string aw__User = "aw_user";
//<Creating error log for list entity>
public const string ListError = "aw__listerror";
//<Creating error log for list entity>
}
public static class aw__Attributes
{
public const string name = "aw_name";
public const string Campaign = "aw_campaign";
public const string FromName = "aw_fromname";
public const string Subject = "aw_subject";
public const string Fromemail = "aw_fromemail";
public const string CreateListStatus = "aw_sent";
public const string ListId = "aw_listid";
}
public static class aw__User_Attributes
{
public const string User = "aw__user";
public const string Address = "aw__address";
public const string City = "aw__city";
public const string State = "aw__state";
public const string PostalCode = "aw__postalcode";
public const string BusinessUnitId = "aw__companyname";
public const string Country = "aw__countrycode";
}
//<Creating error log for list entity>
public static class ListError_Attributes
{
public const string ListID = "aw_listid";
public const string Response = "aw_response";
public const string IntegrationType = "aw_responsetype";
public const string Status = "aw_status";
public const string IntegrationDescription = "aw_description";
public const string ResponseDetail = "aw_errordetail";
public const string DateTime = "aw_datetime";
}
//<Creating error log for list entity>
}
Thank You