Wednesday, 18 February 2015

How to create a provider hosted (high trust) app for sharepoint ?

A Provider Hosted App is a high-trust app for SharePoint. A high trust app uses a digital certificate to establish a trust between remote web application and SharePoint 2013.
You’ll create a .pfx certificate file first, and then a corresponding .cer file.

To create a self-signed .pfx certificate file
1.     Goto the IIS manager, select the ServerName Node in tree view on the left.
Select the Server Certificates option in IIS.
How to create a provider hosted (high trust) app for sharepoint ?
1.      Select the Create Self-Signed Certificate link from the set of links on the right side.
2.      Name the certificate HighTrustSampleCert, and then click Ok.
3.      Right-click the certificate, and then select Export,
4.      Create a folder called C:\Certs.
5.      Back in IIS Manager, export the file to C:\Certs and give it a password.

To create a corresponding .cer file

1.      On the SharePoint server, be sure that the app pool identity for the following IIS app pools have Read rights to the C:\Certs folder:
o    SecurityTokenServiceApplicationPool
o    The app pool that serves the IIS web site that hosts the parent SharePoint web application for your test SharePoint website. For the SharePoint – 80 IIS website, the pool is called OServerPortalAppPool.

2.      In IIS manager, select the ServerName node in the tree view on the left.
3.      Double-click Server Certificates.
4.      In Server Certificates view, double-click HighTrustSampleCert to display the certificate details.
5.      On the Details tab, choose Copy to File to launch the Certificate Export Wizard, and then choose Next.
6.      Use the default value No, do not export the private key, and then choose Next.
7.      Use the default values. Choose Next.
8.      Choose Browse, browse to C:\Certs, name the certificate HighTrustSampleCert, and then choose Save. The certificate is saved as a .cer file.
9.      Choose Next.
10.  Choose Finish.


You have all the required the permissions now. Now you can start the project .
1.      Start Visual Studio 2012 by using the Run as administrator option.

2.      In Visual Studio 2012, on the File menu, choose New, and then choose New Project

3.   In the New Project dialog box, expand the Visual C# node, expand the Office/SharePoint node,           and then choose the Apps node. Choose App for SharePoint 2013.
How to create a provider hosted (high trust) app for sharepoint ?

4.      Name the project, and then choose the OK button.

5.      In the first Specify the App for SharePoint Settings dialog box, name your app and provide the URL of the SharePoint 2013 site that you want to use to debug your app. Under How do you want to host your app for SharePoint, choose Provider-Hosted Choose Finish.

       6. On Selection of provider Hosted, Click Next. The below Screen will be asking the Certificates.  Give the          User certificates, password and Issuer ID and click Finish.The Certificate needs to be created on the                    SharePoint machine and pfx file needs to be exported and shared with the Visual Studio Machine.
How to create a provider hosted (high trust) app for sharepoint ?

1         7.  Now, the Solution has been created.
How to create a provider hosted (high trust) app for sharepoint ?
8.      Solution has 2 projects,
a.      SharepointApp2
b.      SharepointApp2Web
9.      Go to the property of AppWeb project and make the .NET FRAMEWORK4.5 and create a virtual directory will be helpful to host our AppWeb on local IIS .

How to create a provider hosted (high trust) app for sharepoint ?

10.   By clicking Virtual Directory button, Virtual Directory will be created on local IIS.
11.   After property configuration, we are ready with our provided Hosted Application. Rebuild the solution.
12.  Go to the AppManifest.xml in  SharePointApp2 project and set the start page attribute to IIS Directory.

How to create a provider hosted (high trust) app for sharepoint ?
13.      Go to the permission tab. Add the corresponding permission and full control.
14.       Now we will  registered our Client ID with SharePoint, before deploying App. Client ID is nothing but GUID. You can generate the client ID by reaching the APPregnew.aspx inside the 15 hive.
15.       Go to the APPmanifest.xml of SharePointApp2 project and give the client ID.
            <AppPrincipal>
                  <RemoteWebApplication ClientId="*" /
             </AppPrincipal>
4          16. Go to the WebConfig file and add the client ID with below key.

