How to download an attachment file of a note of an entity

    General discussion

  • Hi,

    I used below query to download a pdf file attached with note of an entity, but it shows Access Denied. Id is of annotationid.



    Thursday, March 01, 2012 5:54 AM

All replies

  • hi ,

    use this function to download attatchment from note

     public void Getnote(Guid NoteId)
                string _path ="destination path";//end with "\" //ConfigurationSettings.AppSettings["destination"];
                QueryExpression query = new QueryExpression();
                query.EntityName = "annotation";
                query.ColumnSet = new ColumnSet(new string[] { "documentbody", "filename" });
                ConditionExpression cond = new ConditionExpression("annotationid", ConditionOperator.Equal, NoteId);
                FilterExpression filt = new FilterExpression();
                query.Criteria = filt;
                RetrieveMultipleRequest req = new RetrieveMultipleRequest();
                req.Query = query;
                RetrieveMultipleResponse res = (RetrieveMultipleResponse)ServiceHelper.ServiceProxy.Execute(req);
                int i = 0;
                if (res.EntityCollection.Entities.Count != 0)
                    foreach (Entity notes in res.EntityCollection.Entities)
                            if (!File.Exists(_path) && notes.Contains("documentbody"))
                                string name = "newimage.jpg";
                                byte[] pic = Convert.FromBase64String(notes["documentbody"].ToString());
                                if (notes.Contains("filename"))
                                    name = notes["filename"].ToString();
                                File.WriteAllBytes(_path + name, pic);
                        catch (Exception ex)
                            Log.Write("Exception " + ex.Message + " For notes");

    Anwar noori

    Thursday, March 01, 2012 11:26 AM
  • Hi Anwar,

    Thanks for reply. But my requirement is to do that on client side with OData endpoint or any other way.


    Thursday, March 01, 2012 11:53 AM
  • Any luck on this ? I have a similar requirement. We upgraded from CRM 4.0 to CRM 2011. In CRM 4.0, we were calling download.aspx to download an attachment directly, this however does not seem to be working for CRM 2011. If i try using download.aspx with ID = Annotation Id, it gives Access denied error.
    Saturday, April 28, 2012 10:10 AM
  • Hi Vivek,

    Try this on client side in javascript web resource. This has worked for me.

    SERVER_URL + "/_controls/notes/notesdata.aspx?EnableInlineEdit=true&EnableInsert=true&ParentEntity=10004&id=" + annotationid.toString();

    If this works, please mark it as answer.


    Monday, April 30, 2012 4:09 AM
  • Hi Vidhyut,

    Still no luck. It now gives me "An Error has occured" screen :(

    Update: I changed the ParentEntity to the pType of my entity, and it seemed to open a page with a link to "add new note", but its not exactly what I want. What I want is, for the attachment in the annotation to prompted to open/save. In CRM 4.0, calling the download.aspx would do the trick, this somehow has changed in CRM 2011.

    Using fiddler, I notice CRMWRPCToken & CRMWRPCTimeStamp being passed when I manually try to open the attachment from the Notes, but for me, there is no way to generate these two values. The absence of these 2 values in my reques is probably the reason I get access denied error when I call download.aspx, perhaps !

    Sunday, May 06, 2012 11:24 AM
  • Vivek, did you figure out how to get the Token and TimeStamp values? I'm struggling with the same issue.
    Thursday, June 07, 2012 8:30 PM
  • Hi Wander,

    there is no way to generate Token/Timestamp values which CRM 2011 internally passes to the page to open the attachment, or atleast I was not able to. The Token/Timestamp values were used in CRM 4.0 and has been replaced by the tight integration with AD. This page would have probably been "left out" or missed during the entire process.

    What we are doing now instead is opening up the annotation record. The end user now has to click twice ( click on the attachment which opens up the annotation record, and then click on the attachment to open it) as opposed to just once in CRM 4.0, which would directly open up the attachment. MS should make this customizable, as in I should be able to decide whether I first want to view the attachment or show the open/save attachment directly..just my opinion though.

    To open up the annotation record, I am opening the notes/edit.aspx page, passing it the required guid, entity type, etc etc, which works fine.

    Friday, June 08, 2012 3:04 AM
  • hi anwar,

    thanks for your post.

    my requirement is to download attachments on click of custom button in FormGrid.

    can you please tell me how to get "NoteId" dynamically.

    and what is _path ? how to choose that ?

    i would be thankful for your help.

    Thursday, July 12, 2012 7:11 AM
  • Hi Poseidon,

    _path stores location of where you want to save the attachment file. It can be decided by you.

    NoteId you will have to know for you to be able to open it/download attachment from it. To get NoteId (annotationid) of a Note which is linked to another entity, for e.g, if you want to open a note linked to an Account whose accountid ={some_guid}, then you have to query annotation entity where objectid={some_guid}. anwar has used annotationid=NoteId in the condition expression, instead of which you may use ObjectId={some_guid}.

    ObjectId attribute in annotation stores guid of its parent (not exactly) record.



    Friday, July 13, 2012 6:28 AM