2

I'm a crm newbie so forgive my misunderstandings or misadventures.

I'm trying to programmatically create a webresource (javascript or jscript to be precise) early bound using the OrganizationServiceproxy like this

          var context = new OrganizationServiceContext(service);

          var resource = (from wr in context.CreateQuery<WebResource>()
                          where wr.Name == name && wr.ComponentState.Value == 0
                          select wr).FirstOrDefault();

                if (resource == null)
                {
                  WebResource javascriptWebResource = new WebResource()
                        {
                            Name = name,
                            Description = name,
                            LogicalName = name,
                            DisplayName = value,
                            Content = Convert.ToBase64String(fileBytes),
                            WebResourceType = new OptionSetValue(3)

                        };
                    //context.AddObject(javascriptWebResource);
                    //context.SaveChanges();
                     service.Create(javascriptWebResource);
                }
                else
                {
                 //update the webresource
                 }

My question is- do I need to set more Entity Metadata than what I'm currently setting to successfully create the web resource?

The creation code is not throwing any errors however I cannot find my newly created javascript webresource on the crm server in the specified solution. I guessed it was adding web resource to the default solution therefore I scoured the web and came across a sample in the sdk which goes like this

  Guid theGuid = _serviceProxy.Create(wr);

                //If not the "Default Solution", create a SolutionComponent to assure it gets
                //associated with the ActiveSolution. Web Resources are automatically added
                //as SolutionComponents to the Default Solution.
                if (ActiveSolution.UniqueName != "Default")
                {
                    AddSolutionComponentRequest scRequest = new AddSolutionComponentRequest();
                    scRequest.ComponentType = (int)componenttype.WebResource;
                    scRequest.SolutionUniqueName = ActiveSolution.UniqueName;
                    scRequest.ComponentId = theGuid;
                    var response = (AddSolutionComponentResponse)_serviceProxy.Execute(scRequest);
                }

My question is - if I retrieve the solutionuniquename then will it create the web resource in the appropriate solution and I would be able to see the javascript web resource on the crm server?

Thanks in advance for your help.

Greg Owens
  • 3,878
  • 1
  • 18
  • 42
Hamza Ahmed
  • 1,571
  • 4
  • 18
  • 35
  • Yes you would indeed see the created webresource inside the solution you added it to. – Piyush Aug 29 '12 at 12:32
  • I'm unclear now - are you saying that your Web Resource *is* being created, but it is in the Default Solution and you want it in a specified solution? Or is the code failing to create anything at all (you suspect)? – Greg Owens Aug 29 '12 at 12:37
  • @Greg, you guessed right. The web resource is being created and it is in the default solution. I also succeeded in adding it to the appropriate solution (hint:- use unique name of solution). Now my requirement is different. Programmatically I must detect the existence of a predefined solution, create it if it does not exists and add the javascript web resource to that solution. If the predefined solution already exists then I must update that solution with this js web resource either by creating the js web resource if it does not exists or by updating it if it already exists. Any guidance? – Hamza Ahmed Sep 03 '12 at 05:32

1 Answers1

0

I know this is rather late but I solved it by sending a createrequest if the js does not exist in the crm system or by associating the js to a particular solution if the js already exists. By the way when you create a js in crm it gets added to both the default solution and the solution you want to update.

Hamza Ahmed
  • 1,571
  • 4
  • 18
  • 35