Quantcast
Viewing all articles
Browse latest Browse all 53708

Tip #532: Retrieving global optionsets using web api

Image may be NSFW.
Clik here to view.
Dynamics CRM Tipper
Even the best of us are still finding their footing with new shiny CRM Web API. Tipsters to the rescue in another mini truck stop.

was wondering what is the best way to fetch Global OptionSets using Web API (from now on apiendpoint == https://orgname.crm.dynamics.com/api/data/v8.0):

I can get all global optionsets using apiendpoint/GlobalOptionSetDefinitions– but this is not good from performance point of view. I can get one specific using identifier of optionset – apiendpoint/GlobalOptionSetDefinitions(guid) but how can I get this id? When I try to use $filter with GlobalOptionSetDefinitions I get error that GlobalOptionSetDefinitions doesn’t support $filter. Of course I can use Soap endpoint and RetrieveGlobalOptionSet message but may be I missed something in Web API specifications?

Suggestion from yours truly has hit the mark:

apiendpoint/GlobalOptionSetDefinitions?$select=Name seems to be very efficient, it gives you the full Name -> Id map. Then go by id.

Something like this:

function getbyid(token, metaid) {
  console.log("retrieving single optionset: "
             + metaid);

  webapiCall("GlobalOptionSetDefinitions" 
           + "(" + metaid + ")",
    token,
    function (os) {
      console.log(os.Name);
      console.log(os.OptionSetType);
      if (os.OptionSetType == "Boolean") {
        console.log(os
              .TrueOption.Label
              .UserLocalizedLabel.Label);
      }
    });
}

function getbyname(token, optionsetName) {
  webapiCall("GlobalOptionSetDefinitions"
           + "?$select=Name", 
    token,
    function (reply) {
      var allsets = reply.value;
      console.log("scanning optionsets...");
      allsets.forEach(function (os) {
        if (os.Name == optionsetName) {
          getbyid(token, os.MetadataId);
          return false;
        }
      });
    });
}
Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 53708

Trending Articles