Click here to Skip to main content
15,881,709 members
Articles / Productivity Apps and Services / Sharepoint

My First Sharepoint-Hosted App in 2013

Rate me:
Please Sign up or sign in to vote.
4.91/5 (14 votes)
27 Dec 2012CPOL5 min read 174.1K   10   25
CodeProjectI would recommend to read Apps in SharePoint 2013 post before going through this one.I want to create a SharePoint-Hosted App in 2013 using Visual Studio.I have created a SharePoint 2013 machine with all required software.Just as a best practice, even if you are creating a standalone
I would recommend to read Apps in SharePoint 2013 post before going through this one.
I want to create a SharePoint-Hosted App in 2013 using Visual Studio.
I have created a SharePoint 2013 machine with all required software.
Just as a best practice, even if you are creating a standalone server, enable Active Directory Domain Services.
SharePoint 2013 works better when the system is in a Domain, rather than in a workgroup.

So, I have installed Windows server 2012, enabled all required features. Installed Office 2013, SharePoint 2013, Visual Studio 2012 and Developer tools for SharePoint 2013.

We will create an "App for SharePoint" and try to understand the scaffold code and try to deploy it and see what can happen.

Step 1: Open Visual studio and select "App for SharePoint".

Provide required data. While selecting the hosting option, remember to select the hosting option as "SharePoint-Hosted". You can select Auto-Hosted if you want to deploy your App to Windows Azure. Provider-Hosted if you want to deploy the App to any other hosting Providers.


Step 2: Default "App" code is created and lets have a look at its content.
Project is created with a Default.aspx page and few script files and configuration files. First thing we need to know is about "Default.aspx" page. We already read that every SP-Hosted App we Install/Deploy will be created as a Subweb under SP Web application. That Subweb will be defaulted to this specific page. When user click on the App, he will be navigated to this page first. So better not to mess with the name of the file.
Lets see the content of the file now.

Step 3: I have attached the snapshot of the "Default.aspx" file and see what is the significance of each part.

The first highlighted box refers to the Framework provided JavaScript libraries. As we will be using Client Side Object Model(CSOM), these Jscript libraries will provide the necessary support while execution of our custom code.

Second highlighted box refers to the local script file pertaining to this specific app. We will see in detail about this in next step.

Third one is the sample code I added. We will not be doing extensive coding right now. The objective is to see if there are any hurdles in deploying an App.

Step 4: Open App.js file under scripts. This file content is responsible for Loading the Context and Web information in which the App is running. Look at the code and its almost self explanatory.


Step 5: Now I tried to deploy the App.
Oops, got an error.

" Error occurred in deployment step 'Install app for SharePoint': Failed to install app for SharePoint. Please see the output window for details."

Ok, looked at Output window found another reason.

 " ErrorDetail: Apps are disabled on this site. "

Now we need to remember what we learned in prior post.  Any SharePoint-Hosted App will try to create a Subweb which will be executed in a separate AppDomain apart from SharePoint Farm.
Currently SharePoint dint have any clue on where to Host this app code and what to do with it.

Step 6: We need to create a new App Domain, and register it with SharePoint. So next time when you deploy an App, it know where to host it.
I created my machine on a Domain "Sharepoint13.com". So I would like to create an App Domain "Sharepoint13Apps.com" for hosting purpose. For that go to
Administrative Tools > DNS > following window will b opened showing the Machine Name and the current App Domain in which SharePoint farm is executing.

Right click on Forward Lookup Zone and Click on
"New Zone" > "Next" > "Next" > "Next" > following window will open.

Now the new AppDomain is created, Now we will create a wildcard Alias (CNAME) record to allow apps to create unique domain names within our app domain, Sharepoint13Apps.com. Right-click Sharepoint13Apps.com and select Refresh. Then right-click Sharepoint13Apps.com and select New Alias (CNAME)…
Give the values as I have shown and Click OK.

Now App Domain is created, lets test it now. Open CMD window and try pinging to "abc.Sharepoint13Apps.com". . . Successfully done.

Step 7: We need to register this newly created app domain with SharePoint. Go to Central admin, click on Apps on right menu.
it will give you a message saying

"The Subscription Settings service and corresponding application and proxy needs to be running in order to make changes to these settings. "

This means a new service application running in its own app pool needs to be created and up and running.
Lets create a Managed account with Machine admin credentials. Mine is
"Sharepoint13\Administrator".
Open the PowerShell window and execute below script.

add-pssnapin "Microsoft.Sharepoint.Powershell"

$account = Get-SPManagedAccount sharepoint13\administrator

Remove-SPServiceApplicationPool -Identity SettingsServiceAppPool

$appPoolSubSvc = New-SPServiceApplicationPool -Name SettingsServiceAppPool -Account $account

$appSubSvc = New-SPSubscriptionSettingsServiceApplication -ApplicationPool $appPoolSubSvc -Name SettingsServiceApp -DatabaseName SettingsServiceDB

$proxySubSvc = New-SPSubscriptionSettingsServiceApplicationProxy -ServiceApplication $appSubSvc

It may prompt you once, say Yes.
Some times the above script may give error

"Access denied. Only machine administrators are allowed to create administration service job definitions"
Go to "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\CONFIG\POWERSHELL\Registration" and execute SharePoint.ps1 as Administrator.
Issue will be resolved.

