SharePoint 2013: Add power to Office apps

Using two new development models, you can automate integrated processes between SharePoint and your Office applications.

V. Gnanasekaran

The new development models in Office 2013 and SharePoint 2013 create some new opportunities for integrating Office applications with SharePoint. These development models—apps for SharePoint and apps for Office—help you leverage a variety of Web development options including HTML5, JavaScript, jQuery and CSS3, in addition to the existing capabilities in server-side languages.

These new development paradigms provide more capabilities on the client side. In this article, I’ll show how you can leverage SharePoint in apps for Office through an example scenario.

Apps for SharePoint

The apps for SharePoint model provides more flexible ways to customize your SharePoint environment. You can also use it to access data through various application programming models, such as:

  • Server Object Model
  • Client Object Model 
  • JavaScript Object Model
  • REST API
  • WCF Data Services Framework

You should use these object models and access methods based on the operational context. For example, you should use the Mobile Object Model (available under the Client Object Model) to perform SharePoint-related activities on a Windows Phone.

Apps for Office

The new apps for Office development model lets you interact with Office products through JavaScript APIs. It also helps you leverage other Web development stacks such as HTML5, CSS3 and jQuery. The apps for Office model is Web-based and gives you much more flexibility than traditional Office add-ins.

For this example, let’s look at two common business activities—document preparation and review. You might be generating documents for targeting potential business opportunities such as proposals, sales materials or fliers. You may also be developing detailed reports as part of business processes, transactions, or documents establishing business partnerships and legal bindings such as contractual agreements.

Document review is an integral part of the process. There’s more emphasis placed on this step when dealing with legal documents. Documents are reviewed against business compliance regulations and to ensure the enterprise is safeguarded against any legal issues. When the review process of a legal binding document is compromised, it might lead to financial damages or worse.

Microsoft Word can automatically review a document for grammatical errors. If there were such automation for validating a business or legal document, it would streamline business processes. The example I’ll discuss here will look at providing this type of capability, leveraging the power available through the Office and SharePoint application development models.

A look at business use

As part of a proposal bidding process, the example company, Contoso, has a document review activity where all project proposal documents are reviewed against solution approach, estimates, execution model, differentiating factors and so on. Documents are reviewed on most aspects, based on the context. For example, if a proposal document is prepared for a fixed-fee project, it will be scrutinized for the presence of “objectionable” words. This is the document business validation process.

Objectionable words are defined by the legal team. The legal team feels that presence of these predefined “objectionable” words will unnecessarily expose the company to regulatory issues. Contoso wants to automate all business validation of documents generated by the project team. The company expects this will increase the team’s proposal rate and turnaround time. It would also minimize incidents where documents are overlooked in the review process.

As a first step, Contoso wants to automate the document review process to scan for predefined objectionable words, as this is a repetitive activity. This document review process is based on predefined rules configured in a central location. The company wants to make this process available to employees who will access it within the corporate network through an integrated Windows authentication security model.

This example leverages both SharePoint and apps for Office to develop the scenario and automate the document business validation process. The list of required steps includes:

  • Create a Task Pane App for Office and select Word. Name this OfficeApp1. It will have two projects: OfficeApp1 and OfficeApp1Web. You can do any required custom coding in the OfficeApp1Web project.
  • As you’ll use the SharePoint .NET Client Object Model, add an ASPX (named Home.aspx) page to the project and make it the default page.
  • Update the OfficeApp1.xml file in the OfficeApp1 project, using the DefaultValue attribute of the SourceLocation as Home.aspx as depicted here:

<DefaultSettings> <SourceLocation DefaultValue="~remoteAppUrl/Pages/Home.aspx" /> </DefaultSettings>

  • Create a SharePoint list named ObjectionableWords. Store the list in a sub-site named FirstSiteCollection.
  • Add references to the Microsoft.SharePoint.Client and Microsoft.SharePoint.Client.Runtime in the OfficeApp1Web project.
  • In the code behind the Home.aspx file, add references to Microsoft.SharePoint.Client and the code in Figure 1 under the page load event (this code will link to the SharePoint list, pull out the list of words and populate the words into a JavaScript array).

Figure 1 This script populates the list of Objectionable words.

