Quantcast
Channel: Forum CRM Development
Viewing all articles
Browse latest Browse all 1000

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

$
0
0

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


Viewing all articles
Browse latest Browse all 1000

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>