serving the solutions day and night

Pages

Tuesday, February 12, 2013

Get CRM PickList & Global OptionSet using c#

using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;

Uri OrganizationUri = new Uri(System.Configuration.ConfigurationManager.AppSettings["CRM_Organization_URI"].ToString());
Uri HomeRealmUri = null;
Dictionary<String, String> dicState = new Dictionary<String, String>();
Dictionary<String, String> dicSuffix = new Dictionary<String, String>();

Load Pick List
private void Load_PickList(String entityName, String attID, string attName, Dictionary dic)
{
  using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null))
  {
    IOrganizationService service = (IOrganizationService)serviceProxy;
    QueryExpression qryExp = new QueryExpression(entityName);
    qryExp.ColumnSet = new ColumnSet(attName);
    EntityCollection entCollection = service.RetrieveMultiple(qryExp);
    dic = entCollection.Entities.ToDictionary(e => e.Attributes[attID].ToString(), e => e.Attributes[attName].ToString());
/*    foreach (var c in entCollection.Entities)
    {
      dic.Add(c.Attributes[attID].ToString(), c.Attributes[attName].ToString());
    }
  }*/
}

Load Global OptionSet List
private void Load_OptionSetList(String optionSetName, Dictionary dic)
{
  using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null))
  {
    serviceProxy.EnableProxyTypes();
    RetrieveOptionSetRequest retrieveOptionSetRequest =new RetrieveOptionSetRequest{Name = optionSetName};
    RetrieveOptionSetResponse retrieveOptionSetResponse = (RetrieveOptionSetResponse)serviceProxy.Execute(retrieveOptionSetRequest);
    OptionSetMetadata retrievedOptionSetMetadata =(OptionSetMetadata)retrieveOptionSetResponse.OptionSetMetadata;
    OptionMetadata[] optionList =retrievedOptionSetMetadata.Options.ToArray();
    foreach (OptionMetadata oMD in optionList)
    {
      dic.Add(oMD.Value.ToString(), oMD.Label.UserLocalizedLabel.Label.ToString());
    }
  }
}

Using Linq
dicEmployeeType = OptionSetList("gab_employeetype");
private Dictionary<String, String> OptionSetList(String optionSetName)
        {
            Dictionary<String, String> dic = new Dictionary<String, String>();
            using (serviceProxy)
            {
                RetrieveOptionSetRequest rosRequest = new RetrieveOptionSetRequest { Name = optionSetName };
                RetrieveOptionSetResponse rosResponse = (RetrieveOptionSetResponse)serviceProxy.Execute(rosRequest);
                OptionSetMetadata osm = (OptionSetMetadata)rosResponse.OptionSetMetadata;
                dic = osm.Options.ToDictionary(e => e.Value.ToString(), e => e.Label.UserLocalizedLabel.Label.ToString());
            }
            return dic;
        }


Load Local OptionSet List
private void LocalOptionSetList(String entityName, String optionSetName, Dictionary dic)
{
  using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null))
  {
     serviceProxy.EnableProxyTypes();
     RetrieveAttributeRequest raRequest = new RetrieveAttributeRequest
     {
        EntityLogicalName = entityName,
        LogicalName = optionSetName,
        RetrieveAsIfPublished = true
     };
     RetrieveAttributeResponse raResponse = (RetrieveAttributeResponse)serviceProxy.Execute(raRequest);
     PicklistAttributeMetadata paMetadata = (PicklistAttributeMetadata)raResponse.AttributeMetadata;
     OptionMetadata[] optionList = paMetadata.OptionSet.Options.ToArray();
     foreach (OptionMetadata oMD in optionList)
     {
        dic.Add(oMD.Value.ToString(), oMD.Label.UserLocalizedLabel.Label.ToString());
     }
  }
}

Load All Global OptionSet
private void Load_AllGlobalOptionSet() {
  using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null))
  {
    serviceProxy.EnableProxyTypes();
    RetrieveAllOptionSetsRequest retrieveAllOptionSetsRequest =new RetrieveAllOptionSetsRequest();
    RetrieveAllOptionSetsResponse retrieveAllOptionSetsResponse = (RetrieveAllOptionSetsResponse)serviceProxy.Execute(retrieveAllOptionSetsRequest);
    if (retrieveAllOptionSetsResponse.OptionSetMetadata.Count() > 0)
    {
      Debug.WriteLine("All the global option sets retrieved as below:");
      int count = 1;
      foreach (OptionSetMetadataBase optionSetMetadata in retrieveAllOptionSetsResponse.OptionSetMetadata)
      {
        Debug.WriteLine("{0} {1}", count++, (optionSetMetadata.DisplayName.LocalizedLabels.Count >0)? optionSetMetadata.DisplayName.LocalizedLabels[0].Label : String.Empty);
      }
    }
  }
}


Read Dictionary Key & Value
public CRM()
{
  Load_PickList("gab_state", "gab_stateid", "gab_name", dicState);
  Load_OptionSetList("gab_suffix", dicSuffix);
  foreach (var pair in dicState)
  {
    Debug.WriteLine("{0}, {1}", pair.Key, pair.Value);
  }
  var fKey = dicJurisdiction.Single(x => x.Value == "NE");
  Console.WriteLine(fKey.Key);
}

Credentials
internal ClientCredentials Credentials
{
  get
  {
    //Authenticate using credentials of the logged in user;
    ClientCredentials cntCredentials = new ClientCredentials();
    cntCredentials.UserName.UserName = System.Configuration.ConfigurationManager.AppSettings["CRM_UserName"].ToString();
    cntCredentials.UserName.Password = System.Configuration.ConfigurationManager.AppSettings["CRM_Password"].ToString();
    //cntCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
    return cntCredentials;
  }
}

internal OrganizationServiceProxy CrmService
{
  get
  {
    OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null);
    serviceProxy.EnableProxyTypes();
    return serviceProxy;
  }
}

No comments: