locked
crm 2011 web services lookup parameter type RRS feed

  • Question

  • Currently, I'm trying to create a custom webservice which exposes the CRM product attributes, my external application will access this web service and pass the required parameters (exposed in my webservice) and as a end result, with the passes parameters product will be created in CRM.

    My Problem,

    Currently im testing with 4 attributes of CRM product form,

    - product ID (string)
    - Product Name (string)
    - Default Unit (lookup)
    - Primary Unit (lookup)


    In my method, for product ID and product name, the datatype is string, and what should be my datatype for lookup attributes such as "Default Unit" and "Primary Unit".

    As i see, there is no datatype as lookup in C#.net.... I'm struck here and need help.

    My Code:

    [WebMethod]


            public string CreateProduct(string number, string pname, string defaultunit, string primaryunit)
            {
                string message = string.Empty;
                try
                {
                    OrganizationServiceProxy serviceProxy;
                    ClientCredentials deviceCredentials = null;
                    ClientCredentials clientCredentials = null;
                    Guid productId = Guid.Empty;
                                            
                    //Organization URL
                    Uri OrganizationUri = new Uri(String.Format("http://crm2011/xxx/XRMServices/2011/Organization.svc"));
                    //Discovery URL
                    Uri HomeRealmUri = new Uri(String.Format("http://crm2011/XRMServices/2011/Discovery.svc"));
                    //Setting Client Credentials
                    clientCredentials = this.GetCredentials("contoso\administrator", "pass@word1");
                    //Get the Device Id and Password
                    //  deviceCredentials = this.GetDeviceCredentials();
                    //Using Organization Service Proxy to instantiate the CRM Web Service
                    using (serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, clientCredentials, deviceCredentials))
                    {
                        // This statement is required to enable early-bound type support.
                        serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
                        IOrganizationService service = (IOrganizationService)serviceProxy;
                        //Define Entity Object
                        Entity product = new Entity("product");
                      
                        //Specify the attributes
                        product.Attributes["productnumber"] = number;
                        product.Attributes["name"] = pname;
                        product.Attributes["defaultuomid"] = defaultunit;
                        product.Attributes["defaultuomscheduleid"] = primaryunit;
                     
                        //Execute the service
                        productId = service.Create(product);
                                          
                        //Confirmation Message
                       
                        message = "Product Created with Product Number:- " + pname + " Guid is" + productId.ToString();

                    }
                }
                catch (Exception ex)
                {
                    message = ex.Message;
                }
                //returns the message
                return message;
         }
       


    Thanks & Regards, MS CRM Consultant, V.Surya.


    Tuesday, February 14, 2012 12:00 PM
    Answerer

Answers

All replies

  • Hello Surya,

    Lookup is for CRM 4.0. For CRM 2011 you should use EntityReference class.


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter

    Tuesday, February 14, 2012 12:19 PM
    Moderator
  • use the "EntityReference" with the id of the "defaultunit" and "primaryunit". You can get the id by click "copy a link" button on the "UnitGroup" and Unit Form.
    Tuesday, February 14, 2012 12:30 PM
  • Dear Nallathambi,

    EntityReferencelookup = newEntityReference();

    lookup.Id = newGuid("primaryunit"); 

    And

    i tried this also

    EntityReference lookup = newEntityReference();                 

    lookup.Id = newGuid("7bF6AFFA23-DA56-E111-95C0-08002717A215")

                      

    Tried as per your suggestion but didnt work, am pasting my code above for your reference please correct if any mistake.
    Tuesday, February 14, 2012 1:12 PM
    Answerer
  • Surya,

    You have to use something like following:

    EntityReference reference = new EntityReference();
    reference.Id = new Guid("7bF6AFFA23-DA56-E111-95C0-08002717A215");
    reference.LogicalName = "uom";
    For each EntityReference you have to fill logical name field which actually means to what type of entity your lookup is pointed.


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter


    Tuesday, February 14, 2012 4:42 PM
    Moderator
  • Hi Andriy,

    I tried follwing code and included your code also,

    public string CreateProduct(string number, string pname, string defaultunit, string primaryunit)

    {

    Entity product = new Entity("product");

    Guid productId = Guid.Empty;

    EntityReference reference = new EntityReference();
    reference.Id = new Guid("F6AFFA23-DA56-E111-95C0-08002717A215");
    reference.LogicalName = "uomschedule";

    defaultunit = reference.Id.ToString();
    primaryunit = reference.Id.ToString();

                     
    product.Attributes["productnumber"] = number;
    product.Attributes["name"] = pname;
    product.Attributes["defaultuomid"] = defaultunit;
    product.Attributes["defaultuomscheduleid"] = primaryunit;

    message = "Product Created with Product Number:- " + pname + " Guid is" + productId.ToString();


    }

     

    After entering value in web method

    Am getting this following error:

    <?xml version="1.0" encoding="UTF-8"?>
    <string xmlns="The">http://tempuri.org/">The unit schedule id is missing.</string>

    Help me,

                 


    Thanks & Regards, MS CRM Consultant, V.Surya.

    Tuesday, February 14, 2012 6:56 PM
    Answerer
  • Regarding attributes defaultuomid and defaultscheduleid you should use something like following:

    product["defaultuomscheduleid"] = new EntityReference("uomschedule", new Guid("003B97ED-A8EF-4D68-8E5E-303F9356ECAC"));
    product["defaultuomid"] = new EntityReference("uom", new Guid("842A26EF-2884-4A08-8704-83838E6E814B"));
    

    Of course you should replace guids I've provided with guids from your system.


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter

    Tuesday, February 14, 2012 7:30 PM
    Moderator
  • Hi Andriy,

    Thanks the code which given it will be useful to my scenario and it worked to me. One more question.

    How do i assign GUId dynamically instead of hardcoding or assigning one GUID.

    Please send me the sample code


    Thanks & Regards, MS CRM Consultant, V.Surya.

    Wednesday, February 15, 2012 11:27 AM
    Answerer
  • Hello,

    To get identifier of entities you should fetch this information from CRM. Samples:

    http://technet.microsoft.com/en-us/library/gg328149.aspx

    http://technet.microsoft.com/en-us/library/gg509064.aspx


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter

    Wednesday, February 15, 2012 11:37 AM
    Moderator