web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Most likely problems with "CreateAnnotation" method from a "CrmServiceClient" connection object

(0) ShareShare
ReportReport
Posted on by 105

This is a tough one for me to ask because I'm not getting any errors anymore.  But the process doesn't work.  I'll do my best to explain.

I create a working CrmServiceClient object.  I know it works because I can run queries through it successfully.  I use the "CreateAnnotation" method on that connection object with the intention of attaching a file to a specific account.  The parameters for the method call...

  • string targetEntityTypeName: set to "account"
  • Guid targetEntityId: set to the Guid of the account that I looked up.  I've verified that it's the right value
  • Dictionary<string, CrmDataTypeWrapper> fieldList: set to a dictionary I build based on values I get from the file I'm testing with.  I've tried a few combinations of the following list of dictionary entries...
    • key: "filename" / value: "testfile.docx" (string)
    • key: "mimetype" / value: "application/vnd.openxmlformats-officedocument.wordprocessingml.document" (string)
    • key: "filesize" / value: (an integer matching the file size in bytes, sometimes skipped in my testing)
    • key: "documentbody" / value: (a string representation of the byte array because it wouldn't accept the byte array)
    • key: "subject" / value: "Data Migration File Test" (string)
    • key: "notetext" / value: "This file was migrated from the old CRM instance during our consolidation effort" (string)
    • key: "isdocument" / value: true (boolean, sometimes skipped in my testing)

The return value of the method is a Guid for the newly created Note.  What I get back is the empty GUID (Guid.empty, or all zeroes).  And when I look in our sandbox at the account, there's no new note.  I've verified that my connection in my code is to the same sandbox.  I've verified that the account I'm using has the ability to add a note (by manually adding a note with an attached file).  And oddly enough in the "OneNote" tab of the Account page, I can see that I had some activity, though I don't know how that might be related.  When I run the code, it executes without exception, and just comes back with that empty GUID, and there's no new note on the account.

I'm realizing that the first question here is going to be for me to post the code.  Here goes...  I'll try to trim it to just what's relevant...

Getting the File data...

FileName = Path.GetFileName(SourceFilePath);
string fileExt = Path.GetExtension(SourceFilePath);
FileMimeType = FileUtility.GetMimeType(fileExt); //utility method I wrote to get the mime type
FileSizeInBytesLong = new FileInfo(SourceFilePath).Length;
FileContents = File.ReadAllBytes(SourceFilePath);

 

Build the dictionary to be used in the method call...

retVal.Add(FileNameKV.Key, new CrmDataTypeWrapper(FileNameKV.Value, CrmFieldType.String));
retVal.Add(FileMimeTypeKV.Key, new CrmDataTypeWrapper(FileMimeTypeKV.Value, CrmFieldType.String));
//retVal.Add(FileSizeInBytesKV.Key, new CrmDataTypeWrapper(FileSizeInBytesKV.Value, CrmFieldType.CrmNumber));
retVal.Add(FileContentsStringKV.Key, new CrmDataTypeWrapper(FileContentsStringKV.Value, CrmFieldType.String));
//retVal.Add(IsDocumentKV.Key, new CrmDataTypeWrapper(IsDocumentKV.Value, CrmFieldType.CrmBoolean));

if (!string.IsNullOrEmpty(SubjectLine)) { retVal.Add(SubjectLineKV.Key, new CrmDataTypeWrapper(SubjectLineKV.Value, CrmFieldType.String)); }
else { retVal.Add(SubjectLineKV.Key, new CrmDataTypeWrapper("File Attachment", CrmFieldType.String)); } //required field

if (!string.IsNullOrEmpty(Description)) { retVal.Add(DescriptionKV.Key, new CrmDataTypeWrapper(DescriptionKV.Value, CrmFieldType.String)); }
else { retVal.Add(DescriptionKV.Key, new CrmDataTypeWrapper("File Attachment from migration process", CrmFieldType.String)); }


File info...

It's a Docx file that's 298K

The Question...

So what are some of the possible explanations for why the "CreateAnnotation" method would fail in this way?  What would stop the note from being added?

*This post is locked for comments

I have the same question (0)
  • Verified answer
    David Jennaway Profile Picture
    14,065 on at

    The documentbody should be a Base64 string - you can get this from the byte[] using the System.Convert.ToBase64String Method.

    Also, have you checked the LastCrmError or LastCrmException properties of the CrmServiceClient instance ?

    It might help to also post the code that calls the Create

  • Kevin T 9 Profile Picture
    105 on at

    WOO HOO!!

    The base64 string suggestion was the answer.  One tiny change from...

       Encoding.Default.GetString

    ... to...

       Convert.ToBase64String

    ... solved the problem.  Thank you very much David.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans