SharePoint hosted add-in (app) is one of the two ways to create SharePoint add-ins, the other one being Provider Hosted Add-ins. SharePoint hosted add-ins can be used to deploy lists, custom pages,workflows, web parts and other components. We can have only JavaScript code in the pages that are deployed along with SharePoint Hosted add-in. In order to ensure that we have the required templates go to Visual Studio. From Office/SharePoint section check if the ‘SharePoint Add-in’ template is available. If it is not present we will have to install Office developer tools to add the SharePoint 2016 templates along with Office Templates. You can check out how to set up the Visual Studio environment for SharePoint 2016 here.
Once the visual studio templates for SharePoint 2016 are made available let’s get started with SharePoint hosted add-in creation. From Visual Studio select the SharePoint Add-in and specify the add-in name.
Mention the Office 365 SharePoint Online developer site URL and select the SharePoint Add-in radio button.
On clicking Next, it will pop up the Office 365 Authentication Window.
Click on Sign in. Specify the SharePoint Target version as SharePoint Online and click on Finish.
It will establish a communication with SharePoint and create the SharePoint Add-in project structure.
The SharePoint Hosted Add-in Solution structure will like below.
We can add SharePoint Components like Applicationpages,Lists,Workflows etc. by right clicking the Solution. Select New Item and click on Add.
This will provide us the options that can be added to the project. For the time being I am trying to deploy a List with some prepopulated data to SharePoint Online as an Add-in.
Select List and specify the list name. Click on Add. Specify the type of List we are trying to create. In this case it is a normal custom list. Click on Finish.
It will add the list. Now we can add new columns and specify the data type using the UI. I have added the below columns to the list.
- Employee Name
- Employee Address
- Previous Company
- Experience
We can add default data to the list instance by clicking on the Elements.xml file of the list instance. Each Row field will indicate a SharePoint List item and the Field tag represents the column within the list.
- <Data>
- <Rows>
- <Row>
- <FieldNameFieldName="Employee Name">Rajesh</Field>
- <FieldNameFieldName="Employee Address">Kochi,India</Field>
- <FieldNameFieldName="Previous Company">UB Group</Field>
- <FieldNameFieldName="Experience">4</Field>
- </Row>
- <Row>
- <FieldNameFieldName="Employee Name">Jinesh</Field>
- <FieldNameFieldName="Employee Address">Bangalore,India</Field>
- <FieldNameFieldName="Previous Company">IBM</Field>
- <FieldNameFieldName="Experience">12</Field>
- </Row>
- </Rows>
- </Data>
Now we have to change the start page of the SharePoint Hosted Add-in by specifying the List path in the format SolutionName/Lists/ListName. Here it is ‘SharePointHostedAddIn/Lists/Employee’.
We can set the adequate permissions required from the permissions tab of the AppManifest.xml file.
Finally we are all set to publish the app to SharePoint Online. Right Click the solution and click on ‘Publish’.
This will lead us to the publishing page. Click on ‘Package the add-in’.
Visual Studio will do a build and will create the .app package in the local file system.
It will generate the .app file. If we want to analyze the contents we can take a copy of the .app file and rename it to .cab and unzip it.
Now we can upload the .app file to the App catalog. If the catalog is not present, you can refer this article which describes the app catalog creation in SharePoint Online. Once inside the App Catalog, Click on Apps for SharePoint.
Click on Upload and select the .app file within the local file system.
The upload has been completed. Do a refresh of the page.
The SharePoint Hosted add-in has come up in the app catalog.
Now we can add this add-in to the site from the site contents. Doing a simple search from the site contents search box will list out the recently uploaded add-in.
Click on the add-in. It will ask whether to trust it. Click on Trust It.
It will start adding the add-in to the site contents.
Clicking on the add-in will take us to the list which was set as the start page of the add-in. Inside it, we can see the default list items that were set in the elements.xml file.
Summary - Thus we saw how to deploy a SharePoint Hosted Add-in to SharePoint Online in Office 365.