locked
Not able to create attachment using code in crm 2011 RRS feed

  • Question

  • FileStream stream = File.OpenRead(path);
                        byte[] byteData = new byte[stream.Length];
                        stream.Read(byteData, 0, byteData.Length);
                        stream.Close();
                        string encodedData = System.Convert.ToBase64String(byteData);
                        Entity annotation = new Entity("annotation");
                        annotation.Attributes["subject"] = "Profile";
                        annotation.Attributes["notetext"] = "Resume Attached";                    annotation.Attributes["filename"] = name;
                        annotation.Attributes["mimetype"] = "application/ms-word";
                        EntityReference noteRef = new EntityReference();
                        noteRef.LogicalName = _entnm;
                        noteRef.Id = _entityId;
                        annotation.Attributes.Add("objectid", noteRef);
                        annotation.Attributes.Add("objecttypecode", _entnm);
                        annotation.Attributes.Add("documentbody", encodedData);
                        _service.Create(annotation);

    Hello,

    I'm using above code to create an annotation in crm 2011. Everything is working fine but only documentbody is not getting added to the annotation. I mean, after running the above code , its showing the note text, name, subject everything except the file which has to be created in documentbody. Please help me know where i'm doing mistake.


    Madhu Sudhan.M


    • Edited by Madhu-CRM Monday, October 29, 2012 11:08 AM
    Monday, October 29, 2012 10:32 AM

Answers

  • Hi,

    I tested your code and it works without a problem. So, I'm guessing that the problem lies within the file you are trying to attach. Please try to attach another word file (for example a new word file where you only type one word like "test") and see if it works.

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.
    View my latest gallery contribution here.
    Visit my blog here.


    Tuesday, October 30, 2012 7:57 AM

All replies

  • Hi,

    You can check with the below given code:-

    Entity annotation = new Entity("annotation");
    annotation.Attributes.Add("subject", "Profile");
    annotation.Attributes.Add("filename", "ResumeAttached.doc");
    annotation.Attributes.Add("body", Convert.ToBase64String(result));
    annotation .Attributes.Add("mimetype", "text/plain");
    annotation.Attributes.Add("objectid", new EntityReference("email", _emailId));
    annotation.Attributes.Add("objecttypecode", objecttypecode);
    //annotation.Attributes.Add("objectid", new EntityReference("email", new Guid(email.Id.ToString())));
    _service.Create(annotation);

    I have added an attachment in email entity. You can do it for your entity.

    Hope this helps,

    Thanks,

    Prasad


    Monday, October 29, 2012 12:10 PM
  • Please see this.
    Monday, October 29, 2012 12:38 PM
  • Thanks for your reply prasad ,

    I'm trying to create annotation in an entity. My task is to attach a word document using code. When i debug the above code, the values are showing in attributes of annotation but still file will not get attached.


    Madhu Sudhan.M

    Tuesday, October 30, 2012 7:19 AM
  • Thanks for your reply Dhaval ,

    I'm trying to create annotation in an entity. My task is to attach a word document using code. When i debug the above code, the values are showing in attributes of annotation but still file will not get attached.


    Madhu Sudhan.M

    Tuesday, October 30, 2012 7:19 AM
  • Hi,

    I tested your code and it works without a problem. So, I'm guessing that the problem lies within the file you are trying to attach. Please try to attach another word file (for example a new word file where you only type one word like "test") and see if it works.

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.
    View my latest gallery contribution here.
    Visit my blog here.


    Tuesday, October 30, 2012 7:57 AM
  • Thank you Pavlos,

    I changed the file and it worked. I was creating word document from html. May the conversion format is wrong i guess. I'll check for other method for conversion.


    Madhu Sudhan.M

    Tuesday, October 30, 2012 9:31 AM
  • Hi ALL;

    how to attach a pdf file through javascript to an email

    this my code but it is not working 

     var params = getReportingSession();
                var post = Object();
                post.DocumentBody = encodePdf(params);---encodign the pdf
                post.Subject = "File Attachment";
                post.FileName = "Report.pdf";
                post.MimeType = "application/pdf";
                post.ObjectId = Object();
                post.ObjectId.LogicalName = "email";
                post.ObjectId.Id = emailid;
                post.Objecttypecode = "email";

    that code is working file with the attahcment of the entity but not for emails why is that 

    Thursday, November 15, 2012 12:49 PM
  • Hi,

    emails have another attachement enity than other entities. Other entities use the "Annotation" entity for file attachments. The email entity uses the "ActivityMimeAttachment" entity. Take a look at this for more info.

    For example, in your code you are using the "DocumentBody" property, but this property does not exist in "ActivityMimeAttachment". The equivalent to "DocumentBody" is "Body" for email attachments. Please also take a look at your other attributes to make sure they exist in the "ActivityMimeAttachment" entity.

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.
    View my latest gallery contribution here.
    Visit my blog here.

    Thursday, November 15, 2012 12:57 PM
  • Hi Pavlos;

    i did it but still not working for some reason

    var post = Object();
                post.Body = encodePdf(params);
                post.Subject = "File Attachment";
                post.FileName = "Report.pdf";
                post.MimeType = "application/pdf";
                post.ObjectId = Object();
                post.ObjectId.LogicalName = "email";
                post.ObjectId.Id = "0C1831B4-212F-E221-8B35-984BE17C2819";
                post.Objecttypecode = "email";



                createEntity(post, "ActivityMimeAttachment", "");

    Thursday, November 15, 2012 1:26 PM
  • Hi,

    please use Fiddler to check out the complete error response and paste the error message here.

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.
    View my latest gallery contribution here.
    Visit my blog here.

    Thursday, November 15, 2012 1:56 PM
  • Hi;

    the problem is that i am not getting any error ..but i can not see the attachment with the email??

    Thursday, November 15, 2012 2:01 PM
  • Hi,

    you also have a typo in your code. It should be "ObjectTypeCode" instead of "Objecttypecode". And also try using:

    post.ObjectId = {LogicalName: "email", Id:"0C1831B4-212F-E221-8B35-984BE17C2819"};

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.
    View my latest gallery contribution here.
    Visit my blog here.



    Thursday, November 15, 2012 2:06 PM
  • Hi Pavlos;

    Thats works great thanks.do you know when we open a new email window we can pass this post object to the parameters i am talking about extraqs???

    Thursday, November 15, 2012 2:13 PM