locked
Retrieving metadata in Silverlight with CRM 2011 RRS feed

  • Question

  • I am working on a Silverlight web resource for CRM 2011 online and am trying to retrieve some entity metadata.  However, it appears that the soap service does not contain the needed 'RetrieveEntityRequest' class.  Since the SDK libraries are not compatible with Silverlight I'm not sure how to do it.

    So my questions is how do you retrieve metadata in CRM 2011 online in Silverlight?

    Thanks! - Steve


    Steven Rasmussen
    Saturday, March 12, 2011 12:19 AM

Answers

  • The last part of that walkthrough contains code to use the RetrieveAllEntities Request:
        private void EntityList_Click(object sender, RoutedEventArgs e)
        {
          try
          {
            OrganizationRequest request = new OrganizationRequest() { RequestName = "RetrieveAllEntities" };
            request["EntityFilters"] = EntityFilters.Entity;
            request["RetrieveAsIfPublished"] = false;
            IOrganizationService service = SilverlightUtility.GetSoapService();
            service.BeginExecute(request, new AsyncCallback(EntityList_ClickCallback), service);
          }
          catch (Exception ex)
          { this.ReportError(ex); }
    
    
        }
    
        private void EntityList_ClickCallback(IAsyncResult result)
        {
          try
          {
    
            OrganizationResponse response = ((IOrganizationService)result.AsyncState).EndExecute(result);
            System.Collections.ObjectModel.ObservableCollection<EntityMetadata> entities = (System.Collections.ObjectModel.ObservableCollection<EntityMetadata>)response["EntityMetadata"];
    
            StringBuilder sb = new StringBuilder();
            foreach (EntityMetadata entity in entities)
            {
              sb.AppendLine(entity.SchemaName);
            }
            this.ReportMessage(sb.ToString());
    
    
          }
          catch (Exception ex)
          {
            this.ReportError(ex);
          }
    
    
        }
    
    
    

    Jim Daly Technical Writer Microsoft Dynamics CRM
    Saturday, March 12, 2011 12:27 AM
    Answerer
  • Use the RetrieveEntityRequest.

    With Silverlight you always use the OrganizationRequest, the base class for all Requests.

    Then you need to set the Request Name and any of the parameters required. RetrieveEntity uses EntityFilters and the LogicalName for the entity.

    You will get single OrganizationResponse object returned which you should be able to cast as an EntityMetadata object.

    I hope this helps.


    Jim Daly Technical Writer Microsoft Dynamics CRM
    Wednesday, March 16, 2011 3:40 AM
    Answerer

All replies

  • The last part of that walkthrough contains code to use the RetrieveAllEntities Request:
        private void EntityList_Click(object sender, RoutedEventArgs e)
        {
          try
          {
            OrganizationRequest request = new OrganizationRequest() { RequestName = "RetrieveAllEntities" };
            request["EntityFilters"] = EntityFilters.Entity;
            request["RetrieveAsIfPublished"] = false;
            IOrganizationService service = SilverlightUtility.GetSoapService();
            service.BeginExecute(request, new AsyncCallback(EntityList_ClickCallback), service);
          }
          catch (Exception ex)
          { this.ReportError(ex); }
    
    
        }
    
        private void EntityList_ClickCallback(IAsyncResult result)
        {
          try
          {
    
            OrganizationResponse response = ((IOrganizationService)result.AsyncState).EndExecute(result);
            System.Collections.ObjectModel.ObservableCollection<EntityMetadata> entities = (System.Collections.ObjectModel.ObservableCollection<EntityMetadata>)response["EntityMetadata"];
    
            StringBuilder sb = new StringBuilder();
            foreach (EntityMetadata entity in entities)
            {
              sb.AppendLine(entity.SchemaName);
            }
            this.ReportMessage(sb.ToString());
    
    
          }
          catch (Exception ex)
          {
            this.ReportError(ex);
          }
    
    
        }
    
    
    

    Jim Daly Technical Writer Microsoft Dynamics CRM
    Saturday, March 12, 2011 12:27 AM
    Answerer
  • Awesome!  Thanks so much for the quick answer!  Not sure why I didn't see this when I went through that document.  Anyway, I will test this over the weekend.

    Thanks! - Steve


    Steven Rasmussen
    Saturday, March 12, 2011 12:42 AM
  • That was it!  Thanks!  Do you know how to change it to just retrieve a single entity instead of all the entity metadata?  I image you change the 'RetrieveAllEntities' to something else but not sure what to put. Also not sure where to tell it the name of the entity that I would like to retrieve.

    Any additional help would be greatly appreciated!  Thanks.  Steve


    Steven Rasmussen
    Wednesday, March 16, 2011 12:58 AM
  • Use the RetrieveEntityRequest.

    With Silverlight you always use the OrganizationRequest, the base class for all Requests.

    Then you need to set the Request Name and any of the parameters required. RetrieveEntity uses EntityFilters and the LogicalName for the entity.

    You will get single OrganizationResponse object returned which you should be able to cast as an EntityMetadata object.

    I hope this helps.


    Jim Daly Technical Writer Microsoft Dynamics CRM
    Wednesday, March 16, 2011 3:40 AM
    Answerer
  • That worked!  However, just to make note for anyone else going here searching for the answer you need to add the required parameters of 'MetadataId' and 'RetrieveAsIfPublished' as well.  Simply set the MetadataId to Guid.Empty since we don't know what it is.

    Here is my code that I used:

    OrganizationRequest request = new OrganizationRequest();
    request.RequestName = "RetrieveEntity";
    request["EntityFilters"] = EntityFilters.Entity | EntityFilters.Attributes | EntityFilters.Relationships;
    request["LogicalName"] = EntityName;
    request["MetadataId"] = Guid.Empty;
    request["RetrieveAsIfPublished"] = true;
    Service.ExecuteAsync(request);
    

    Steven Rasmussen
    Wednesday, March 16, 2011 9:13 PM
  • Hi Jim,

    Can you help how to retrieve Option Set values of an attribute from meta data.

    Thanks,

    Wednesday, April 6, 2011 2:25 PM
  • I'm actually just starting to create a sample for this today.

    Once those options are retrieved they need to be cached to allow the UI to bind to them for the Silverlight ComboBox so they are available for options.

    The sample I'm starting will show how that can be done.


    Jim Daly Technical Writer Microsoft Dynamics CRM
    • Proposed as answer by Adnan Haider Thursday, April 7, 2011 2:52 PM
    Wednesday, April 6, 2011 3:04 PM
    Answerer
  • Thanks for help Jim. Here is what works for me. I hope this will help others. I am using soap for silverlight example in SDK.

    OrganizationRequest request = new OrganizationRequest();
    
    request.RequestName = "RetrieveAttribute";
    
    request["EntityLogicalName"] = "opportunity";    
    
    request["LogicalName"] = "new_secondaryreason";
    
    request["MetadataId"] = Guid.Empty;
    
    request["RetrieveAsIfPublished"] = true;
    
    
    
    IOrganizationService service = SilverlightUtility.GetSoapService();
    
    service.BeginExecute(request, new AsyncCallback(RetrieveReasonsPCallback), service);
    
    
    
    

    Now trying to update status of opportunity using "SetState" request but having issues. Can anyone help and see whats wrong with the following code:

    EntityReference er = new EntityReference();
    
    er.Id = new Guid("4C085214-A479-2360-E011-000C29EB3827");
    
    er.LogicalName = "opportunity";
    
    
    
    OptionSetValue States = new OptionSetValue();
    
    States.Value = 1;
    
    
    
    OptionSetValue Status = new OptionSetValue();
    
    Status.Value = 3;
    
    
    
    OrganizationRequest request = new OrganizationRequest() { RequestName = "SetState" };
    
    request["EntityMoniker"] = er;
    
    request["State"] = States;
    
    request["Status"] = Status;
    
    
    
    IOrganizationService service = SilverlightUtility.GetSoapService();
    
    service.BeginExecute(request, new AsyncCallback(Setstate_ClickCallback), service);
    
    
    
    
    Thx,
    
    

    Thursday, April 7, 2011 2:51 PM
  • Jim

    Have you completed the sample you mentioned above, and is there a place where it can be accessed, please. (If it's in the SDK, please just say the version it first appears in and what it is called.)

    Thanks

    Peter

    Friday, August 26, 2011 8:52 PM
  • Sadly I haven't gotten the Silverlight OptionSet sample finished. I did one for JScript: Using Option Set Options with the REST Endpoint - JScript, but frankly the whole MVVM part in Silverlight was challenging for me. I moved on to some other projects where I could move forward.

    There are some concepts in the JScript sample that would also apply to the Silverlight sample.

    In theory I've overcome the issues that were blocking me, so I'll raise the priority in getting that finished.


    Jim Daly Technical Writer Microsoft Dynamics CRM
    Friday, August 26, 2011 9:30 PM
    Answerer
  • Great, thanks.

    I'm finding the Silverlight challenging too ...


    Peter Loudon
    Friday, August 26, 2011 10:11 PM
  • Just in case someone runs across this thread here is how I was able to retrieve a single option set:

    //REQUEST
    try
                {
                    OrganizationRequest request = new OrganizationRequest();
                    request.RequestName = "RetrieveAttribute";
                    request["EntityLogicalName"] =  _EntityType;
                    request["LogicalName"] = _IconDefField;
                    request["MetadataId"] = Guid.Empty;
                    request["RetrieveAsIfPublished"] = true;
                    IOrganizationService service = SilverlightUtility.GetSoapService();
                    service.BeginExecute(request, new AsyncCallback(BuildLegendGetOptionsComplete), service);
                }
                catch (Exception ex)
                {
                    this.ReportError(ex);
                }

    //Response
    try
                {
                   StringBuilder sb = new StringBuilder();
                   OrganizationResponse response = ((IOrganizationService)result.AsyncState).EndExecute(result);
                   PicklistAttributeMetadata picklist = (PicklistAttributeMetadata)response.Results[0].Value; 
                   foreach( OptionMetadata option in picklist.OptionSet.Options ) 
                   {
                       sb.AppendLine(option.Label.UserLocalizedLabel.Label);  
                   } 
                    this.ReportMessage(sb.ToString());
                }
                catch (Exception ex)
                {
                    this.ReportError(ex);
                }


    WeDoMapping

    Friday, March 9, 2012 5:59 PM