<appSettings>
<add key="ClientId" value="1de402c2-911a-47f5-8b51-fd8b57144c41"/>
<add key="ClientSecret" value="7Q1y02pvvWMBW7fzlAEnHsSGGATFWra1YEFCIo117sg="/>
<add key="ClientSigningCertificatePath" value="C:\MyCertificate.pfx"/>
   <add key="ClientSigningCertificatePassword" value="****"/>
  <add key="IssuerId" value="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx"/>
 </appSettings>

17.Now deploy the Solution. So after successful deployment it will show like a page.

How to create a provider hosted (high trust) app for sharepoint ?
Click the Trust it. The App will get installed. We can lunch the app from our SharePoint Portal. 


Monday, 16 February 2015

How to create SharePoint list picker control using Javascript (pickertreedialog.js) ?

Reference:

<script type="text/javascript" src="/_layouts/1033/pickertreedialog.js"></script>

HTML Controls:

<div id="tralert" style="display: none">
   <div class="informationBar">
      <span id="lblMessage"></span>
   </div>
</div>

<div>
<input type="text" id="txtListPicker" readonly="readonly" />
<input type="button" id="btnListPicker" onclick="listPickerScript();" value="..." />
</div>

JavaScript:


//Global variables

var strListGuid, strListUrl;

//Launch list picker
function listPickerScript() {
try {
var callback = function (arr) {
if (arr == null || arr == undefined)
return;

strListUrl = arr[3];

strListGuid = getListGUID(arr[0]);

document.getElementById('txtListPicker').value = arr[2];
 
try {
if (arr[2] == "") {
document.getElementById('tralert').style.display = "block";
document.getElementById('lblMessage').innerHTML = "Don't select a site. Select a list.";

}
 
else {
document.getElementById('tralert').style.display = "none";
}

}
 
catch (exp) { }

}
 
SP.SOD.executeFunc('pickertreedialog.js', 'LaunchPickerTreeDialog', function () {
LaunchPickerTreeDialog("RouterPickerSelectListTitle", "RouterPickerSelectListText", "websListsFolders", "", appweburl, "", "", "", "/_layouts/images/Copy.gif", 0, callback, '', '');

});

}
 
catch (exp) { }

}

 
//Get GUID of List
function getListGUID(strGUID) {
try{
var strSplit = strGUID.split("?");
strSplit = strSplit[0].split(":");
return strSplit[1];

}
 
catch (exp) { }

}

Saturday, 14 February 2015

How to configure environment for SharePoint hosted apps on premise?



Note : All the configuration setup should be done using the system account.
Setup DNS for App domain:
To set up DNS go to Administrative tool and select DNS.

How to configure environment for SharePoint hosted apps on premise?

All Apps on SharePoint have unique URL domain names, so before we’re done, we need to set up a wildcard alias.
A wildcard alias basically means you can add anything in front of the domain name and point it to a specific address.
In the DNS manager select for your domain and right click select “New Alias”.

How to configure environment for SharePoint hosted apps on premise?

In the New Resource Record dialog box, enter an asterisk (*) as the Alias name, and then either browse to the record for your server or type in the DNS-name of your server. See the below screenshot.
How to configure environment for SharePoint hosted apps on premise?

Click ok and then ok.
How to configure environment for SharePoint hosted apps on premise?
Setup DNS for Services:

