Monday, September 16, 2013

How to solve problem "EntityState must be set to null, Created (for Create message) or Changed (for Update message)" while to try to update earlying binding entity?

How to solve problem "EntityState must be set to null, Created (for Create message) or Changed (for Update message)" while to try to update earlying binding entity?

There is one method in OrganizationServiceContext object calls: UpdateObject
Do not use the one in OrganizationServiceProxy. If use, the get the error message

context.UpdateObject(en);
//serviceProxy.Update(en);


ASP.NET and Web Tools for Visual Studio 2013 RC

http://blogs.msdn.com/b/webdev/archive/2013/09/09/announcing-release-of-asp-net-and-web-tools-for-visual-studio-2013-rc.aspx

Friday, September 13, 2013

The funny way to tell difference between null and undefined in JavaScript

You: What is name?
JavaScript: name? What's a name? I don't know what you're talking about. You haven't ever mentioned any name before. Are you seeing some other scripting language on the (client-)side?

name = null;
You: What is name?
JavaScript: I don't know.
In short; undefined is where no notion of the thing exists; it has no type, and it's never been referenced before in that scope; null is where the thing is known to exist, but it's not known what the value is.
One thing to remember is that null is not, conceptually, the same as false or "" or such, even if they equate after type casting, i.e.

name = false;
You: What is name?
JavaScript: Boolean false.

name = '';
You: What is name?
JavaScript: Empty string


http://stackoverflow.com/questions/801032/why-is-null-an-object-and-whats-the-difference-compared-to-undefined


The minimum steps to try out Microsoft CRM early binding

1. Generate CRM entity by CrmSDvcUtil.exe tool in folder of crm\sdk\bin
Reference:
http://msdn.microsoft.com/en-us/library/gg327844.aspx
CrmSvcUtil.exe /url:http://ServerName/OrganizationName/XRMServices/2011/Organization.svc /out:CSFileName.cs /username:usernamewithDomain /password:password /namespace:namespace /serviceContextName:contexName

2. Add following code in console app (Copy generate cs file into project and add it into)
            var _credentials = new ClientCredentials();
            _credentials.Windows.ClientCredential = (NetworkCredential)CredentialCache.DefaultCredentials;
            string CRM_SDK_URL = "http://ServerName/OrganizationName/XRMServices/2011/Organization.svc";
            OrganizationServiceProxy proxy = new OrganizationServiceProxy(new Uri(CRM_SDK_URL), null, _credentials, null);
            proxy.EnableProxyTypes();
            Account account = new Account { Name = "Sample Parent Account" };
            var _parentAccountId = proxy.Create(account);

Thursday, September 12, 2013

How to get list of sub accounts in CRM plugin ?

public void Execute(IServiceProvider serviceProvider)
  {
            Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext) serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                Entity entity = (Entity)context.InputParameters["Target"];
                if (entity.LogicalName == "account") 
                {
                    // Obtain the organization service reference.
                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                    //Get current account id (accountid)
                    var currentAccountid = entity["accountid"];
                    Microsoft.Xrm.Sdk.Query.QueryByAttribute querybyattribute = new Microsoft.Xrm.Sdk.Query.QueryByAttribute("account");
                    querybyattribute.ColumnSet = new Microsoft.Xrm.Sdk.Query.ColumnSet("accountnumber", "accountid", "emailaddress1");

                    querybyattribute.Attributes.AddRange("parentaccountid");
                    querybyattribute.Values.AddRange(currentAccountid);

                    EntityCollection retrieved = service.RetrieveMultiple(querybyattribute);
                    int count = 0;
                    foreach (var en in retrieved.Entities)
                    {
                        count += 1;
                        en.Attributes["accountnumber"] = count.ToString();
                        service.Update(en);
                    }
               }  
 
  }