The following code example demonstrates how to upload an attachment to a note programmatically.
Example
The following code example shows how to upload attachment to an annotation entity instance.
The code first creates an account entity instance, and then adds an attached note to it with a doc file as an attachment.
1:
2: using System;
3: using System.Collections.Generic;
4: using System.Text;
5: using System.IO;
6: using Microsoft.Crm.Sdk.Utility;
7:
8: namespace Microsoft.Crm.Sdk.HowTo
9: {
10: // Use the following Microsoft Dynamics CRM namespaces in the sample.
11: using CrmSdk;
12:
13: class UploadAttachment
14: {
15: static void Main(string[] args)
16: {
17: // TODO: Change the server URL and organization to match your
18: // Microsoft Dynamics CRM Server and Microsoft Dynamics CRM Organization.
19: UploadAttachment.Run("http://localhost:5555", "CRM_Organization");
20: }
21:
22: public static bool Run(string crmServerUrl, string orgName)
23: {
24: #region Setup Data Required for this Sample
25:
26: bool success = false;
27:
28: #endregion
29:
30: try
31: {
32: // Set up the CRM Service.
33: CrmService service = CrmServiceUtility.GetCrmService(crmServerUrl, orgName);
34: service.PreAuthenticate = true;
35:
36: // Create an account object.
37: account acct = new account();
38:
39: // Set the account properties.
40: acct.accountnumber = "CRM102";
41: acct.name = "Fourth Coffee";
42: acct.address1_name = "Primary";
43: acct.address1_line1 = "1234 Elm Street";
44: acct.address1_city = "Redmond";
45: acct.address1_stateorprovince = "WA";
46: acct.address1_postalcode = "54199";
47:
48: // Creates an account in the Crm.
49: Guid createdAccountId = service.Create(acct);
50:
51: // Now create the annotation object.
52: annotation note = new annotation();
53: note.notetext = "This is a sample note";
54: note.subject = "Test Subject";
55: note.objectid = new Lookup();
56: note.objectid.type = EntityName.account.ToString();
57:
58: // Sets the note's parent to the newly created account.
59: note.objectid.Value = createdAccountId;
60: note.objecttypecode = new EntityNameReference();
61: note.objecttypecode.Value = EntityName.account.ToString();
62:
63: // Create the note.
64: Guid createdNoteId = service.Create(note);
65:
66: #region Setup Additional Data Required for this Sample
67:
68: // Now convert the attachment to be uploaded to Base64 string.
69: // This will create a doc file in the current folder of executable.
70: string currentPath = System.Environment.CurrentDirectory.ToString();
71: TextWriter tw = new StreamWriter(currentPath + "\\Crm" + createdNoteId.ToString() + ".doc");
72: // Write a line of text to the file.
73: tw.WriteLine("This document is for testing an attachment upload feature of CRM 4.0.");
74: tw.Close();
75:
76: #endregion
77:
78: // Open a file and read the contents into a byte array.
79: FileStream stream = File.OpenRead(currentPath + "\\Crm" + createdNoteId.ToString() + ".doc");
80: byte[] byteData = new byte[stream.Length];
81: stream.Read(byteData, 0, byteData.Length);
82: stream.Close();
83:
84: // Encode the data using base64.
85: string encodedData = System.Convert.ToBase64String(byteData);
86:
87: // Now update the note.
88: annotation updateNote = new annotation();
89: updateNote.annotationid = new Key();
90: // Set the Note ID that is being attached to.
91: updateNote.annotationid.Value = createdNoteId;
92: updateNote.documentbody = encodedData;
93: updateNote.filename = "Crm" + createdNoteId.ToString() + ".doc";
94: updateNote.mimetype = @"application\ms-word";
95: service.Update(updateNote);
96:
97: #region check success
98:
99: if (createdNoteId != Guid.Empty)
100: {
101: success = true;
102: }
103:
104: #endregion
105:
106:
107: #region Remove Data Required for this Sample
108:
109: if (createdNoteId != Guid.Empty)
110: service.Delete(EntityName.annotation.ToString(), createdNoteId);
111:
112: if(createdAccountId != Guid.Empty)
113: service.Delete(EntityName.account.ToString(), createdAccountId);
114:
115: if (File.Exists(currentPath + "\\Crm" + createdNoteId.ToString() + ".doc"))
116: File.Delete(currentPath+"\\Crm" + createdNoteId.ToString() + ".doc");
117:
118: #endregion
119:
120: }
121: catch (System.Web.Services.Protocols.SoapException err)
122: {
123: string strError = String.Format("An error occurred. The message is {0}. The detail is {1}.", err.Message, err.Detail.OuterXml.ToString());
124: }
125:
126:
127: return success;
128: }
129: }
130: }
No comments:
Post a Comment