In this lab, you will
- You must have an Office 365 tenant to complete this lab. If you do not have one, the lab for O3651-7 Setting up your Developer environment in Office 365 shows you how to obtain a trial.
In this exercise you will create a basic SharePoint-Hosted app that you can enhance in follow-on exercises.
- Create the new project in Visual Studio 2013:
- Launch Visual Studio 2013 as administrator.
- In Visual Studio select File/New/Project.
- In the New Project dialog:
1. Select Templates/Visual C#/Office/SharePoint/Apps.
2. Click App for SharePoint 2013.
3. Name the new project DeepDiveSPApp and click OK.
- In the New App for SharePoint wizard:
1. Enter the address of a SharePoint site to use for testing the app (NOTE: The targeted site must be based on a Developer Site template)
2. Select SharePoint Hosted as the hosting model.
3. Click Finish.
4. When prompted, log in using your O365 administrator credentials.
- Test the app
- After the project is created, press F5 to debug the app.
- Verify that the app launches and greets you.
- Stop debugging.
- The code in the project template utilizes the client-side object model (CSOM). However, it can be rewritten to utilize the equivalent REST calls.
- Open the app.js file located in the scripts folder.
- Delete all of the JavaScript code in the file.
- Add the following code to the app.js file.
(function () {
"use strict";
jQuery(function () {
jQuery.ajax({
url: "../_api/web/currentuser",
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
},
success: function (data, status, jqXHR) {
jQuery("#message").text("Welcome, " + data.d.Title);
},
error: function (jqXHR, status, message) {
jQuery("#message").text(message);
}
});
});
}());
- Press F5 to debug the app.
- Verify that the app launches and greets you.
In this exercise you will add an app part to your project for displaying song titles based on a specified artist.
- Add the new Client Web Part
- In Visual Studio 2013, right click the DeepDiveSPApp project and select Add/New Item.
- In the New Item dialog, select Client Web Part (Host Web).
- Name the new app part MusicPart and click Add.
- In the Specify the Client Web Part Page dialog, select Create a New App Web Page for the Client Web Part Content.
- Click Finish
- In the Element.xml file that describes the Client web part, replace the ClientWebPart element with the following code.
<ClientWebPart
Name="MusicPart"
Title="Music App Part"
Description="Displays songs from a specified artist"
DefaultWidth="300"
DefaultHeight="200">
<Content Type="html" Src="~appWebUrl/Pages/MusicPart.aspx?{StandardTokens}&Artist=_Artist_" />
<Properties>
<Property
Name="Artist"
Type="string"
WebBrowsable="true"
WebDisplayName="Artist"
WebDescription ="The artist to search"
WebCategory="Configuration"
DefaultValue="artist"
RequiresDesignerPermission="true"
PersonalizableIsSensitive="false"
PersonalizationScope="shared"/>
</Properties>
</ClientWebPart>
- Prepare the App Part user interface
- Open MusicPart.aspx for editing.
- Add the following HTML to the body element for displaying information in the app part.
<div>
<ul id="songList"></ul>
</div>
- Add the following script reference to the head section to include functionality for the app part.
<script type="text/javascript" src="../Scripts/apppart.js"></script>
- Code the App Part.
- Right click the scripts nodeand select Add/New Item.
- In the Add New Item dialog, select Web and then JavaScript File.
- Name the new file apppart.js.
- Click Add.
- Add* the following code to apppart.js to call the MusicBrainz service and display songs for the designated artist.
(function () {
"use strict";
$(function () {
var ctx = SP.ClientContext.get_current();
var request = new SP.WebRequestInfo();
request.set_url(
"http://www.musicbrainz.org/ws/2/release-group?query=artist:" + artist
);
request.set_method("GET");
responseDocument = SP.WebProxy.invoke(ctx, request);
ctx.executeQueryAsync(onSuccess, onError);
});
}());
var onSuccess = function () {
var xmlDoc = $.parseXML(responseDocument.get_body());
$(xmlDoc).find("release-group").each(function (i) {
var title = $(this).children("title").first().text();
$("#songList").append("<li>" + title + "</li>")
});
}
var onError = function () {
alert("failed!");
}
var getQueryStringParameter = function (p) {
var params =
document.URL.split("?")[1].split("&");
var strParams = "";
for (var i = 0; i < params.length; i = i + 1) {
var singleParam = params[i].split("=");
if (singleParam[0] == p)
return decodeURIComponent(singleParam[1]);
}
}
var artist = getQueryStringParameter("Artist");
var responseDocument = "";
- Test the App Part
- Open AppManifest.xml
- Click Remote Endpoints
- Add the endpoint http://www.musicbrainz.org and click Add
- Press F5 to start debugging.
- When the app launches, navigate away from the app home page to the home page of the host web.
- Place the page in edit mode.
- Click Insert/App Part.
- Select the Music App Part and click Add
- Hover over the app part and select Edit Web Part from the menu.
- In the Web Part Properties, change the Artist property to a different value.
- Click OK and verify that songs appear in the app part.
- Stop debugging.
In this exercise you will add a Menu Item custom action to invoke the song search from a SharePoint list.
- Add the new Menu Item Custom action
- In Visual Studio 2013, right click the DeepDiveSPApp project and select Add/New Item.
- In the New Item dialog, select Menu Item Custom Action.
- Name the new app part SongSearchAction and click Add.
- In the Specify the Properties to Create Custom Action for menu Item dialog
1. Select Host Web
2. Select List Template
3. Select Custom List
4. Click Next
- In the Specify the Properties to Create Custom Action for menu Item dialog
1. Enter Song Search as the title
2. Enter DeepDiveSPApp\Pages\MusicPart.aspx as the target page
3. Click Finish
- Open MusicPart.aspx for editing.
- Add the following script reference to the head section just before the apppart.js reference.
<script type="text/javascript" src="/_layouts/15/sp.requestexecutor.js"></script>
- Code the Menu Item Custom Action
- Open apppart.js for editing.
- Add the following code to the bottom on the file to retrieve an artist name from a custom list selection.
var appWebUrl = getQueryStringParameter("SPAppWebUrl");
var hostWebUrl = getQueryStringParameter("SPHostUrl");
var listId = getQueryStringParameter("SPListId");
var listItemId = getQueryStringParameter("SPListItemId");
if (typeof (listId) != "undefined" && typeof (listItemId) != "undefined") {
listId = listId.substring(1, listId.length - 1);
var executor = new SP.RequestExecutor(appWebUrl);
executor.executeAsync({
url: "../_api/SP.AppContextSite(@target)/web/lists(guid'" + listId +
"')/getItemByStringId('" + listItemId +
"')?@target='" + hostWebUrl + "'",
method: "GET",
headers: {
"accept": "application/json;odata=verbose",
},
success: function (data) {
artist = JSON.parse(data.body).d.Title;
},
error: function (data) {
artist = "artist";
}
});
}
- Enclose the call to MusicBranz in a timeout function as a simple way to ensure the artist name is retrieved from the list before the call is made. The following code shows how this is done
setTimeout(function () {
var ctx = SP.ClientContext.get_current();
var request = new SP.WebRequestInfo();
request.set_url(
"http://www.musicbrainz.org/ws/2/release-group?query=artist:" + artist
);
request.set_method("GET");
responseDocument = SP.WebProxy.invoke(ctx, request);
ctx.executeQueryAsync(onSuccess, onError);
}, 2000)
- Test the Menu Item custom action
- Open AppManifest.xml
- Click Permissions
- Select Web for the Scope.
- Select Read for the Right
- Press F5 to start debugging.
- When the app launches, navigate away from the app home page to the home page of the host web.
- On the host web home page, click Site Contents.
- Click Add an App.
- Click Custom List.
- Name the new list Artists.
- Click Create
- Add a new artist name to the list.
- Using the item menu, select Song Search.
Congratulations! You have completed investigating SharePoint-Hosted apps.