I have to create list when SharePoint hosted app is installing. Is there any function call in SharePoint which runs when app is adding or installing in SharePoint site.
If you just need to create a list, you can do it using CAML. The SharePoint-Hosted App contains a sandbox solution. When the App is installed the Feature in the sandbox solution is activated and the list will be created.
The ability to do this through code is not built-in without using a Remote Event Receiver. But, if you use a Remote Event Receiver, you are no longer building just a SharePoint-Hosted App. You are also building a Provider-Hosted App.
- Check to see if you need to update
- Ensure current user has permissions to do the update
- Do the update
I plan on covering this in detail in a Pluralsight that I'm currently working on.
There is no initialization code that can be run when a SharePoint hosted app is installed unless you have set up a separate farm solution or remote event receiver. You can create declarative components in the app web, but as you've likely realized this does not work for creating items in the host web.
A common pattern to solve this issue is to direct the person who installs the app (who should have admin privileges on the site where they installed it) to visit a page on your app that sets everything up for them running under that user's privilege level. This can be the first place the full page experience in your app begins, you can later check if the setup has been run and direct users to other pages within your app as necessary.
There is some great information out there on implementing logic you would have previously put into a SharePoint customization (server or sandbox solution, for example), I would recommend reviewing the Microsoft Virtual Academy course: "Transform SharePoint Customizations to SharePoint App Model." as well as looking at some great examples on the Office Dev Patterns and Practices repository on GitHub. Specifically, you should find the information on the "provisioning pattern" useful; make sure you're looking at SharePoint hosted examples (often marked with the term "JSOM").