SharePoint hosted app – Detailed Tutorial + Example

In this SharePoint tutorial, we will see an example of SharePoint hosted add-in or previously known as SharePoint hosted apps.

Before going into the example, we will check what are SharePoint add-ins and different types of SharePoint add-ins and finally, we will see different ways to package the SharePoint Add-ins or Apps.

I will show you, how to create a site column, content type and a SharePoint Online list in SharePoint hosted add-in or apps using visual studio for SharePoint Online Office 365. Finally, we will see how to add content type to that SharePoint list in SharePoint hosted add-in using Visual Studio.

What is SharePoint Add-ins or Apps?

According to Microsoft, A SharePoint Add-in is a self-contained piece of functionality that extends the capabilities of SharePoint websites to solve a well-defined business problem. And here are a few things on SharePoint Apps or SharePoint add-ins:

  • In the case of SharePoint Add-ins or Apps, then code will never run in the SharePoint server, and this will increase the stability of the SharePoint server. The code will always run outside of your SharePoint server.
  • We can write the business logic using the client-side APIs like Rest API, JSOM, CSOM, etc.
  • By using SharePoint Add-ins, we can develop components like pages, lists, columns, workflows, content types, web parts, list templates, etc.
  • Another thing, we should understand the concepts of Host web and App web or Add-in web. The SharePoint websites where SharePoint Add-ins are installed, and from which users launch them, are called host webs. When the app will open, it will open in a child web inside the host web and that is known as the add-in web.
  • When you install a SharePoint Add-in, you will see a tile in the SharePoint site contents page, from where users can click and navigate to the SharePoint site.

Types of SharePoint Add-ins or Apps

There are two types of SharePoint add-ins or apps we can develop in SharePoint.

  • SharePoint hosted App or SharePoint hosted Add-in: In the case of a SharePoint hosted add-in, the components are mostly in the add-in web. And, here, we will use JavaScript to write our business logic. We can directly write the JavaScript code on the page itself or we can give a reference to a JavaScript file (Custom file).
  • Provider-hosted Add-ins or Provider-hosted apps: We can have the same components that will be a SharePoint hosted add-ins, but in the case of provider-hosted add-ins, at least there will be one remote component like a web application, service or database, etc. that is hosted remotely, outside of your SharePoint farm. Here, we will the client-side object model code, C#.Net by using Microsoft.SharePoint.client.dll Check out an article on How to create provider hosted app in SharePoint online.

How to distribute SharePoint Add-in package?

There are two ways, we can distribute an add-in package in SharePoint.

  • Organization add-in catalog site: You can create an app catalog site in your Office 365 tenant/on-premises and upload the package file in the Apps for SharePoint list. This is a dedicated site collection for deploying add-in packages.
  • Office Store: We can also deploy it to the Microsoft Office store. For this, you need a Seller Dashboard account. Mostly, companies upload the paid apps to the Office store.

How to create a SharePoint hosted Add-in using Visual Studio

Now, let us see, how to create a SharePoint hosted apps or add-in step by step using visual studio.

If you are creating a SharePoint hosted add-in first time for SharePoint Online using Visual Studio 2017/2019, remember to create a SharePoint developer site. If you do not have the developer site created, then create a new SharePoint developer site.

In this particular example, we will create a site column, content type, and then we will add the site column to the SharePoint content type. Then we will create a SharePoint list in the SharePoint hosted add-in, and finally, we will see how to deploy the SharePoint hosted apps or add-in to the SharePoint app catalog site.

Create Site Column

We will see here how to create a Site column in SharePoint using SharePoint hosted Add-in using Visual Studio 2017/2019.

Open the Visual Studio 2017. Go to File->New->Project.

create a SharePoint hosted app in office 365
create a SharePoint hosted app in office 365

The New Project Page will come. Select the “Add-ins” from “Office/SharePoint” which is available in the left panel and “SharePoint Add-in” from the middle panel. In the Name field give the name for the Project. I have given the name as “AddContentTypeToSharePointProductList” the same name will display in the Solution name. Click on OK.

how to create a SharePoint hosted app in SharePoint online
how to create a SharePoint hosted app in SharePoint online

The below screenshot will come when you are creating a new project, it is displaying where your Project will store in your local system.

SharePoint hosted app in SharePoint online step by step
SharePoint hosted app in SharePoint online step by step

New SharePoint Add-in dialogue box will come. In the Dialogue box “What SharePoint Add-in settings” field give the SharePoint developer site URL. Select the “SharePoint-hosted” from “How do you want to your SharePoint Add-in?”. Click on Next.