·         Go to central admin, under Application management and select manage services on the server and check for the below 2 service:
    • App Management Service
    • Microsoft SharePoint foundation Subscription Settings Service
  • These 2 services shouldn’t be stopped It should be started.
  • If it is stopped please run the below PowerShell script`
  • Before running powershell scripts use “Start-Transcript”, so that you can track all the commands you are executing in SharePoint 2013 management shell
  • Get-SPServiceInstance | where{$_.GetType().Name -eq "AppManagementServiceInstance" -or $_.GetType().Name -eq "SPSubscriptionSettingsServiceInstance"} | Start-SPServiceInstance.
  • After executing the powershell commands please check whether the services are running on the machine from the central administration screen
  • Once you came to know that App management and Subscription Settings services are running, you have to configure respective service applications. To configure please follow the below steps. To perform the below steps Please login as an administrator and run all the below PowerShell  code.
$account = Get-SPManagedAccount “ sp\administrator”.
o   AppManagementService
$appPoolAppSvc = New-SPServiceApplicationPool -Name AppServiceAppPool -Account $account.
o   SPSubscriptionSettingsService
$appPoolSubSvc = New-SPServiceApplicationPool -Name SettingsServiceAppPool -Account $account.
o   Creating AppManagementService Service Application
$appAppSvc = New-SPAppManagementServiceApplication -ApplicationPool $appPoolAppSvc -Name AppServiceApp -DatabaseName Appmanagement_Service_DB.
o   Creating Proxy for AppManagementService service application
$proxyAppSvc = New-SPAppManagementServiceApplicationProxy -ServiceApplication $appAppSvc.
o   Creating SubscriptionSettingsService Service Application
$appSubSvc = New-SPSubscriptionSettingsServiceApplication –ApplicationPool $appPoolSubSvc –Name SettingsServiceApp –DatabaseName SubscriptionSettings_Service_DB.
o   Creating Proxy for SubscriptionSettingsService service application
$proxySubSvc = New-SPSubscriptionSettingsServiceApplicationProxy –ServiceApplication $appSubSvc.
You can check the Application services and databases created after running above powershell commands.Please check the below screenshots.
How to configure environment for SharePoint hosted apps on premise?




After doing these setting once again if you go Back to the app and deploy the same you may get the below error:
"Error occurred in deployment step "Installed app for SharePoint": The local SharePoint server is not                  available. Check that the server is running and connected to the SharePoint Farm"
·         To resolve the above error switch back to system account and open the SQL server management studio and assign the db_owner permissions to an account which you are using to deploy the app for below databases.

  •  SharePoint_Config.
  •  SharePoint_Admin_ [GUID].
  • Your current web application that needs to host the app.
  • Appmanagement_Service_DB (app management service application database).
  • SubscriptionSettings_Service_DB (Subscription Settings service application database).
  • Grant DOMAIN\myApp_Admin accont a sysadmin server role on SQL server.
  • Open SharePoint Power Shell and execute Add-SPShellAdmin <DOMAIN\myApp_Admin> command.


        Provide full control permissions to the site collection where we are deploying the app.
Make sure that you have added that user under Farm administrators account from central administration.

How to configure environment for SharePoint hosted apps on premise?


        You have all the required the permissions now. Now you can go ahead and deploy the app. Once you deploy the app successfully and trying to view the app then you might get the popup to enter the credentials. Please enter it once however if you get this below pop up again and again then follow the below steps:
How to configure environment for SharePoint hosted apps on premise?


·         On the Sharepoint server,
o   Click on Start -> Run and type regedit.
o   Locate the key 3. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa
o   Right click on this key and choose New > DWord Value
o   Name this one "DisableLoopbackCheck"
o    Double-click then on it and type the value “1”
o    Reboot your server.
Once done with the above procedures then try to open the app URL and check for the app to successfully execute.




Monday, 9 February 2015

How to prepare Host-Web-Url and App-Web-Url, for SharePoint hosted app, that works for both root site and sub-sites?

$(document).ready(function () {
sharePointReady();
});

function sharePointReady() {
var temphostweburl =
decodeURIComponent(
getQueryStringParameter('SPHostUrl')
);

domainUrl = temphostweburl.substring(0, temphostweburl.indexOf("/sites"));
var tempHostUrl = temphostweburl.substring(temphostweburl.indexOf("/sites/") + 7, temphostweburl.length);

var splitedHostArr = tempHostUrl.split("/");

if (splitedHostArr.length > 1)
{ hostweburl = domainUrl + "/sites/" + splitedHostArr[0]; }
else
{ hostweburl = temphostweburl; }

appweburl =
decodeURIComponent(
getQueryStringParameter('SPAppWebUrl')
);

 
 
scriptbase = hostweburl + '/_layouts/15/';
$.getScript(scriptbase + 'SP.Runtime.js',
function () {
$.getScript(scriptbase + 'SP.js',
function () { $.getScript(scriptbase + 'SP.RequestExecutor.js', execCrossDomainRequest); }
);
}
);
}


function getQueryStringParameter(param) {
var params = document.URL.split("?")[1].split("&");
var strParams = "";
for (var i = 0; i < params.length; i = i + 1) {
var singleParam = params[i].split("=");
if (singleParam[0] == param) {
return singleParam[1];
}
}
}