private WebResource CreateWebResource(string fileName, OrganizationService orgService, string filePath) { var createWebresoruce = new CreateWebResourceWindow(fileName); createWebresoruce.ShowDialog(); if (createWebresoruce.CreatedWebResource == null) return null; var createdWebresource = createWebresoruce.CreatedWebResource; createdWebresource.Content = GetEncodedFileContents(filePath); createdWebresource.Id = orgService.Create(createdWebresource); return createdWebresource; }
private void UpdateTheWebresource(string selectedFilePath, string connectionString) { try { OrganizationService orgService; var crmConnection = CrmConnection.Parse(connectionString); //to escape "another assembly" exception crmConnection.ProxyTypesAssembly = Assembly.GetExecutingAssembly(); using (orgService = new OrganizationService(crmConnection)) { var isCreateRequest = false; var fileName = Path.GetFileName(selectedFilePath); var choosenWebresource = GetWebresource(orgService, fileName); if (choosenWebresource == null) { AddErrorLineToOutputWindow("Error : Selected file is not exist in CRM."); AddLineToOutputWindow("Creating new webresource.."); var createWebresoruce = new CreateWebResourceWindow(fileName); createWebresoruce.ShowDialog(); if (createWebresoruce.CreatedWebResource == null) { AddLineToOutputWindow("Creating new webresource is cancelled."); return; } isCreateRequest = true; choosenWebresource = createWebresoruce.CreatedWebResource; } choosenWebresource.Content = GetEncodedFileContents(selectedFilePath); if (isCreateRequest) { //create function returns, created webresource's guid. choosenWebresource.Id = orgService.Create(choosenWebresource); AddLineToOutputWindow("Webresource is created."); } else { AddLineToOutputWindow("Updating to Webresource.."); var updateRequest = new UpdateRequest { Target = choosenWebresource }; orgService.Execute(updateRequest); AddLineToOutputWindow("Webresource is updated."); } AddLineToOutputWindow("Publishing the webresource.."); var prequest = new PublishXmlRequest { ParameterXml = string.Format("<importexportxml><webresources><webresource>{0}</webresource></webresources></importexportxml>", choosenWebresource.Id) }; orgService.Execute(prequest); AddLineToOutputWindow("Webresource is published."); } _myStopwatch.Stop(); AddLineToOutputWindow(string.Format("Time : " + _myStopwatch.Elapsed)); } catch (Exception ex) { AddErrorLineToOutputWindow("Error : " + ex.Message); } }