create a SharePoint hosted app in office 365
create a SharePoint hosted app in office 365

Select the “SharePoint Online” from “What is the earliest version of SharePoint that you want your add-in to target?”. It is by default “SharePoint online”. Here I want to show in SharePoint online so If it is not selected then select the “SharePoint Online”. Click on Finish.

how to create a SharePoint hosted app in SharePoint online
how to create a SharePoint hosted app in SharePoint online

It will take time to Create the “AddContentTypeToSharePointProductList” Project.

how to create a SharePoint hosted app in SharePoint online
how to create a SharePoint hosted app in SharePoint online

Now we can able to see our Project is developed in the right side panel we can able to see the “Solution Explorer” panel. I want to store all the Site column, Content type and list file in one folder So I have created a new folder.

It is a best practice to store all the file in one folder. To create a new folder Go to “AddContnetTypeToSharePointProductList”(Project name). And right click on that we able to see the “Add” option. Go for the “New Folder”.

SharePoint-hosted-add-in
SharePoint-hosted-add-in

I have renamed the folder as “SharePointProductFolder”, right click on that. One box will come to select the “Add” and click on “NewItem”.

how to create a SharePoint hosted app in SharePoint online
how to create a SharePoint hosted app in SharePoint online

In the “Add New Item” page we can able to see List, Content Type, Site Column etc. To add the Site column, Select the “Site Column”. Give name for the Site Column and Click on “Add” button.

create a SharePoint hosted app in office 365
create a SharePoint hosted app in office 365

The Site column is created now. From the right panel, we can see our site column name. I have created the “ProductName” site column as a Single line of text. Like that, I have created a “VendorName” site column whose type is a single line of text.

I have created “ProductCatagory” column as like “ProductName” and “Vendorname” but I want the choice column type. We can set the data type of a column in the Elements.xml page. We need to add below code in the Elements.xml file.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Field
ID="{1459e766-aced-4f45-a1af-4f1d6bcafd47}"
Name="ProductCatagory"
DisplayName="Product Catagory"
Type="Choice"
Required="FALSE"
Group="Custom Site Columns">
<CHOICES>
<CHOICE>Mobile</CHOICE>
<CHOICE>Computer</CHOICE>
</CHOICES>
<MAPPINGS>
<MAPPING Value="1">Mobile</MAPPING>
<MAPPING Value="2">Computer</MAPPING>
</MAPPINGS>
</Field>
</Elements>

One more Site Column I have created named as “UnitPrice”. I want the data type of “UnitPrice” column as Currency. So to set the data type open the Elements.xml file and add the below code.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Field
ID="{0872513b-9268-4d7b-b3e9-c7147c5bb049}"
Name="UnitPrice"
DisplayName="Unit Price"
Type="Currency"
Decimals="6"
Min="0"
Required="FALSE"
Group="Custom Site Columns">
</Field>
</Elements>

Create Custom Content type

To create a custom content type in SharePoint hosted add-in using Visual Studio 2017, follow the below process.

Go to the Folder which we already have created and Right click on it. Then click on Add->New Item.

SharePoint hosted app in SharePoint online step by step
SharePoint hosted app in SharePoint online step by step

Select the “Content Type”. Give a Proper name for the Content Type and click on “Add”.

create SharePoint hosted app in office 365
create SharePoint hosted app in office 365

Now “Choose Content Type settings” page will come. Select the Parent for the content type, the content type inherited from which parent type. It is by default “Item” is selected. We can change the Type. Click on “Finish”.

how to create a SharePoint hosted app in SharePoint online
how to create a SharePoint hosted app in SharePoint online

Add Site Column to Content type

After the SharePoint content type created automatically, the Add a Site column to the content type page will come. We can add the Site column by clicking on “Click here to add a Column”.

Here I have added the below columns:

  • Product Name (Single Line of Text)
  • VendorName (Single Line of Text)
  • Product Catagory (Choice column)
  • Unit Price (Currency Column).
create a SharePoint hosted app in office 365
create a SharePoint hosted app in office 365

Create a SharePoint List using SharePoint Hosted Add-in Or App

Now we will see how to add a list using SharePoint hosted add-in or apps using visual studio 2017.

Right Click on “SharePointProductFolder”->Add->New Item.

Select the “List” and give a proper name for SharePoint List. I have given the name “Products”.

SharePoint hosted app in SharePoint online step by step
SharePoint hosted app in SharePoint online step by step

List Settings page will come. In the “What name do you want to display for your list?” we can able to see the list name which I have given as a List Name.

