Upload or Download an Attachment to a Note

To upload or download an attachment, you must use an HTML page. This HTML page must exist on a server in the same domain as the Microsoft CRM server. You can use this method with a note, an activity attachment, or a sales literature item. This example is for the note class. Once you have completed this method sample, see the following Results section.

Class Reference

Schema Reference

  • annotation.xsd

Example

[C#]

// strServerName should be set with the name of the platform Web server
String strServerName = "myservername";

// strVirtualDirectory should be set with the name of the Microsoft CRM
// virtual directory on the platform Web server
String strVirtualDirectory = "mscrmservices";
String strDir = String.Concat("https://", strServerName, "/",
                              strVirtualDirectory, "/");

// BizUser proxy object
Microsoft.Crm.Platform.Proxy.BizUser bizUser 
               = new Microsoft.Crm.Platform.Proxy.BizUser ();
bizUser.Credentials = System.Net.CredentialCache.DefaultCredentials;
bizUser.Url = String.Concat(strDir, "BizUser.srf");

// CRMNotesManager proxy object
Microsoft.Crm.Platform.Proxy.CRMNotesManager  notesManager 
               = new Microsoft.Crm.Platform.Proxy.CRMNotesManager();
notesManager.Credentials = System.Net.CredentialCache.DefaultCredentials;
notesManager.Url = String.Concat(strDir, "CRMNotesManager.srf");

String strErrorMsg;
try
{
      Microsoft.Crm.Platform.Proxy.CUserAuth userAuth = bizUser.WhoAmI();

   // Set up the XML string for NotesManager
   StringBuilder notesManagerXml = new StringBuilder("<annotation>");
   notesManagerXml.Append("<subject>Sample Note</subject>");
   notesManagerXml.Append("<isdocument>0</isdocument>");
   notesManagerXml.Append("<notetext>Here is the note text</notetext>");
   notesManagerXml.Append("<ownerid type=\"");
   notesManagerXml.Append
     (Microsoft.Crm.Platform.Types.ObjectType.otSystemUser.ToString());
   notesManagerXml.Append("\">");
   notesManagerXml.Append(userAuth.UserId);
   notesManagerXml.Append("</ownerid>");
   notesManagerXml.Append("</annotation>");

   // Now create the note
   String strNoteId = notesManager.Create(userAuth,
                                          notesManagerXml.ToString());

   // This is the ID of a contact that already exists
   String strContactId = "{E7B5C6F8-9A0B-4049-897B-E5852673D98E}";

   Microsoft.Crm.Platform.Proxy.CObjectName objName 
               = new Microsoft.Crm.Platform.Proxy.CObjectName();
   objName.Id = strContactId;
   objName.Type = Microsoft.Crm.Platform.Proxy.ObjectType.otContact;

   // Attach the note to a contact
   notesmanager.SetParent(userAuth, strNoteId, objName);

}
catch (System.Web.Services.Protocols.SoapException err)
{
   // Process the platform error here
   strErrorMsg = String.Concat("ErrorMessage: ", err.Message, " ",
                   err.Detail.OuterXml, " Source: ", err.Source);
}

[HTML]
NoteUpload.htm
<html>
   <head>
      <title>Note Upload</title></head>
   <body alink="#0000ff" vlink="#0000ff" bgcolor="#ffffff">
      <form name="Attachments" enctype="multipart/form-data" method="POST"
                action="https://myservername/MSCRMSERVICES/NoteUpload.srf">
         <input type="hidden" name="AttachmentType" value="5">
         <input type="hidden" name="AttachmentId"
                value="{192D46FF-1F84-4F74-AE74-BABC3E4AC70B}">
         <input type="hidden" name="UserId"
                value="{78861AAC-6104-4283-B303-0C861108FE26}">
         <input type="hidden" name="MerchantId"
                value="{C59799EE-412C-4096-B60C-FA1AD13AA4CB}">
         <input type="hidden" name="ErrorURL"
                value="https://myservername/AttachmentHelpers/error.htm">
         <input type="hidden" name="SuccessURL"
                value="https://myservername/AttachmentHelpers/NoteDownload.htm">
         <input type="File" name="userFile" size="20">
         <input type="submit" name="UPL" value="Submit">
   </body>
</html>

NoteDownload.htm
<html>
   <head>
      <title>Note Download</title></head>
   <body alink="#0000ff" vlink="#0000ff" bgcolor="#ffffff">
      <A HREF="https://myservername/MSCRMSERVICES/NoteDownload.srf?AttachmentType=5&
         AttachmentId=%7B192D46FF-1F84-4F74-AE74-BABC3E4AC70B%7D&
         UserId=%7B78861AAC-6104-4283-B303-0C861108FE26%7D&
         MerchantId=%7BC59799EE-412C-4096-B60C-FA1AD13AA4CB%7D&
         ErrorURL=https://myservername/AttachmentHelpers/error.htm&
         FileName=NoteFile.txt">
         Download!</A>
   </body>
</html>

Results

When you click the Submit button in the form, the HTML page will try to upload the file. If there is an error, it will go to the URL specified in the tag named ErrorUrl. A query string will be added to the end of this file such as:

https://<servername>/AttachmentHelpers/error.htm&hr=80040217

If there is no error, the HTML page will go to the URL specified in the tag named SuccessURL.

© 2005 Microsoft Corporation. All rights reserved.