web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :

CRM 2011- Update web resource by code

Alessandro Graps Profile Picture Alessandro Graps 2,664

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.


Archiviato in:Microsoft Dynamics Crm Tagged: .Net, C#, CRM, Microsoft Dynamics CRM

This was originally posted here.

Comments

*This post is locked for comments