In the “Do you want to create a customizable list template or a list instance based on existing list type?” option two filed are there

  • “Create a customizable list template and a list instance of it”: By default, it is selected the Custom List. If You want to create Announcements, Calendar, Contacts or any other type of template for List, then choose from the dropdown and select it. Click on Finish.
how to create a SharePoint hosted app in SharePoint online
how to create a SharePoint hosted app in SharePoint online
  • Another field is “Create a list instance based on an existing list template”.If We do not want to create a new one we want to use existing List template then go for the second option. In the drop-down, we can able to see all the list template available in the developer site. But I have chosen the first option.
SharePoint hosted app samples
SharePoint hosted app samples

Create SharePoint List Views

After creating the new list successfully the below screenshot page will come. We can create “Views“, add “Content Types” to ist and we can directly add the site column to the list from “Type a new or existing column name” option.

how to create a SharePoint hosted app in SharePoint online
how to create a SharePoint hosted app in SharePoint online

To add the field in the “All items” view, add the fields to “Selected columns”. Here I have added “Title”, “Created By” and “Modified By” field to “All Items” views.

SharePoint hosted app samples
SharePoint hosted app samples

Setup SharePoint List Properties

In the “List”  we can able to see all the list details list Title, List URL, Description and we can see one more option. “Display list at Quick Launch“: If we do not want to see the App in the Quick lunch we can uncheck the text box.

SharePoint hosted app deployment
SharePoint hosted app deployment

To Add content type to the SharePoint list, click on “Content Types”. We can able to see one box where all the content type will available. We can the Content type here. I have added the Item content type (by default), folder and “ProductDetsils”(we select from the dropdown). Click On Ok.

create a SharePoint hosted app in office 365
create a SharePoint hosted app in office 365

Now we need to give Permission for the Site. So click on “AppMainfest.xml” which is present in the left panel. We can able to see the “Permission” option in the ribbon. I have given the full control permission for Site collection, Web and List.

sharepoint 2013 app development step by step
sharepoint 2013 app development step by step

Deploy SharePoint hosted Add-in or App using Visual studio

To deploy the SharePoint hosted App to the developer site, go to the Project name. My project name is “AddContnetTypeToSharePointProductList”, right click on it. Then Click on “Deploy”.

what is sharepoint hosted app
sharepoint hosted app in sharepoint online

After Click on Deploy wait for some time. It will take time. After some time we will get a page like below it is asking for permission. I will ask “Do you trust the AddContentTypeTpSharePointProductList?”. Click on “Trust It”.

sharepoint hosted app tutorial
sharepoint hosted app tutorial

We are giving the Permission the app will available on the SiteContent page of developer site. On the page, it is displaying Hello and display name of logged in user.

what is sharepoint hosted app
what is sharepoint hosted app

To see the list item type your developer https://Site URL/AddContentTypeToSharePointProductList(AppName)/Lists/Products(Listname)

SharePoint 2013 app development step by step
SharePoint 2013 app development step by step

This is how we can deploy a SharePoint hosted add-in or apps to the app catalog site and use in the SharePoint site.

SharePoint Hosted Add-in or Apps – Video Tutorial

Here is a video tutorial on the SharePoint hosted app, I have explained, how to create a site column, content type, add site columns to a content type and how to create a list in SharePoint hosted add-in using visual studio 2017.

Remove app from SharePoint Online using PowerShell

Let us see, how to remove an app from SharePoint Online site using PowerShell. Here I have a SharePoint online developer site that has few SharePoint hosted add-ins deployed to the site.

Remove SharePoint online Add-in or Apps using Browser

We can easily remove SharePoint add-in or apps using a browser. Open the site and then go to the “Apps in Testing” library (I am doing this in a developer site). You can read about an article on the Developer site template overview and also you can read how to create a developer site in Office 365 SharePoint online.

Then click on (…) on the particular Add-in you want to remove. Then click on Remove like below.

remove app from sharepoint online
remove app from sharepoint online

This will remove the add-in from the site.

Remove app from SharePoint Online using PowerShell

But sometimes while removing an add-in may be from the visual studio or manually, it does not remove properly. Sometimes it gave an error like: sorry we couldn’t remove the app.

In these cases, the best way is to remove the SharePoint hosted add-in by using PowerShell. But we need to first retrieve the AppInstanceId.

How to retrieve AppInstanceId of Add-in or APP in SharePoint online?

Open the site and then go to the “Apps in Testing” library. And then click on the (…) and then click on VIEW LOGS like below:

