Tuesday 2 April 2013

Search field schema name using field display name in MS CRM 2011


Here is sample code

private string GetAttributeSchemaName(IOrganizationService _Service, string attributeDisplyName)
{

string attributeSchemaName = null;

try
{

RetrieveEntityRequest req = new RetrieveEntityRequest();

req.RetrieveAsIfPublished = true;

req.LogicalName = "opportunityproduct";

req.EntityFilters = EntityFilters.Attributes;

RetrieveEntityResponse resp = (RetrieveEntityResponse)_Service.Execute(req);

for (int a = 0; a < resp.EntityMetadata.Attributes.Length; a++)
{

if (resp.EntityMetadata.Attributes[a].DisplayName.UserLocalizedLabel != null)
{
if (resp.EntityMetadata.Attributes[a].DisplayName.UserLocalizedLabel.Label.ToLower() == attributeDisplyName.ToLower())
{
attributeSchemaName = resp.EntityMetadata.Attributes[a].LogicalName;
break;
}

}
}
}

catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}

return attributeSchemaName;

}

Re-Open Lost Opportunity in MS CRM 2011 using C#



Here is same code

try
{
OrganizationRequest _request = new OrganizationRequest();
_request.RequestName = "SetState";
_request.Parameters = new Microsoft.Xrm.Sdk.ParameterCollection();

_request.Parameters.Add(new KeyValuePair<string, object>("EntityMoniker", new EntityReference() { Id = _ID, LogicalName = "opportunity" }));
_request.Parameters.Add(new KeyValuePair<string, object>("Status", new OptionSetValue() { Value = 1 }));
_request.Parameters.Add(new KeyValuePair<string, object>("State", new OptionSetValue() { Value = 0 }));

_Service.Execute(_request);

}
catch (Exception _ex)
{
throw _ex;
}