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.
-
May be not!!! In provider hosted app, there is. Can you explain why you need that.– Atish Kumar DipongkorCommented Mar 31, 2016 at 7:19
-
I have to write code to create lists when app is installing/adding to SharePoint site. I have 3 to 4 app parts in my app. Now I have write code to create lists in site where app is installed, where I have to add my code to create lists– Lavanya PuppalaCommented Mar 31, 2016 at 7:26
2 Answers
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.
It's not hard to implement your own JavaScript "App start" functionality to do updates. The basic process is:
- Check to see if you need to update
- Ensure current user has permissions to do the update
- Do the update
For more information check out this blog post the Chris O'Brien: JavaScript-based provisioning in SharePoint/Office 365
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.
Additionally, since SharePoint hosted apps can only run JavaScript, all of the custom code they run must be done as the user who is using the app (there is no "app permission" level to leverage here). The app does not run the custom logic in your JavaScript, it is actually the users using the app that run the JavaScript defined there.
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").