using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
namespace MyNameSpace
{
public class CrmService
{
private IOrganizationService _service;
public IOrganizationService Default
{
get { return _service; }
}
public CrmService(IOrganizationService service)
{
_service = service;
}
public Entity RetrieveByQuery(string entityName, string[] columns, bool activeRecordsOnly, LinkEntity[] linkEntities, params ConditionExpression[] condition)
{
QueryExpression qe = new QueryExpression(entityName);
if (columns.Length > 0)
qe.ColumnSet = new ColumnSet(columns);
else
qe.ColumnSet.AllColumns = true;
if (null != condition)
qe.Criteria.Conditions.AddRange(condition);
if (activeRecordsOnly)
qe.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);
if (null!=linkEntities)
qe.LinkEntities.AddRange(linkEntities);
EntityCollection entityCollect = (EntityCollection)Default.RetrieveMultiple(qe);
if (entityCollect.Entities.Count > 0)
{
return entityCollect.Entities.First();
}
else
{
return null;
}
}
public Entity RetrieveByQuery(string entityName, ColumnSet columns, bool activeRecordsOnly, LinkEntity[] linkEntities, params ConditionExpression[] conditions)
{
QueryExpression qe = new QueryExpression(entityName);
if (null == columns)
qe.ColumnSet.AllColumns = true;
else
qe.ColumnSet = columns;
if (null != conditions)
qe.Criteria.Conditions.AddRange(conditions);
if (activeRecordsOnly)
qe.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);
if (null!=linkEntities)
qe.LinkEntities.AddRange(linkEntities);
EntityCollection entityCollect = (EntityCollection)Default.RetrieveMultiple(qe);
if (entityCollect.Entities.Count > 0)
{
return entityCollect.Entities.First();
}
else
{
return null;
}
}
public EntityCollection RetrieveMultipleByQuery(string entityName, ColumnSet columns, bool activeRecordsOnly, LinkEntity[] linkEntities, params ConditionExpression[] condition)
{
QueryExpression qe = new QueryExpression(entityName);
if (null == columns)
qe.ColumnSet.AllColumns = true;
else
qe.ColumnSet = columns;
if (null != condition)
qe.Criteria.Conditions.AddRange(condition);
if (activeRecordsOnly)
qe.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);
if (null != linkEntities)
qe.LinkEntities.AddRange(linkEntities);
EntityCollection entityCollect = (EntityCollection)Default.RetrieveMultiple(qe);
return entityCollect;
}
public EntityCollection RetrieveMultipleByQuery(string entityName, string[] columns, bool activeRecordsOnly, LinkEntity[] linkEntities, params ConditionExpression[] condition)
{
QueryExpression qe = new QueryExpression(entityName);
qe.ColumnSet = new ColumnSet(columns);
if (null != condition)
qe.Criteria.Conditions.AddRange(condition);
if (activeRecordsOnly)
qe.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);
if (null != linkEntities)
qe.LinkEntities.AddRange(linkEntities);
EntityCollection entityCollect = (EntityCollection)Default.RetrieveMultiple(qe);
return entityCollect;
}
public QueryExpression GetQuery(string entityName, ColumnSet columns, bool activeRecordsOnly, LinkEntity[] linkEntities, params ConditionExpression[] condition)
{
QueryExpression qe = new QueryExpression(entityName);
if (null == columns)
qe.ColumnSet.AllColumns = true;
else
qe.ColumnSet = columns;
if (null != condition)
qe.Criteria.Conditions.AddRange(condition);
if (activeRecordsOnly)
qe.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);
if (null != linkEntities)
qe.LinkEntities.AddRange(linkEntities);
return qe;
}
public EntityMetadata RetrieveEntity(string entityName)
{
RetrieveEntityRequest req = new RetrieveEntityRequest();
req.LogicalName = entityName;
req.RetrieveAsIfPublished = true;
RetrieveEntityResponse res = (RetrieveEntityResponse)Default.Execute(req);
return res.EntityMetadata;
}
public OptionSetMetadata RetrieveOptionSet(string optionsetName)
{
RetrieveOptionSetRequest req = new RetrieveOptionSetRequest();
req.Name = optionsetName;
req.RetrieveAsIfPublished = true;
RetrieveOptionSetResponse res = (RetrieveOptionSetResponse)Default.Execute(req);
return res.OptionSetMetadata as OptionSetMetadata;
}
public AttributeMetadata RetrieveAttribute(string entityName,string attributeName)
{
RetrieveAttributeRequest req = new RetrieveAttributeRequest();
req.EntityLogicalName = entityName;
req.LogicalName = attributeName;
req.RetrieveAsIfPublished = true;
RetrieveAttributeResponse res = (RetrieveAttributeResponse)Default.Execute(req);
return res.AttributeMetadata;
}
public int GetPickListValueWithPrefix(string entity, string attributeName, string twoDigitValue)
{
PicklistAttributeMetadata attr = (PicklistAttributeMetadata)RetrieveAttribute(entity, attributeName);
OptionMetadata option = new OptionMetadata();
if (attr != null)
{
if (attr.OptionSet != null)
option = attr.OptionSet.Options.ToList().Find(o => o.Value.Value.ToString().EndsWith(twoDigitValue));
}
else
{
throw new InvalidOperationException("Option set not found");
}
if (option != null)
{
if (option.Value.HasValue)
return option.Value.Value;
else
throw new InvalidOperationException("Option " + twoDigitValue + " not found");
}
else
{
throw new InvalidOperationException("Option " + twoDigitValue + " not found");
}
}
public int GetPickListValue(string entity, string attributeName, string label)
{
PicklistAttributeMetadata attr = (PicklistAttributeMetadata)RetrieveAttribute(entity, attributeName);
OptionMetadata option = new OptionMetadata();
if (attr != null)
{
if (attr.OptionSet != null)
option = attr.OptionSet.Options.ToList().Find(o => o.Label.UserLocalizedLabel.Label.ToLower().Equals(label.ToLower()));
}
else
{
throw new InvalidOperationException("Option set not found");
}
if (option != null)
{
if (option.Value.HasValue)
return option.Value.Value;
else
throw new InvalidOperationException("Option " + label + " not found");
}
else
{
throw new InvalidOperationException("Option " + label + " not found");
}
}
}
}