Creating entities in CRM via C#


I'm trying to implement an EDI solution here at work but need some help. The data source will be xml file which is imported into CRM. I found some sample code that does similar things, but I'm trying to test it manually.

This is the main program :  Where   nOrd.Execute();   <-- this is where the error is, it's saying no argument for Iservice Provider. Is there any way to find out a list of values they are expecting here? I want to run this code once but I dont know what argument it's expecting. I want to then see if this creates the order in CRM.

static void Main(string[] args)
            NewOrders nOrd = new NewOrders();
            nOrd.Execute();   <-- this is where the error is, it's saying no argument for Iservice Provider.

Open in new window

This is the NewOrder class:
  public void Execute(IServiceProvider serviceProvider)
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            if (context.InputParameters.Contains("Target"))
                if (context.InputParameters["Target"] is Entity)
                    Entity entity = (Entity)context.InputParameters["Target"];
                    if (entity.LogicalName == "Orders")
                            Entity createOrder = new Entity("order");
                            createOrder["customerid"] = "Test";

                            if (context.OutputParameters.Contains("id"))
                                createOrder["parentorderid"] =
                                    new EntityReference { Id = new Guid(context.OutputParameters["id"].ToString()), LogicalName = entity.LogicalName };
                            IOrganizationServiceFactory serviceFactory =
                            IOrganizationService client = serviceFactory.CreateOrganizationService(context.UserId);
                        catch (Exception e)
                            Console.WriteLine("This is the error " + e.ToString());
                    else return;

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Athar SyedCommented:
The execute() function takes a parameter of type IServiceProvider, which you have not supplied in your test code.

The IServiceProvider parameter of the Execute method is a container for several service useful objects that can be accessed within a plug-in. The service provider contains instance references to the execution context, IOrganizationServiceFactory, ITracingService, and more.

Below is an example on MSDN on how to call the Execute method
Account target = new Account();
target.Name = "Fabrikam";
CreateRequest req = new CreateRequest();
req.Target = target;
req["SuppressDuplicateDetection"] = true;
req["CalculateMatchCodeSynchronously"] = true;
req["SolutionUniqueName"] = "MySolutionName";
CreateResponse response = (CreateResponse)_service.Execute(req);

Open in new window

REF: Use Messages with Execute method

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Dynamics

From novice to tech pro — start learning today.