remove app sharepoint online powershell
remove app sharepoint online powershell

Then from browser, you can see the AppInstanceId like below:

sharepoint online remove app using PowerShell
sharepoint online remove app using PowerShell

Once the AppInstanceId is ready now we can run the below PowerShell script to remove the Add-in.

PowerShell script to remove SharePoint Add-in

You can run, debug and test the below PowerShell script in a Windows PowerShell ISE or inside a visual studio code. Make sure to give the below dll references:

  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dll
Add-Type -Path "E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$AppInstanceid = New-Object Guid("6F91CD30-7B5D-4A08-A06E-A72A82051813")
$siteUrl = "https://onlysharepoint2013.sharepoint.com/sites/DevSite/"
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$securePassword=ConvertTo-SecureString "**********" -AsPlainText -Force
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials(“bijay@onlysharepoint2013.onmicrosoft.com”, $securePassword)
$web = $ctx.Web
$appInst = $ctx.Web.GetAppInstanceById($AppInstanceid)
$appInst.Uninstall();
$ctx.ExecuteQuery()

Once you run the above PowerShell script, you can see the removing status, here you can see it is showing Uninstalling.

sharepoint online remove app or add-in using Powershell
sharepoint online remove app or add-in using Powershell

After some time if you will refresh, the SharePoint add-in will already be removed from the SharePoint site.

This is how to remove app from SharePoint Online using PowerShell.

There was a problem with activating the app web definition

Now, let us see, how to solve “Error occurred in deployment step ‘Install SharePoint Add-in’: Failed to install SharePoint Add-in. Please see the output window for details. There was a problem with activating the app web definition. Cannot complete this action.” The error comes while developing SharePoint hosted add-in or apps using visual studio 2017/2019 for SharePoint Online or SharePoint 2013/2016.

Recently in a SharePoint training, I was explaining about SharePoint hosted add-in development and deployment using visual studio, but while deploying I got the error: Error occurred in deployment step ‘Install SharePoint Add-in’: Failed to install SharePoint Add-in. Please see the output window for details.

There was a problem with activating the app web definition. Cannot complete this action
There was a problem with activating the app web definition. Cannot complete this action

When I checked the output window, I can see an error like: There was a problem with activating the app web definition. Cannot complete this action.

There was a problem with activating the app web definition sharepoint 2013
There was a problem with activating the app web definition sharepoint 2013

Reason:

In the SharePoint hosted add-in I was creating a site column, content type and then I was creating a SharePoint custom list. Then in that list, I was trying to add the content type in SharePoint Online.

Solution:

Not sure, if it the solution will work every time. But it worked for me.

  • First I retract the app and clean the solution in visual studio 2017/2019.
  • Then I deleted the content type and SharePoint Online list from the Add-in project.
  • And finally, I created the content type and added the existing Site columns to the content type.
  • Then created the SharePoint custom list to the add-in project and added the content type to the list.
  • Then I deployed the project. This time it deployed the Add-in correctly without giving any error.

The page you selected contains a list that does not exist. It may have been deleted by another user

Recently I was trying to deploy a SharePoint hosted add-in from visual studio 2015 to a SharePoint online site. But while deploying I got the below error. Error occurred in deployment step ‘Install SharePoint Add-in’: Failed to install SharePoint Add-in. Please see the output window for details.

The page you selected contains a list that does not exist
The page you selected contains a list that does not exist

When I checked the output window, I can see the error like below:

The page you selected contains a list that does not exist. It may have been deleted by another user.

It may have been deleted by another user error in SharePoint Add-in
It may have been deleted by another user error in SharePoint Add-in

The page you selected contains a list that does not exist

I have created a SharePoint hosted add-in using visual studio and deployed to SharePoint online site.

After that, I have added a workflow into that add-in. And after that, I changed the Integrated App to True like below, so that the workflow will be available inside the host web.

Error occurred in deployment step Install SharePoint Add-in Failed to install SharePoint Add-in. Please see the output window for details.
Error occurred in deployment step Install SharePoint Add-in Failed to install SharePoint Add-in. Please see the output window for details.

The workflow has been attached to the add-in web list. Now after deploy I got the above error.

The solution I figure out is that while adding the workflow, in the SharePoint customization wizard, uncheck the “Yes, associate this workflow with the following libraries and lists. If this step is omitted, you must manually associate the workflow after it is created.” option like below:

SharePoint add-in workflow deploy to host web
SharePoint add-in workflow deploy to host web

