Listing Files Not Uploaded and Stored using the Sage CRM interface

Hints, Tips and Tricks

Technical Hints Tips and Tricks that cover customization and development using Sage CRM. API usage and coding are covered.

Listing Files Not Uploaded and Stored using the Sage CRM interface

  • Comments 12
  • Likes

Occasionally you may customer requirement to store relatively large numbers of documents associated with a company, person or opportunity record outside the main librabry mechanism.  As an example of this requirement imagine an organisation that sells design services to real estate developers.  Such a company may find that their sales person is regularly given dozens of files that include brochures, plans and photos that all form part of the information need for the customer to recieve a properly constructed proposal.

The document upload into Sage CRM allows the easy upload of files on an Ad Hoc basis but it can be time consuming when there are a large number of files that need importing.

I have previously touched on the batch import of documents in to the library in the article "Moving files into the Library using the serverside COM API".

Another approach that can be taken is to allow the files to be copied directly by the system administrator to a folder on the network.

The snag is going to be that any files should not be accessible except via a URL.  If the folder containing the files was a virtual directory then the files could be easily accessed via the browser.  But placing a folder directly 'on the web' is potentially a security issue.  For example Sage CRM's own library structure hides the documents and they can only be accessed via a URl that 'passes through' Sage CRM.

The way that the path to a file is build has has been discussed in the article "The display of a photo or image in the person screen". 

Of course you could upload the document to an external Document Manager System but then you would have the challenge of building the integration.

In this example I have just created a folder called DemoFiles underneath the existing Library structure.

This folder contains a set of files.

You can see from this screen below I have been able to link to those documents which can then be downloaded.

The additional folder name has been stored in the company record in a field called comp_customfolder.

The code below is an ASP customization but the equivalent could also be carried out in the .NET API.  I have used the Scripting.FileSystemObject.

Note:  The variable sInstallName is created in the include file (sagecrmnolang.js/accpaccrmnolang.js/eware.js).

<!-- #include file ="sagecrm.js"-->
<%

var contentBlock = CRM.GetBlock("content"); var myBlockContainer = CRM.GetBlock("Container"); with (myBlockContainer) { AddBlock(contentBlock); DisplayButton(Button_Default) = false; } //example using objects var mySystemObject = Server.CreateObject('Scripting.FileSystemObject'); var mySource = CRM.GetContextInfo("Company","comp_customfolder"); var myPath = "C:\\Program Files\\Sage\\CRM\\"+sInstallName+"\\Library\\" +mySource; var myFile var myFolder = mySystemObject.GetFolder(myPath); var myFiles = new Enumerator(myFolder.Files); var myCount = 0; contentBlock.contents+='<ul>'; while(!myFiles.atEnd()) { myFile = myFiles.item(); contentBlock.contents+='<li><a href=/'+sInstallName+'/eware.dll/do/'+mySource+'/'+encodeURI(myFile.Name)+'?SID=179740597744232&Act=1282&Mode=0&FileName='+mySource+'/'+encodeURI(myFile.Name)+'>'+myFile.Name+'</a>'; myFiles.moveNext(); ++myCount } contentBlock.contents+='</ul><br>There are '+myCount+ ' files<br>'; CRM.AddContent(myBlockContainer.Execute()); Response.Write(CRM.GetPage());

 %>

 

Comments
  • Hi Jeff

    I need to do this pretty urgently for a client - I've tried creating an asp page in the custom pages folder (called CompDocuments.asp) from the above and linking this to a tab on the company record that launches the custom file, but I just get a 500 - Internal server error when clicking on the tab. Here's the code I used:

    <!-- #include file ="sagecrm.js"-->

    <%

    <P></P>

    var contentBlock = CRM.GetBlock("content");

    var myBlockContainer = CRM.GetBlock("Container");

    with (myBlockContainer)

    {

     AddBlock(contentBlock);

     DisplayButton(Button_Default) = false;

    }

    //example using objects

    var mySystemObject = Server.CreateObject('Scripting.FileSystemObject');

    var mySource = CRM.GetContextInfo("Company","comp_customfolder");

    var myPath = "C:\\Program Files (x86)\\Sage\\CRM\\"+sInstallName+"\\Library\\" +mySource;

    var myFile

    var myFolder = mySystemObject.GetFolder(myPath);

    var myFiles = new Enumerator(myFolder.Files);

    var myCount = 0;

    contentBlock.contents+='<ul>';

    while(!myFiles.atEnd())

    {

     myFile = myFiles.item();

       contentBlock.contents+='<li><a href=/'+sInstallName+'/eware.dll/do/'+mySource+'/'+encodeURI(myFile.Name)+'?SID=179740597744232&Act=1282&Mode=0&FileName='+mySource+'/'+encodeURI(myFile.Name)+'>'+myFile.Name+'</a>';

     myFiles.moveNext();

     ++myCount

    }

    contentBlock.contents+='</ul><br>There are '+myCount+ ' files<br>';

    CRM.AddContent(myBlockContainer.Execute());

    Response.Write(CRM.GetPage());

    %>

    The only thing I changed was the file path to include the (x86) as it is a 64 bit server. They have a custom entity already with asp pages that work fine so don't think this is a server setting.

    Are there any steps I've missed or something wrong with the code?

  • sorry - i should have said as well that this is in CRM v7.0c

  • Can you run ASP pages?  Can you instantiate the FileSystem object?

  • I have this ASP snippet working, it's great.  But what if I don't want to copy everything over to the CRM Library folder?  I have a new client with years of photographs on a shared network folder.  The Scripting object finds the files and populates the screen, but I can't get CRM to see them once the users click on the shortcut.  

    Is there some way I can redirect to that folder?

  • Is the folder exposed to the web server? - with direct web access?

  • As far as I know it is.  It's exposed via IIS, and I can browse to it like this:

    http://mypc/crm/images/test/

    and see the typical raw folder display.

  • This basic code is how any COM code can see a folder

    //example using objects

    var mySystemObject = Server.CreateObject('Scripting.FileSystemObject');

    var myPath = Server.MapPath('/CRM/custompages/');

    var myFile

    var myFolder = mySystemObject.GetFolder(myPath);

    var myFiles = new Enumerator(myFolder.Files);

    var myCount = 0;

    Response.Write('<ul>');

    while(!myFiles.atEnd())

    {

     myFile = myFiles.item();

     Response.Write('<li><a href='+CRM.URL(myFile.Name)+'>'+myFile.Name+'</a>');

     myFiles.moveNext();

     ++myCount

    }

    Response.Write('</ul><br>There are '+myCount+ ' files<br>')

  • Hello Jeff,

    could you please show how to achieve this in .Net? Thanks

  • Hello Jeff,

    Hope you are doing well.  Quick question about this - I'm trying to use it in v7.1SP2 and all seems to work fine until you try clicking on the link of an item at which point CRM logs you out, and does not open the attachment.  Did something change in 7.1SP2 that would make the URL String not be respected and force a logoff?  Any thoughts would be greatly appreciated.

    Thanks,

    Scotty Chapman

    Blytheco, LLC

    schapman@blytheco.com

  • Never mind - the logoff was happening because the SID was not being grabbed for the logged in user.  Added that in and all works fine.

  • Does anybody know if this same thing can be achieved but for .rdp files instead of document files?

    I am trying to embed this feature in the cases entity whereby users can launch a Windows remote desktop connection in the same way as they launch a document.

  • Ben

    You should be able to use this to list rdp files.  The code lists all of the files within the folder.