locked
CRM 2011 C# Plugin: Creating Word Doc using OpenXML & Attaching the Document to Entity RRS feed

  • Question

  • I have created a plugin that is using OpenXML to create a Microsoft Word document. Originally I wrote the plugin to create and save the Word document to the user's C drive, but the plugin actually ended up saving the document on the CRM server's C drive. (Maybe you know how I can get it to save on the user's computer??? If so, THANK YOU!)

    So now I am trying to get the created document to attach to the entity I am triggering the Update plugin to run on. I used OpenXML and the Document Reflector to create the code that creates the Word. Now I am having trouble attach that created document to the entity. This is what I have wrote but it does not work:

            protected void ExecutePostAgencyAnalysisUpdate(LocalPluginContext localContext)
            {
                IPluginExecutionContext context = localContext.PluginExecutionContext;
                IOrganizationService service = localContext.OrganizationService;
                ITracingService tracingService = localContext.TracingService;
                
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }
    
                Guid AgencyAnalysisID = context.PrimaryEntityId;
    
                Entity ety = service.Retrieve("apd_agencyanalysis", AgencyAnalysisID, new ColumnSet(new string[] {"apd_agencycontactemail"}));
                
                string w = ety.LogicalName;
                    
                ///////////////////////////////
                GeneratedCode.GeneratedClass xmlFile = new GeneratedCode.GeneratedClass();
                xmlFile.CreatePackage(@"C:\SampleWord.doc");
                
                //string filename = "Agency Analysis.doc";
    
                FileStream stream = File.OpenRead(@"C:\SampleWord.doc");
                byte[] byteData = new byte[stream.Length];
                stream.Read(byteData, 0, byteData.Length);
                stream.Close();
    
                string encodedData = System.Convert.ToBase64String(byteData);
    
                Entity annotation = new Entity("annotation");
                annotation.Attributes["subject"] = "Agency Analysis Document";
                annotation.Attributes["notetext"] = "My note text";
    
                EntityReference noteRef = new EntityReference();
                noteRef.LogicalName = "apd_agencyanalysis";
                noteRef.Id = ety.Id;
                annotation.Attributes["documentbody"] = encodedData;
                annotation.Attributes["filename"] = "SampleWord.doc";
                annotation.Attributes["mimetype"] = @"application\ms-word";
                annotation.Attributes.Add("objectid", noteRef);
                annotation.Attributes.Add("objecttypecode", "apd_agencyanalysis");


    Mike Karls

    Thursday, September 18, 2014 2:40 PM

Answers

  • Here is the code that I got to work
                GeneratedCode.GeneratedClass xmlFile = new GeneratedCode.GeneratedClass();
                xmlFile.CreatePackage(@"C:\SampleWord.doc");
                
                //string filename = "Agency Analysis.doc";
                string f1 = @"C:\SampleWord.doc";
                
                FileStream stream = File.OpenRead(f1);
                byte[] byteData = new byte[stream.Length];
                stream.Read(byteData, 0, byteData.Length);
                stream.Close();
    
                string encodedData = System.Convert.ToBase64String(byteData);
    
                Entity annotation = new Entity("annotation");
                annotation.Attributes["subject"] = "Agency Analysis Document";
                annotation.Attributes["notetext"] = "My note text";
    
                EntityReference noteRef = new EntityReference();
                noteRef.LogicalName = "apd_agencyanalysis";
                noteRef.Id = ety.Id;
                annotation.Attributes["documentbody"] = encodedData;
                annotation.Attributes["filename"] = "SampleWord.doc";
                annotation.Attributes["mimetype"] = @"application\ms-word";
                annotation.Attributes.Add("objectid", noteRef);
                annotation.Attributes.Add("objecttypecode", "apd_agencyanalysis");
                service.Create(annotation);


    Mike Karls

    • Marked as answer by Mike Karls Friday, September 19, 2014 1:55 PM
    Friday, September 19, 2014 1:55 PM

All replies

  • The plugin says it succeeded but there is no file or entry in the Notes section on the entity.

    I think my problem is with these lines:

                GeneratedCode.GeneratedClass xmlFile = new GeneratedCode.GeneratedClass();
                xmlFile.CreatePackage(@"C:\SampleWord.doc");
                
                FileStream stream = File.OpenRead(@"C:\SampleWord.doc");

     

    Mike Karls

    Thursday, September 18, 2014 2:42 PM
  • mike,

    you are trying to access file system of your server from a plugin. This is not allowed by default.

    please can you explain why you have gone this path? Mail merge can get you crm fields into a word template.

    Jithesh

    Thursday, September 18, 2014 11:22 PM
  • Hi Mike,

    It is not a good way to access user C driver or something. As Jithesh says it is not allowed. Also In ur code you set the annotation fields but you do not create or update innotation and in youur entit  you did not give any entitreference for annotation. Thats why your doc is not attached to your entity.


    Polat Aydın Crm Software Developer

    Friday, September 19, 2014 12:12 AM
  • Jithesh,

    Ultimately I am looking to create a PowerPoint file from data in CRM. I was trying a Word file initially to make sure I could do that.


    Mike Karls

    Friday, September 19, 2014 1:27 PM
  • Here is the code that I got to work
                GeneratedCode.GeneratedClass xmlFile = new GeneratedCode.GeneratedClass();
                xmlFile.CreatePackage(@"C:\SampleWord.doc");
                
                //string filename = "Agency Analysis.doc";
                string f1 = @"C:\SampleWord.doc";
                
                FileStream stream = File.OpenRead(f1);
                byte[] byteData = new byte[stream.Length];
                stream.Read(byteData, 0, byteData.Length);
                stream.Close();
    
                string encodedData = System.Convert.ToBase64String(byteData);
    
                Entity annotation = new Entity("annotation");
                annotation.Attributes["subject"] = "Agency Analysis Document";
                annotation.Attributes["notetext"] = "My note text";
    
                EntityReference noteRef = new EntityReference();
                noteRef.LogicalName = "apd_agencyanalysis";
                noteRef.Id = ety.Id;
                annotation.Attributes["documentbody"] = encodedData;
                annotation.Attributes["filename"] = "SampleWord.doc";
                annotation.Attributes["mimetype"] = @"application\ms-word";
                annotation.Attributes.Add("objectid", noteRef);
                annotation.Attributes.Add("objecttypecode", "apd_agencyanalysis");
                service.Create(annotation);


    Mike Karls

    • Marked as answer by Mike Karls Friday, September 19, 2014 1:55 PM
    Friday, September 19, 2014 1:55 PM
  • thank you mike for position the final solution.

    good to hear that it is working.

    you may have concurrency issues if you use same filename from plugin. Please append timestamp or something.😊

    regards

    Saturday, September 20, 2014 12:11 AM