Once this script is successfully executed. We have created a new service Application, and its proxy which will run under its own Service App Pool. To verify this go to
Central Admin > Service Applications > Manage Service Applications > you can see below Svc application created , up and running.
Step 8: Now go to Central Admin > Apps > Configure App URLs

You can see that the new App Domain we created automatically populated here, just mention app prefix as "Apps" and say OK.

We are all set to deploy our SharePoint-Hosted App. Right click on Solution and say "Deploy" 

Once App is deployed successfully, go to SharePoint Application and click on "Site Actions" the little gear symbol on top right and click on Site Content. You can see your App there.

Lastly we have to observe one more thing before closing this topic. Observe the url of site and the url of the App after loading.
We can see SharePoint-Hosted App loaded in different App Domain thus causing no harm to Actual SharePoint Farm in any circumstances.

                      Objective Achieved.
Is it helpful for you? Kindly let me know your comments / Questions.
This article was originally posted at http://pratapreddypilaka.blogspot.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionDid not work for me Pin
Mohammad Abu-Ali31-Jul-17 6:19
Mohammad Abu-Ali31-Jul-17 6:19 
QuestionPerfect! Pin
Rockie(aka Collapse Troll)3-Jul-16 7:51
Rockie(aka Collapse Troll)3-Jul-16 7:51 
Questionerror while trying to Configure an environment for apps for SharePoint (SharePoint 2013) Pin
Madhav Gunjal24-May-16 23:17
Madhav Gunjal24-May-16 23:17 
QuestionPing request fails after app domain creation Pin
astrixtech29-Oct-15 1:56
astrixtech29-Oct-15 1:56 
AnswerRe: Ping request fails after app domain creation Pin
Madhav Gunjal24-May-16 23:19
Madhav Gunjal24-May-16 23:19 
GeneralMy vote of 5 Pin
Aakash Kumar Morya12-Oct-15 0:06
Aakash Kumar Morya12-Oct-15 0:06 
QuestionSharePoint Hosted App is Deployed successfully but it is showing blank page. Pin
Member 1116150117-Oct-14 5:48
Member 1116150117-Oct-14 5:48 
AnswerRe: SharePoint Hosted App is Deployed successfully but it is showing blank page. Pin
tony_thescientist22-Dec-14 2:42
tony_thescientist22-Dec-14 2:42 
Question5 Pin
youdhbir_singh30-Sep-14 0:48
youdhbir_singh30-Sep-14 0:48 
QuestionSharePoint-Hosted Apps are working from local domains Pin
svpoorna4-Feb-14 17:44
svpoorna4-Feb-14 17:44 
QuestionAsk for Credentials Pin
mohamedakb11-Dec-13 23:29
mohamedakb11-Dec-13 23:29 
AnswerRe: Ask for Credentials Pin
PratapReddyP14-Jan-14 12:54
PratapReddyP14-Jan-14 12:54 
Bugrt Pin
Singh Vijay Kumar8-Oct-13 1:49
professionalSingh Vijay Kumar8-Oct-13 1:49 
GeneralRe: rt Pin
PratapReddyP8-Oct-13 10:19
PratapReddyP8-Oct-13 10:19 
QuestionError i have face Pin
ravinsingh24-Jul-13 4:11
ravinsingh24-Jul-13 4:11 
sir,
your post is nice. when I follow you post I getting error. then I have follow http://technet.microsoft.com/en-us/library/fp161236.aspx this link. it's working for me. your post missing "App Management service application create" part. when I create this then it's working. Please verify that.
Questionyou sir, are a genius. Pin
homerinasia25-Jun-13 9:05
homerinasia25-Jun-13 9:05 
QuestionError Pin
Member 96551808-Apr-13 21:28
Member 96551808-Apr-13 21:28 
GeneralNice post Pin
pydi.suresh20-Mar-13 3:11
pydi.suresh20-Mar-13 3:11 
GeneralMy vote of 5 Pin
pydi.suresh20-Mar-13 3:10
pydi.suresh20-Mar-13 3:10 
QuestionNeed Help Pin
naveenkumarmaddipati4-Mar-13 1:44
naveenkumarmaddipati4-Mar-13 1:44 
AnswerRe: Need Help Pin
naveenkumarmaddipati5-Mar-13 19:27
naveenkumarmaddipati5-Mar-13 19:27 
QuestionError occurred in deployment step 'Install app for SharePoint': The System Account cannot perform this action. Pin
Akshay Srinivasan222-Jan-13 4:45
Akshay Srinivasan222-Jan-13 4:45 
AnswerRe: Error occurred in deployment step 'Install app for SharePoint': The System Account cannot perform this action. Pin
Andi Fandrich15-Feb-13 1:01
professionalAndi Fandrich15-Feb-13 1:01 
GeneralRe: Error occurred in deployment step 'Install app for SharePoint': The System Account cannot perform this action. Pin
vivekthangaswamy30-Sep-13 15:09
professionalvivekthangaswamy30-Sep-13 15:09 
GeneralMy vote of 5 Pin
Akshay Srinivasan26-Jan-13 23:36
Akshay Srinivasan26-Jan-13 23:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.