Click here to Skip to main content
15,886,689 members
Articles / Productivity Apps and Services / Sharepoint

Create a SharePoint Hosted App

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
6 Oct 2015CPOL3 min read 7.7K   1   1
How to create a SharePoint hosted app

In my earlier post, I’ve walked through the creation of Provider hosted apps. Today, I’ll show how to create a SharePoint hosted app. And we’ll see how to consume an external web service API such as USDA nutrition information API.

I’m using a Office 365 tenant, however you can use on premises farm or a single server SharePoint 2013 development environment as well. Creating the SharePoint hosted app itself is very simple.

Fire up Visual Studio and create a new project.

Select Office/SharePoint -> Apps -> App for SharePoint, enter a name for the project and location.

New Project - SharePoint App

Enter the site url and select SharePoint-hosted. Click Finish. (If you are using Office 365, enter the credentials when prompted.)

App SharePoint Settings

Click next and choose which version of SharePoint you want to target. E.g. SharePoint 2013 or SharePoint online. Click Finish.

Press F5 and check that the app is loaded.

Each SharePoint hosted app has its own web called the ‘app web’ and the domain is different from the host web. It is actually an extension of the host web domain. For example, if the hostweb is https://domain.sharepoint.com, then the app web will be https://domain-<uniqueid>.sharepoint.com.

This is to avoid scripting attacks. We can only use JavaScript and JSOM in SharePoint hosted apps. We cannot use C# code including client side code or CSOM.

If we add artifacts such as lists and libraries to this project, it will be added to the app web. There are ways to create these artifacts in the host web and read from them.

If we need to create, modify, update or read from the host web, we need to request for specific permissions in the Appmanifest.xml. This is presented to the user or admin who will be installing the app. The app will be installed only if the user agrees to all the permissions requested. So, it is a best practice to only request for permissions which are absolutely required for the app to work.

I think it is better to keep only temporary artifacts in the app web. It would be better to store the persistent information in the host web. This is because, in case the app is uninstalled, the app web and all the related data is deleted or removed.

Even if you decide to store some of the information in app web, it would be a good idea to take frequent backups to prevent data loss.

For example, if you right click on the project and choose Add -> New Item -> List, the list gets added to app web. The same is true if you add any artifact such as Content type, workflow, etc.

Retrieve External Data from a Web Service API

Let's create a simple application which queries the USDA nutrition information database API.

U.S. Department of Agriculture, Agricultural Research Service 2014. USDA National Nutrient Database for Standard Reference, Release. Nutrient Data Laboratory Home Page, http://www.ars.usda.gov/nutrientdata

Add a text box and a button to the default.aspx page. And also, a section to show the results.

XML
<div>
  <input id="txtFood" type="text" />
  <input id="searchFood" type="button" value="button" />
</div>
<div>
  <table style="border: solid; border-width: medium">
    <tbody id="foodResults">
    </tbody>
</table>
</div>

In the App.js, query the web service and populate the results. You need to get an API Key from U.S. Department of Agriculture, Agricultural Research Service and replace API_Key in the url below.

JavaScript
$("#searchFood").click(function () {
 var request = new SP.WebRequestInfo();
 var searchTerm = $("#txtFood").val();

#replace the API_Key in the url
 request.set_url(
 "http://api.nal.usda.gov/ndb/search/?format=json&q=" + 
 searchTerm + "&sort=n&max=10&offset=0&api_key=API_Key"
 );
 request.set_method("GET");
 responseDocument = SP.WebProxy.invoke(context, request);
 context.executeQueryAsync(onSuccess, onError);
 });

function onSuccess(data, status) {
 var result = $.parseJSON(responseDocument.get_body());
 var items = result.list.item;
 var arrLength = items.length;
 $("#foodResults").empty();
 $("#foodResults").append("<tr><th 
 >Food Group</th><th>Food Name</th></tr>");
 for (var i = 0; i < arrLength; i++) {
 var foodName = items[i].name;
 var foodGroup = items[i].group;
 $("#foodResults tr:last").after("<tr>
 <td style=\"border:solid;border-width:thin\">" + 
 foodGroup + "</td><td style=\"border:solid;border-width:thin\">" + 
 foodName + "</td></tr>");
 }
}

var onError = function () {
 alert("failed!");
}

You can expand on this further by getting the food item id, and getting the full nutrition information. And adding other functionality such as adding foods and storing them in SharePoint, etc. The possibilities are endless limited only by your imagination! Also, explore other APIs like twitter, etc. and share your experiences with implementing them.

Related Posts

The post Create a SharePoint hosted app appeared first on SharePoint Guide.

This article was originally posted at http://www.thesharepointguide.com/sharepoint-hosted-app

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThanks Pin
cuteangel49396-Oct-15 12:43
cuteangel49396-Oct-15 12:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.