CRM 2011- Update web resource by code

I am implementing a new Solution. I need to update a Web Resource XML when an user clicks on Save button. The solution is simple. First of all I put inside the app configuration the path to the XML file on file system (ProjectsXMLPath) and its Guid of Web Resource from solution (ProjectsXMLPathXMLGUID). I know the Guid because Web Resource is part of solution and it’s id is unchangeable.

// read parameters from config file
string projectSourcePath = ConfigurationManager.AppSettings["ProjectsXMLPath"];
Guid projectsGuid = Guid.Parse(ConfigurationManager.AppSettings["ProjectsXMLPathXMLGUID"]);

// Get config content
byte[] byt = File.ReadAllBytes(projectSourcePath );
using (var xrm = new XrmServiceContext(_serviceProxy))
{
  WebResource webResource = xrm.WebResourceSet.FirstOrDefault(w => w.Id == projectsGuid );

  if (webResource == null)
  {
    string error= string.Format("web resource with ID: {0} doesn't exist in Projects Solution.", projectsGuid );
    throw new Exception(error);
  }

  // convert the byte array to a Base64 string - and update web resource content
  webResource .Content = Convert.ToBase64String(byt);

  xrm.UpdateObject(webResource);
  xrm.SaveChanges();
}

Now Web Resource Content is updated but it is not published. I need to publish the content with this command:

PublishXmlRequest publishRequest = new PublishXmlRequest();

publishRequest.ParameterXml =
  "" +
  "   " +
  "       {" + projectsGuid + "}" +
  "   " +
  "";

xrm.Execute(publishRequest);

It’s important to know this way because when we want to update a WebResource with license during thw setup, this is a fast way to do.

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...