locked
Hello World from C# RRS feed

  • Question

  • I'm trying to use the MSCRM 4.0 SDK to create some entities (and look them up first).  Is there a Hello World of sorts?  All the examples are plugins/workflows/etc, can't find one that just connects, issues a query and then creates an account (or a lead or whatever).

    Here's where I am so far:
    using Microsoft.Crm.Sdk;

    Ok, so that's a little joke, but seriously, WSDL, Microsoft.Crm.Sdk... Which/when/why?
    Monday, May 4, 2009 6:51 PM

Answers

  • Ok, got that.  Now how do I dynamically add to the picklist?  So far I've got this:
    	// Create new picklist label
    CrmLabel newCrmLabel = new CrmLabel();
    LocLabel englishLabel = new LocLabel();
    // Use US English lang code
    englishLabel.LanguageCode = new CrmNumber(1033);
    englishLabel.Label = metro;
    newCrmLabel.LocLabels = new LocLabel[] { englishLabel };

    InsertOptionValueRequest iov = new InsertOptionValueRequest();
    iov.EntityLogicalName = "new_myentity";
    iov.AttributeLogicalName = "new_metroarea";
    iov.Label = metro;
    iov.Value = ????
    The bit I don't get is how I'm supposed to come up with a value.  Shouldn't CRM do that?  Also, do I need to publish this operation, and if so is that synchronous?

    Thanks for the help!
    To the Value field you have to insert identifier of picklist option. Look here .

    After adding of options you have to publish cusomizations. Look here .

    Истина открывается подготовленному уму. Мой блог - http://a33ik.blogspot.com
    Monday, May 4, 2009 8:11 PM
    Moderator

All replies

  • Hi, Max.

    Here sample for creation of account.
    I don't use Web References - only microsoft.crm.sdk.dll and microsoft.crm.sdktypeproxy.dll.
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Crm.SdkTypeProxy;
    using Microsoft.Crm.Sdk;
    using Microsoft.Win32;
    using Microsoft.Crm.Sdk.Query;
    
    namespace VehiclesInConvertor
    {
        class Program
        {
            static void Main(string[] args)
            {
                CrmService crmService = GetCrmService();
    
    	    account newaccount = new account();
    	    account.name = "Created from cshapr";
    	    Guid accountid = crmService.Create(account);
    
            }
    
            private static CrmService GetCrmService()
            {
                CrmAuthenticationToken token = new CrmAuthenticationToken();
                token.AuthenticationType = AuthenticationType.AD;
                token.OrganizationName = "Winner-Automotive";
    
                CrmService crmService = new CrmService();
                crmService.UseDefaultCredentials = true;
                crmService.Url = (string)(Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\MSCRM").GetValue("ServerUrl")) + "/2007/crmservice.asmx";
                crmService.CrmAuthenticationTokenValue = token;
    
                return crmService;
            }
        }
    }
    

    Истина открывается подготовленному уму. Мой блог - http://a33ik.blogspot.com
    Monday, May 4, 2009 7:04 PM
    Moderator
  • Excellent, thanks.  That worked and then I added a lookup like so:

    				ConditionExpression ce = new ConditionExpression("new_title", ConditionOperator.Equal, t.FullName);				
    				FilterExpression filter = new FilterExpression();
    				filter.FilterOperator = LogicalOperator.And;
    				filter.AddCondition(ce);
            QueryExpression query = new QueryExpression();
            query.EntityName = "new_myentity";
            query.ColumnSet = new AllColumns();
            query.Criteria = filter;
            RetrieveMultipleRequest retrieve = new RetrieveMultipleRequest();
            retrieve.Query = query;
            retrieve.ReturnDynamicEntities = true;
            RetrieveMultipleResponse retrieved = (RetrieveMultipleResponse) mService.Execute(retrieve);
            DynamicEntity povoent;
    				if (retrieved.BusinessEntityCollection.BusinessEntities.Count >= 1)
    				{
    				}
    The remaining problem is with specifying a PickList field.  StringProperty doesn't do it it seems, but I haven't researched yet.
    Monday, May 4, 2009 7:25 PM
  • Hi, Max.

    There no problem with specifying of Picklist field types. In filters use int values as a value of filter. In create of new records - use PicklistProperty and Picklist:

    DynamicEntity entity = new DynamicEntity("new_entity");
    entity.Properties.Add(new PicklistProperty("new_picklistproperty", new Picklist(1)));


    Истина открывается подготовленному уму. Мой блог - http://a33ik.blogspot.com
    Monday, May 4, 2009 7:32 PM
    Moderator
  • Ok, got that.  Now how do I dynamically add to the picklist?  So far I've got this:
    	// Create new picklist label
    	CrmLabel newCrmLabel = new CrmLabel();
    	LocLabel englishLabel = new LocLabel();
    	// Use US English lang code
    	englishLabel.LanguageCode = new CrmNumber(1033);
    	englishLabel.Label = metro;
    	newCrmLabel.LocLabels = new LocLabel[] { englishLabel };
    
    	InsertOptionValueRequest iov = new InsertOptionValueRequest();
    	iov.EntityLogicalName = "new_myentity";
    	iov.AttributeLogicalName = "new_metroarea";
    	iov.Label = metro;
    	iov.Value = ????
    The bit I don't get is how I'm supposed to come up with a value.  Shouldn't CRM do that?  Also, do I need to publish this operation, and if so is that synchronous?

    Thanks for the help!
    Monday, May 4, 2009 7:41 PM
  • Ok, got that.  Now how do I dynamically add to the picklist?  So far I've got this:
    	// Create new picklist label
    CrmLabel newCrmLabel = new CrmLabel();
    LocLabel englishLabel = new LocLabel();
    // Use US English lang code
    englishLabel.LanguageCode = new CrmNumber(1033);
    englishLabel.Label = metro;
    newCrmLabel.LocLabels = new LocLabel[] { englishLabel };

    InsertOptionValueRequest iov = new InsertOptionValueRequest();
    iov.EntityLogicalName = "new_myentity";
    iov.AttributeLogicalName = "new_metroarea";
    iov.Label = metro;
    iov.Value = ????
    The bit I don't get is how I'm supposed to come up with a value.  Shouldn't CRM do that?  Also, do I need to publish this operation, and if so is that synchronous?

    Thanks for the help!
    To the Value field you have to insert identifier of picklist option. Look here .

    After adding of options you have to publish cusomizations. Look here .

    Истина открывается подготовленному уму. Мой блог - http://a33ik.blogspot.com
    Monday, May 4, 2009 8:11 PM
    Moderator
  • Hi djMax

    Take a look at our Linq to CRM tool, it allows you to write LINQ queries against CRM instead of using FetchXml and QueryExpressions.
    XrmLinq - LINQ to Dynamics CRM http://www.xrmlinq.com
    Tuesday, May 5, 2009 10:16 AM