Wednesday, June 1, 2016

Set up a Development Environment for Creating SharePoint Hosted App

“Apps for SharePoint are easy-to-use, lightweight web applications that integrate popular web standards and technologies to extend the capabilities of a SharePoint website.” [MSDN]
 
Configuring an on-premises development environment for Apps

Prerequisites
  • A windows server machine ,  installed and configured for SharePoint
  • Microsoft SQL server
  • Microsoft visual studio
1. Confirm that the SharePoint Administration (spadmin) and SharePoint Timer (sptimer) services are running.
  • Start -> Administrative Tools-> Services
  • Or using command prompt 
    •  net start spadminv4
    •  net start spadminv4
2. Start “App Management Service” and “Microsoft SharePoint Foundation Subscription Settings service”
  • Go to SharePoint Central Administration
  • Application Management -> Service Applications -> Manage Services on Server
  • Start App Management Service
  • Start Microsoft SharePoint Foundation Subscription Settings service

3. Make sure your user (not SP-Farm) has the following permissions
  • securityadmin fixed server role on the SQL Server instance
  • db_owner fixed database role on all databases that are to be updated
  • Administrators group on the server on which you are running the Windows PowerShell cmdlets
If not assign those permissions to that user

4. Create service applications - Need to create following service applications
  • App Management Service Application
  • Subscription Settings Service Application

Open the SharePoint Management Shell and run the following commands.
(Account should be the above account )

$account = Get-SPManagedAccount "domain\user"
$appPoolSubSvc = New-SPServiceApplicationPool -Name SettingsServiceAppPool -Account $account
$appPoolAppSvc = New-SPServiceApplicationPool -Name AppServiceAppPool -Account $account
$appSubSvc = New-SPSubscriptionSettingsServiceApplication -ApplicationPool $appPoolSubSvc -Name SettingsServiceApp -DatabaseName SettingsServiceDB
$proxySubSvc = New-SPSubscriptionSettingsServiceApplicationProxy -ServiceApplication $appSubSvc
$appAppSvc = New-SPAppManagementServiceApplication -ApplicationPool $appPoolAppSvc -Name AppServiceApp -DatabaseName AppServiceDB
$proxyAppSvc = New-SPAppManagementServiceApplicationProxy -ServiceApplication $appAppSvc


Then navigate to Central administration -> Application Management -> Service Applications - > Manage Service Application. You will be able to see the newly created above service applications.

5. Configuring App URLs
  • Central Administration -> Apps -> Configure App URLs
    •  Create the app domain name- Ex. App.mydomain.com
    •  Create the app domain prefix – Ex. App
If the domain name is not a proper one make sure to add a DNS entry with that domain name in the Host file in the VM.

6. Create a web application
  • Central Administration -> Application Management -> Web Application -> Manage Web Application -> Create a new web application
7. Create a new site collection under the web application created above
  • Central Administration -> Application Management -> Site Collections -> Create Site Collections
Create a site collection with a “Developer Site” template 


8. Make sure that the user (created on step 3) has the Db-Owner permission to below databases if not, assign the Db-Owner permission to the user
  • SharePoint_Config
  • SharePoint_Admin_ [GUID]
  • Current web application content database that needs to host the app
  • App management service application database
  • Subscription Settings service application database
9. Log in to the virtual machine again with the user account configured in the step 3 (Non-farm account)

10. Create App
  • Open Visual Studio and create a new project with the template “App for SharePoint 2013” 
    • Put a name for the Sharepoint app
    •  Put above created site collection URL to sharepoint site for debugging 
    •  Choose "SharePoint-Hosted" for the host type

11. Deploy the application

12. Add below Registry entries to the windows registry
  • Go to “HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa” 
    •  Add a dword , name = DisableLoopbackCheck , value=1
  • Go to “HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters”
    •  Add a dword , name= DisableStrictNameChecking , value=1 , type=decimal

13. Go to your developer site -> Apps In Testing -> Click on the app you have created

14. You will be redirected to the fresh app you just have created

More details read...

Set up an on-premises development environment for SharePoint Add-ins

http://social.technet.microsoft.com/wiki/contents/articles/25793.step-by-step-guide-to-configure-environment-for-creating-your-first-sharepoint-hosted-app.aspx

https://support.microsoft.com/en-us/kb/896861
 



Monday, May 23, 2016

Template method design pattern


This design pattern comes under behavioral design pattern

Definition
Define the skeleton of an algorithm in an operation, deferring some steps to client subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
Basically base class defines the Placeholders, skeleton of the operation and derived classes implement those Placeholders as necessary.

UML Diagram 







Main components of the design pattern
AbstractClass  - The base class
All the abstract primitive operations are defined here

A template method which defines skeleton/structure of the algorithm. This method calls the primitive operations and other necessary methods which resides in the abstract class.

ConcreteClass – Sub class
Implements the abstract operations which defined in the base abstract class as needed

Usage

Mostly using when creating frameworks. A framework normally has multiple steps (Placeholders) and finally they all call by a base method. Those steps can be overridden in different ways in different scenarios.  But that base method is common to all scenarios and it should be a final method which is no one can override it.
All reusable code is declared in the framework's base classes, and then clients of the framework are free to do customizations by creating derived classes as needed.

Example

Think, that we are going to implement a common database access class which can be used to retrieve data from multiple database servers like “SQL Server”, “MySql”, “Oracle” and Etc.





Abstract class = DataAccess
Template Method = RunQuery() -
Abstact Methods = OpenConnection(), OpenTransaction(), ProcessQuery(),EndTransaction(),CloseConnection()
Concreate Classes = SQLDataAccess , MySQLDataAccess  - All the above abstract methods must implement on these concreate classes


DataAccess Class - Base Class



SQLDataAccess Class


MySQLDataAccess Class
    


When to implement template method

A template pattern should be used when there is an algorithm with many implementations but when we can find significant similarity among them. Simply where a number of components/scenarios shared the same process but the implementation was slightly different from each other.
Sometimes Template method referred as “The Hollywood principle”, when we think from the superclass point of view simply it means “Don’t call us, we will call you”.  That means instead of calling the methods from base class in the subclasses, the methods from the subclass are called by the template method in base class. Hence the template method implemented by the base class should not be overridden. So the Template method in the base class should be a “Final“and non-abstract method.