If you have already added the workflow, but you want to uncheck the above box, I did not get any option to do this. So better option is to add the workflow once again and uncheck the above option like above. Then the error will not come. This is how to fix the error, The page you selected contains a list that does not exist. It may have been deleted by another user.

Error occurred in deployment step ‘Install SharePoint Add-in’: An instance of this App already exists at the specified location

Recently I was trying to develop a SharePoint hosted add-in using visual studio 2015 and was trying to deploying to a SharePoint online developer site. But while deploying I got the below error, even I tried to deploy multiple times but every time it throws some error.

The error message comes as:
Skipping the uninstall step because the SharePoint Add-in is in an invalid state and cannot be uninstalled.
Error occurred in deployment step ‘Install SharePoint Add-in’: An instance of this App already exists at the specified location

Skipping the uninstall step because the SharePoint Add-in is in an invalid state and cannot be uninstalled
Skipping the uninstall step because the SharePoint Add-in is in an invalid state and cannot be uninstalled

Sometimes the below error also came:
Error occurred in deployment step ‘Install SharePoint Add-in:’ The remote server returned an error: (503) Server Unavailable.

Error occurred in deployment step Install SharePoint Add-in The remote server returned an error (503) Server Unavailable
Error occurred in deployment step Install SharePoint Add-in The remote server returned an error (503) Server Unavailable

Error occurred in deployment step ‘Install SharePoint Add-in’: An instance of this App already exists at the specified location

The errors were looking strange because I have not modified anything while developing the add-in, it was just a basic add-in without any customization. But still it was giving the same error, even I tried to redevelop the SharePoint hosted add-in but no success.

Sometimes the add-in did not uninstall properly then it will give an error like above. In those cases, you can remove the add-in from “Apps in Testing” in the developer site. You can select the add-in and then click on … and then click on REMOVE button like below:

error occurred in deployment step install app for sharepoint failed to install app for sharepoint
error occurred in deployment step install app for sharepoint failed to install app for sharepoint

Alternatively, you can also remove the add-in from the Site Contents page, Open the Site Contents page and then click on … and then click on Remove like below:

The remote server returned an error (503) Server Unavailable sharepoint online
The remote server returned an error (503) Server Unavailable sharepoint online

In my case it was different, Then I found out that there is a Service degradation in the SharePoint Online going on in the Office 365 tenant.

That information I found in the Office 365 Admin center Servce health section.

The below was the full message.

User impact: Admins may have been unable install or activate SharePoint add-ins or SharePoint Framework solutions.
Latest message: Title: Can’t install or activate SharePoint add-ins or SharePoint Framework solutions

User Impact: Admins may have been unable install or activate SharePoint add-ins or SharePoint Framework solutions.

Final status: We’ve completed deployment of the fix and confirmed that service has been restored.

Scope of impact: This issue could have potentially affected any admin attempting to install or activate SharePoint add-ins or SharePoint Framework solutions.

Start time: Monday, February 5, 2018, at 5:00 AM UTC

End time: Thursday, February 8, 2018, at 8:00 PM UTC

Preliminary root cause: A coding issue resulted in a problem in which admins were unable to install or activate SharePoint add-ins or SharePoint Framework solutions.

Next steps:
– We’re reviewing our update procedures to better identify similar issues during our development and testing cycles.

Admins may have been unable install or activate SharePoint add-ins or SharePoint Framework solutions
Admins may have been unable install or activate SharePoint add-ins or SharePoint Framework solutions

Once the Service is restored, I am able to deploy the same SharePoint hosted add-in without changing anything.

You may like the following SharePoint apps or ad-in tutorials:

Conclusion

In the above SharePoint hosted app or add-in example, we learned some important things about the SharePoint hosted app. We knew

  1. How to create a site column in SharePoint hosted app or add-in using Visual Studio 2017/2019
  2. How to create a custom content type in SharePoint hosted app or add-in in SharePoint Online Office 365.
  3. How to add the site column to content type in SharePoint hosted app using visual studio 2017/2019 in SharePoint Online Office 365.
  4. How to create a new list in the SharePoint hosted app in the SharePoint hosted app
  5. How to enable the field from the view in SharePoint hosted app
  6. How to add a content type to SharePoint list in SharePoint hosted app
  7. How to deploy the SharePoint hosted app using visual studio to SharePoint Online developer site.
  8. How to Remove app from SharePoint Online using PowerShell
  9. Fix error: There was a problem with activating the app web definition
  10. Fix error: The page you selected contains a list that does not exist. It may have been deleted by another user
  11. Error occurred in deployment step ‘Install SharePoint Add-in’: An instance of this App already exists at the specified location
>