Show list of documents from SharePoint related to a record on the main form

Posted by

There are times when you want to show all the documents associated to a record on the Main form for better User Experience and easy access. Below is an image of how the final result will look like. This is the Document Associated View which display all documents stored in SharePoint and associated to this record.

 

Basically we are going to use JavaScript to display this view in an IFrame on the main page. You can find this view in the Documents section of your entity (see below).

Click F12 to find the URL for this view in the page source.

So the Url looks something like this

/userdefined/areas.aspx?formid=0e2b86e3-f409-4a03-abef-f862a6754cde&inlineEdit=1&navItemName=Documents&oId=%7b8500CFA7-741B-E711-80FB-C4346BAC894C%7d&oType=10191&pagemode=iframe&rof=true&security=852023&tabSet=areaSPDocuments&theme=Outlook15White/

Save this URL as we will require it to add in the JavaScript code

Now create an empty IFrame on the main form. Make sure to keep the “Restrict cross-frame scripting, where supported” option as Unchecked. Also copy the IFrame name to reference in the JavaScript. Set a good number of rows in the formatting tab to make sure the IFrame doesn’t have a scrollbar.

Now select the IFrame on the form and select Form properties. In the Form Properties there’s an Events tab. Click on the Events tab and then Click on the add button to add a JavaScript file.

On click of add button you will see a pop up of the Web resources lookup. Select the Js file which you created and click Add.

you can also create the JavaScript file from here by clicking the new button.Fill all the details, in type field select JScript and Click on TextEditor

In the TextEditor add the below JavaScript.

function SetDocumentFrame() {

//You can see what the url should be by navigating to the ‘Documents’ area under related records, viewing the page soure
//and looking for ‘areaSPDocuments’. The formid appears to be nothing more than a random guid value and not tied to anything
//specific in your org.

//Use: Make sure Document Management is enabled for the entity (helps to turn on automatic folder creation)
// Add a web resource with this code to the form
// Execute this function during the form’s OnLoad event

var url = Xrm.Page.context.getClientUrl() +
/userdefined/areas.aspx?formid=0e2b86e3-f409-4a03-abef-f862a6754cde&inlineEdit=1&navItemName=Documents&oId=%7b” +
Xrm.Page.data.entity.getId().replace(“{“, “”).replace(“}”, “”) + “%7d&oType=” +
Xrm.Page.context.getQueryStringParameters().etc +
“&pagemode=iframe&rof=true&security=852023&tabSet=areaSPDocuments&theme=Outlook15White”;

Xrm.Page.getControl(“IFRAME_SharepointDocuments“).setSrc(url);
}

Make sure to replace the above URL with your document associated view URL. Also replace the name of the IFrame with the name of the IFrame you created, save it and publish it. Also remember the name of the JScript method, in this case SetDocumentFrame. In the look up view add the newly created JavaScript Web Resource.

Now add an event handler by clicking on the Add button. This will open a pop up, select your JavaScript function and type the name of the JavaScript function i.e.  SetDocumentFrame.

Click OK and your ready to go! What you’ve basically done is added a function to display the Document associated view in the main form using the View’s URL.

 

Hope this helps 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s