MS CRM stores attachments directly in the database which allows security to be enforced on attachments. If you delete a entity, all of its annotations are removed with it. By way of comparison this offers a lot more control than just saving a pathname for an attached file like GoldMine does with its RFC822 format emails.
In MS CRM 4.0 the attachments are stored in a text field in the ActivityMimeAttachment table, and in an nvarchar(max) in the AnnotationBase table. You can use SQL "like" comparator in a where clause against an nvarchar but not for a text field. nvarchar(max) was introduced with SQL 2005.
Creating an activitymimeattachment ( Email attachment )
The following is a code snippet from a larger method that creates the email entity as well. This code is inside a try catch for the email creation.
Notice that a file can be converted into a format that can be saved into the activitymimeattachment.body attribute by using the File.ReadAllBytes() and the Convert.ToBase64String() methods.
Creating an annotation (Notes attachment)
The following code creates an annotation which can belong to any entity with a Notes tab. In this case it is a Letter activity. This is very similar to the above code for the activitymimeattachment. One key difference is that an annotation can also just be a Note, so it has an attribute just to specify if it is a note or an attachment.
Notice the isdocument attribute of the annotation. This needs to be set to true for an attachment. Also notice the overriddencreatedon attribute. The addition of this is one of the many improvements in MS CRM 4.0 and removed one of my pet peeves about MS CRM 3.0. When importing annotations in MS CRM 3.0 all of the annotations would be set to the import date which is useless for the end user. The ONLY way to change it was to go back with a SQL Update to change the createdon date. Now we have the overriddencreatedon attribute so that we don't have to "break the rules" and access the data tables directly.
The following method is a helper to look up a mimetype for a given file extension.
MS CRM 4.0 Attachment Size Limitation
MS CRM stores attachments directly in the database, and there is a 5Mb attachment limit by default with CRM 4.0. This limit is set for good reason, and if you want to increase it, you may want to see if it starts to impact your system performance.
This can be overwritten by doing the following:
- Changing the maxRequestLength value in the main CRM web.config file. By default it is 8Mb which doesn't impact the System Setting below. However if you increase the Maximum File size above the maxRequestLength you will have problems.
<httpRuntime executionTimeout="300" maxRequestLength="8192"/>
There are many examples on the web that show something like the following which pretty well removes any size constraints from the web.config.
<httpRuntime executionTimeout="9999" maxRequestLength="99999"/>
- Save the web.config and restart IIS.
- For MS CRM 4.0 that just leaves adjusting the Maximum file size in System Settings with default of 5,120 Kb shown below to something higher but less than the maxRequestLength.
Alternate 3. In MS CRM 3.0 there is a registry setting that needs to be set instead. In HKEY_Local_Machine\Software\Microsoft\MSCRM
the maxuploadfilesize value can be changed from it's 5Mb default.