Exercise 1: Using the Visio JSOM
In this exercise you will learn how to create and use a simple Visio Services JSOM script. The objective of this exercise is to demonstrate all of the tasks required to use the Visio Services JSOM. In this exercise, you will create a simple script that logs user activity on a Visio diagram to a Content Editor Web part.
Task 1 – Creating the Script
In this task, you will be developing a script to use ECMAScript that will allow you to communicate with the SharePoint Visio services.
- Navigate to %Office2010DeveloperTrainingKitPath%\Labs\VisioJSOM\Starter and open FabriKamOrgChart.js in Visual Studio
Add the following variables and function call to your code between the <script> tags. vwaControl will be used to hold a reference to a Visio Services Web Access part instance. vwaPage will be used to hold a reference to the page object. Sys.Application.add_load() is used to add an event handler that is called once the scripts have been loaded and all objects in the application have been loaded and initialized.
var logItems = true; var webPartElementID = "WebPartWPQ4"; var vwaControl; var vwaPage; Sys.Application.add_load(onApplicationLoad);
Add the following function to your code after Sys.Application.add_load(). This function is the event handler that was specified in the last step. In this function, you are creating a new Visio Web Access Control object that is associated with the Visio Web Access web part. You are also adding an event handler that will be called once the Visio diagram is done loading.
function onApplicationLoad() { try { vwaControl = new Vwa.VwaControl(webPartElementID); vwaControl.addHandler("diagramcomplete", onDiagramComplete); } catch(err) { } }
Add the following function to your code after the onApplicationLoad() function. This function is the event handler that you specified in the last step. In this function, you obtain a reference to the active Visio page object and add an event handler that is called when the active shape selection changes.
function onDiagramComplete() { try { vwaPage = vwaControl.getActivePage(); vwaPage.setZoom(-2); vwaControl.addHandler("shapeselectionchanged", onShapeSelectionChanged); } catch(err) { } }
Add the following function to your code after the function onDiagramComplete(). This event handler that will be triggered every time the shape selection changes.
function onShapeSelectionChanged() { var sh = vwaPage.getSelectedShape(); writelog("Logging: onShapeSelectionChanged - " + sh.getName()); }
Add the following function to your code after the function onShapeSelectionChanged(). This function will write messages to a Content Editor Web part. You will use it to trace the flow of events as you work with a Visio Web Access web part.
function writelog(item) { if(logItems) { var output = item + '<br />'; document.getElementById('logdiv').innerHTML = output + document.getElementById('logdiv').innerHTML; } }
- Save your changes to FabriKamOrgChart.js
- Open Internet Explorer and navigate to https://intranet.contoso.com
- Click on Shared Documents
- Click on Add Document
- Click Browse and navigate to %Office2010DeveloperTrainingKitPath%\Labs\VisioJSOM\Starter\ FabriKamOrgChart.js
- Click Open and then OK
- Repeat steps 10-12 for FabriKamOrgChart.vdw
Task 2 – Setting up the Web Part Page
In this task, you will be setting up a SharePoint web part page that will incorporate your Visio document and ECMAScript.
- Open up Internet Explorer and navigate to https://intranet.contoso.com
- Click the Site Actions dropdown and select More Options
- Filter by Page, select Web Part Page and click Create
- Title the page FabriKamOrgChart and for the Layout Template, select Header, Left Column, Body
- Click the DocumentLibrary drop down and select SitePages
Click Create
Figure 1
New Web Part Page dialog
- Click Add a Web Part in the Body section of the page
- View the Categories section and select BusinessData. View the Web Parts section, select VisioWebAccess, and then click Add
- Click the dropdown menu next to the checkbox in the Visio Web Access web part
- Select Edit Web Part
- View the Web Drawing Display section of the Visio Web Access property box and under Web Drawing URL, click the box and navigate to FabriKamOrgChart.vdw, which is located in the Shared Documents folder
- View the Toolbar and User Interface section and uncheck every option except for Show default background
- View the Appearance section, set the Height to 600 and change the ChromeType to None
- Click OK to save the changes
- If the checkbox in the Visio Web Access web part is checked, uncheck it
- Click Add a Web Part in the Left Column section of the page
- View the Categories section and select MediaandContent. View the WebParts section, select ContentEditor and then click Add
- Click the dropdown menu next to the checkbox in the Content Editor web part
- Select Edit Web Part
- Open up the SharePoint site homepage in a new tab or window
- Click Shared Documents
- Find FabriKamOrgChart.js, right-click on it and select Copy shortcut
- Go back to the web part page you were working on before
- Right-click in the box under ContentLink in the ContentEditor section and select Paste
- View the Appearance section and set the Width to 200. Be sure to select Yes next to the Width box
- Set the ChromeType to None in the Appearance section
- Click OK to save the changes
Click Stop Editing in the top left corner of the page
Task 3 – Verifing the Visio Web Access WebPartElementID
In this task, you will learn how to go about verifying the correct web part element ID for a Visio Web Access web part. This is necessary to verify that the web part element ID you use in your script is the web part element ID associated with the Visio Web Access web part you desire to interact with.
- If you’re not on your web part page already, go to https://intranet.contoso.com, click on Site Pages and click on FabriKamOrgChart
- Right-click on the page and select View source
- Search for the keyword class="VisioWebAccess"
- Look just above that div tag and find the div tag that starts with <div WebPartID
Find the id attribute within the div tag and take note of the value of that attribute. That is the web part element ID that needs to be reflected in the ECMAScript. In task 1, that value was WebPartWPQ4. If the element ID does not match, change the value in your FabriKamOrgChart.js script and re-save it to the site.
Figure 3
Web Part Page Source Code
Exercise 1 Verification
To verify that the ECMAScript and Visio Web Access web part are functioning together and correctly, perform the following steps
- Click on Mike at the top
View the content editor along the left side and verify the following output
Logging: onShapeSelectionChanged – Executive
Click on additional people to view similar results
If there is nothing being logged, review Task 3 1-5 in the previous exercise section to make sure your JavaScript is referencing the correct WebPart id.
Figure 4
Finished Web Part Page