Answered by:
Q: How to retrieve notes from a custom entity?

Question
-
New to CRM 4.0, but using the SDK. I can use the CRMService and retrieve fields from the entities. Now I need to retrieve notes from a custom entity, in the notes are also some attachments like images/documents that I need to download to my website I'm building. How can I do this?
Brian
Thursday, May 20, 2010 3:09 PM
Answers
-
Entity for notes in MS CRM is annotation. if you want to reterive all the notes against an entity use QueryByAttribute. see below.
http://blogs.javista.com/2009/12/22/tips-tricks-displaying-contact-images-in-microsoft-dynamics-crm/
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = "AdventureWorksCycle";
CrmService service = new CrmService();
service.Url = ""http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
service.CrmAuthenticationTokenValue = token;
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Create a column set holding the names of the columns to be retrieved.
ColumnSet cols = new ColumnSet();
cols.Attributes = new string [] {"notetext", "documentbody"};
// Create the query object.
QueryByAttribute query = new QueryByAttribute();
query.ColumnSet = cols;
query.EntityName = EntityName.annotation.ToString();Guid yourCustomIdGuid = new GUid("Guid of your custom entity");
String objecttypecode = "object type code of your custom entity";
query.Attributes = new string [] {"objectId" , "objecttypecode"};
query.Values = new object[] {yourCustomIdGuid, objecttypecode };
// Execute the retrieval.
BusinessEntityCollection retrieved = service.RetrieveMultiple(query);Download Attachment from annotation
http://msdn.microsoft.com/en-us/library/cc151179.aspx
#region How to download attachment from annotation record
// SDK: annotationId = new Guid("{bee08735-09d3-de11-9d71-00155da4c706}");
Guid annotationId = setupAnnotationId;
// Define the columns to retrieve from the annotation record.
ColumnSet cols1 = new ColumnSet();
cols1.Attributes = new string[] { "filename", "documentbody" };
// Retrieve the annotation record.
annotation annotationAttachment = (annotation)service.Retrieve(EntityName.annotation.ToString(), annotationId, cols1);
// Download the attachment in the current execution folder.
using (FileStream fileStream = new FileStream(annotationAttachment.filename, FileMode.OpenOrCreate))
{
byte[] fileContent = new UTF8Encoding(true).GetBytes(annotationAttachment.documentbody);
fileStream.Write(fileContent, 0, fileContent.Length);
}
Muhammad Ali Khan
http://malikhan.wordpress.com- Proposed as answer by Muhammad Ali Khan Thursday, May 20, 2010 3:32 PM
- Marked as answer by Jim Glass Jr Thursday, May 20, 2010 4:46 PM
Thursday, May 20, 2010 3:31 PM
All replies
-
Replace <RegardingObjectID> with the entity record that you want to retrieve notes for and this query should work.
Guid regardingObjectId = new Guid("<RegardingObjectID>");
QueryExpression query = new QueryExpression();
query.EntityName = "annotation";
query.ColumnSet = new AllColumns();
query.Criteria = new FilterExpression();
query.Criteria.FilterOperator = LogicalOperator.And;
ConditionExpression condition1 = new ConditionExpression();
condition1.AttributeName = "objectid";
condition1.Operator = ConditionOperator.Equal;
condition1.Values = new Object[] { regardingObjectId };
query.Criteria.Conditions = new ConditionExpression[] {
condition1};
crmService.RetrieveMultiple(query);
Blake Scarlavai - http://mscrmdev.blogspot.com/ - Sonoma Partners - http://www.sonomapartners.com/Thursday, May 20, 2010 3:30 PM -
Entity for notes in MS CRM is annotation. if you want to reterive all the notes against an entity use QueryByAttribute. see below.
http://blogs.javista.com/2009/12/22/tips-tricks-displaying-contact-images-in-microsoft-dynamics-crm/
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = "AdventureWorksCycle";
CrmService service = new CrmService();
service.Url = ""http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
service.CrmAuthenticationTokenValue = token;
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Create a column set holding the names of the columns to be retrieved.
ColumnSet cols = new ColumnSet();
cols.Attributes = new string [] {"notetext", "documentbody"};
// Create the query object.
QueryByAttribute query = new QueryByAttribute();
query.ColumnSet = cols;
query.EntityName = EntityName.annotation.ToString();Guid yourCustomIdGuid = new GUid("Guid of your custom entity");
String objecttypecode = "object type code of your custom entity";
query.Attributes = new string [] {"objectId" , "objecttypecode"};
query.Values = new object[] {yourCustomIdGuid, objecttypecode };
// Execute the retrieval.
BusinessEntityCollection retrieved = service.RetrieveMultiple(query);Download Attachment from annotation
http://msdn.microsoft.com/en-us/library/cc151179.aspx
#region How to download attachment from annotation record
// SDK: annotationId = new Guid("{bee08735-09d3-de11-9d71-00155da4c706}");
Guid annotationId = setupAnnotationId;
// Define the columns to retrieve from the annotation record.
ColumnSet cols1 = new ColumnSet();
cols1.Attributes = new string[] { "filename", "documentbody" };
// Retrieve the annotation record.
annotation annotationAttachment = (annotation)service.Retrieve(EntityName.annotation.ToString(), annotationId, cols1);
// Download the attachment in the current execution folder.
using (FileStream fileStream = new FileStream(annotationAttachment.filename, FileMode.OpenOrCreate))
{
byte[] fileContent = new UTF8Encoding(true).GetBytes(annotationAttachment.documentbody);
fileStream.Write(fileContent, 0, fileContent.Length);
}
Muhammad Ali Khan
http://malikhan.wordpress.com- Proposed as answer by Muhammad Ali Khan Thursday, May 20, 2010 3:32 PM
- Marked as answer by Jim Glass Jr Thursday, May 20, 2010 4:46 PM
Thursday, May 20, 2010 3:31 PM -
I would highly recommend you download the latest CRM SDK (4.0.0012) as it will make your job significantly easier. Also take a look at the latest Customer Portal Accelerator on codeplex - it has examples of how to download attachments on your website.
To give you some perspective on the new Microsoft.Xrm SDK way of querying from notes, here is the code using the new LINQ-to-CRM provider included in the SDK: Note that this code finds a contact by my name, then loops through the available notes. Notice how much simpler and straight-forward this is!
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Xrm; using Microsoft.Xrm.Client; namespace Mitch { class Program { static void Main(string[] args) { var crm = new XrmDataContext(CrmConnection.Parse("Authentication Type=Integrated; Server=http://devcrm/Shan")); var shan = (from c in crm.contacts where c.firstname == "Shan" && c.lastname == "McArthur" select c).FirstOrDefault(); if (shan != null) { foreach (var note in shan.Contact_Annotation) { Console.WriteLine(note.notetext); } } } } }
Shan McArthur
www.shanmcarthur.netThursday, May 20, 2010 5:19 PMModerator