1

I saved some document templates in Notes of a case. I need to get the download url of the document, which I will use in someother plugin which requires that.

Now the problem is, I uploaded a document to notes section, I got its download url by downloading it and checking the download url.

When I feed this as input to the plugin, it works only for the user who uploaded it.

For other users its not accessible, it says

enter image description here

Now where can I upload document in CRM so that it is viewable and downloadable by all users?

Vignesh Subramanian
  • 7,161
  • 14
  • 87
  • 150

2 Answers2

2

You need to get a valid WRPCTokenUrl. Instead of storing the direct URL for fetching the document (which would not contain a valid WRPCTokenUrl for other users), you would store the guid of your Note and later get a valid URL when needed.

The Note guid can be used for fetching a valid download URL as described in Extending Microsoft Dynamics CRM 2011: Build Attachment Download URL Links with JQuery. The relevant code snippet from that page can be seen here:

function getDocumentUrl(annotId) {
    var URL = urlbase + '/userdefined/edit.aspx?etc=5&id={' + annotId + '}';
    var docUrl;

    // get the security token to build the href link. if the token cannot be found,
    // a null value is returned
    $.get(URL, function (data) {
        // get the form data via the 'URL'
        data = $.parseHTML(data);

        // locate the security element
        var securityTokenElement = $(data).find("[WRPCTokenUrl]");

        if (securityTokenElement) { // if the security element was found on the Note form
            // locate the security token within the security element
            var securityTokenUrl = SecurityTokenElement.attr("WRPCTokenUrl");

            // if the security token is located, build the url
            if (securityTokenUrl) {
                docUrl = urlbase + "/Activities/Attachment/download.aspx?AttachmentType=5&AttachmentId={" +
                         annotId + "}&IsNotesTabAttachment=undefined" + SecurityTokenUrl;
            }
        }
    });

    return docUrl;
}
Henrik H
  • 5,747
  • 1
  • 21
  • 33
2

There is a much easier way to access the document data if that's what you are trying to by using a simple web service call. I wouldn't try accessing the document data via the URL, I don't think that's really how its meant to be done.

Check out Sample: Upload, retrieve, and download an attachment.

// Retrieve the annotation record.
Annotation retrievedAnnotation =  (Annotation)_serviceProxy.Retrieve("annotation", _annotationId, cols);
_fileName = retrievedAnnotation.FileName;

// Download the attachment in the current execution folder.
using (FileStream fileStream = new FileStream(retrievedAnnotation.FileName, FileMode.OpenOrCreate))
{
    byte[] fileContent = Convert.FromBase64String(retrievedAnnotation.DocumentBody);
    fileStream.Write(fileContent, 0, fileContent.Length);
}
James Wood
  • 17,286
  • 4
  • 46
  • 89