string arrayString = string.Empty; string PIN = string.Empty; // Starting with ClientContext, the constructor requires a URL to the // server running SharePoint. ClientContext context = new ClientContext("https://c4968397007/sites/First/"); // Point this URL to the right URL in your environment // Assume the Web has a list named "ObjectionableWords." List objWrdsList = context.Web.Lists.GetByTitle("ObjectionableWords"); // This creates a CamlQuery that has a RowLimit of 100, // and also specifies Scope="RecursiveAll" so that it grabs all list items CamlQuery query = CamlQuery.CreateAllItemsQuery(100); Microsoft.SharePoint.Client.ListItemCollection items = objWrdsList.GetItems(query); // Retrieve all items in the ListItemCollection from List.GetItems(Query). context.Load<List>(objWrdsList); context.Load<Microsoft.SharePoint.Client.ListItemCollection>(items); context.ExecuteQuery(); foreach (Microsoft.SharePoint.Client.ListItem listItem in items) { if (arrayString.Length > 0) arrayString += ","; arrayString += "'" + listItem.FieldValues["Words"].ToString() + "'"; } this.ClientScript.RegisterArrayDeclaration("myWords", arrayString);

  • Office 2013 lets you interact with its apps through Office.js, the JavaScript API library for Office. Add the function getDocument to the OfficeApp1.js JavaScript file, where all the interactions with Word will happen (see Figure 2).

Figure 2 Add the getDocument Function to the OfficeApp1.js JavaScript file.

function getDocument() { //var fileContent = ''; var strContent = ''; Office.context.document.getFileAsync("text", function (result) { var myFile = result.value; myFile.getSliceAsync(0, function (result) { if (result.status == "succeeded") fileContent = result.value.data; checkString(); checkPendingItem(); }); }); }

  • This function interacts with a Word document through the JavaScript API library for Office. It extracts the content and calls the other function checkString, which will actually perform the validation.
  • Add the function checkString to the Office.js file, as shown in Figure 3.

Figure 3 Function checkString added to the Office.js file.

function checkString() { // Check for the objectionable words. for (var i = 0; i < myWords.length; i++) { contains = fileContent.toLowerCase().trim().indexOf(myWords[i].toLowerCase().trim()); if (contains >= 0) { break; } } // If the document contains "Objectionable" words, indicate it. if (contains < 0) { document.getElementById('imgSts').src = 'YES.PNG'; document.getElementById('imgSts').height = '42'; document.getElementById('imgSts').width = '42'; } else { document.getElementById('imgSts').src = 'NO.PNG'; document.getElementById('imgSts').height = '42'; document.getElementById('imgSts').width = '42'; } }

This function compares the extracted Word content with the JavaScript array, which was populated with words from the SharePoint list. It will then indicate whether the extracted content contains any of the words from the objectionable list.

Once you run the solution, it will flag words that aren’t allowed. For example, when a user types the word “Best” (which is on the ObjectionableWords list) and clicks Review, the app will state the document doesn’t comply with the “objectionable words” requirement.

When a user writes “TODO” to indicate a pending item in a document, the application will show whether the document is complete. You can further customize this process to show a percentage completion of a document. This will be helpful especially in scenarios where different parts of a document are assembled across different locations and you need to track the document’s overall progress.

Productivity advantages

Using this level of automation with SharePoint and its Office apps, Contoso realizes the following benefits:

  • It can ensure consistency in document business validation.
  • Any changes to the objectionable words list occur centrally and are reflected in the document review process across the enterprise.
  • Supplementing the document review process with this type of automation reduces the time required for completion.
  • Rules-based document review provides more sophisticated capabilities.

The SharePoint Client Object Model lets you use apps for Office to integrate with SharePoint resources. Combining the power of both SharePoint and Office products through apps for Office helps you achieve significant productivity gains. SharePoint and Office become more than the sum of each solution.

V. Gnanasekaran

V. Gnanasekaran has more than 14 years of experience as a TOGAF 9 Certified Enterprise Architect and part of Microsoft Consulting Services India, where he provides architectural consulting services to customers. His current areas of focus include SharePoint, SQL Server OLAP/OLTP and Windows Azure. He has published technology articles in various journals including The Architecture Journal and has been published on CodeProject. He blogs at gnanasekaran.com.