Wednesday, December 30, 2015

Angular timeout ($timeout) service promise

In angular JS, if you want perform some action after some time (or you want to wait for some time) you can use $timeout service. This service returns the promise which we can resolve by "then". In following example, it will take 2 seconds to call the function "toBeCalled".


function Controller($timeout) {

           wait(2).then(function () {

            })

            function wait(seconds) {
                return $timeout(toBeCalled, seconds * 1000);
            }

            function toBeCalled() {
                console.log("called after 2 seconds");
            }
        }

Saturday, December 19, 2015

Entity Framework Code First Approach

Code First approach does not actually Mean that you can not utilize existing database. Instead, it lets you deal with existing database or you can start with code and Entity Framework will create new database. Let us See both approaches,

Existing Database

Open your project in Visual Studio 2013 and click Add New Item -> ADO.NET Entity Model
Click on the Code First from database option. This wizard will lead you to create model classes.

Wednesday, December 16, 2015

ASP.NET Web API 2 Tips and Tricks

Web API Supports RPC(Remote Procedure Call) API and REST API(Representational State Transfer). To simplify this, RPC means action based routing and REST API means Default GET/POST/.... Methods.
REST API offers only method per request type except the Get which comes two times (One for getting Single Record and one for all Records).

Note: You can not use both techniques in One Controller. you can use One only one technique in one controller.

Configuration


Tuesday, December 15, 2015

How to Know when your angular rendering is complete (Callback function for ngrepeat/ng-repeat)

There are scenarios when you need to know that angular rendering is complete due to ng-repeat directive. Here we need to understand that the success call back method of $http does not mean your rows are rendered. following is the scenario where we can utilize this ng-repeat callback method,
1- You may need to perform particular Jquery Method after rendering of rows is complete.
2- Format generated rows after they are rendered.
3- I needed this to use data tables object ( $('#tbl').DataTable();) after rendering is complete.

To start with, we need to write an attribute level directive which we can call like,

Monday, August 3, 2015

Azure AD Graph API cannot access Microsoft Account


Problem 1:

User.Identity.Name is null for Microsoft users.

Solution

in Global.ascx, write following code,
 protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
        {
            ClaimsIdentity id = ((ClaimsIdentity)User.Identity);
            Claim claim = id.FindFirst(ClaimTypes.Email);
            if (claim != null)
            {
                string email = claim.Value;
                id.AddClaim(new Claim(ClaimTypes.Name, email));
            }
        }


Problem 2: 

https://graph.windows.net/tenantid/users/testemail@hotmail.com ?api-version=2013-04-05 is not returning user profile which the same api works for the local users of AD.

Solution

https://graph.windows.net/contoso.com/users?api-version=2013-11-08&$filter=startswith(userPrincipalName, 'testemail_hotmail')

Wednesday, July 8, 2015

Adding Application Constants by removing Magic Strings in Angular applications

Defining App


var myApp = angular.module('myApp', []);

Adding Constants


These constants are used to remove magic string from the application and user application level constants,

myApp.constant('AppConstants', (function () {
    var constants = {};
 
    constants.App = {};
    constants.App.Name = "My Application Name";
 
    constants.LocalStorage = {};
    constants.LocalStorage.profileKey = "profile";
    constants.LocalStorage.clientsKey = "clients";
 
 
    constants.clsProfile = {};
    constants.clsProfile.Email = "Email";
 
    return constants;
 
}())
 
);

How to add toaster in Angular JS

Index page:

 Open Home main (Container page) page  and add following references.
Remember, If you will add following lines in any of angular view, it will not work.

Reference:

<script src="https://cdnjs.cloudflare.com/ajax/libs/angularjs-toaster/0.4.9/toaster.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/angularjs-toaster/0.4.9/toaster.min.css" rel="stylesheet" />
<script src="https://code.angularjs.org/1.2.0/angular-animate.min.js"></script>


Add Toaster Tag

<toaster-container></toaster-container>

Tuesday, July 7, 2015

Angular JS $state.go redirects to home page

Topic


$urlRouterProvider.otherwise() called before $state.go()

Explaination

i faced above problem in one of my application. I needed to change the state on click event of anchor tag. User was being navigated to Otherwise route rather than redirecting to the route that was called on click event. following was my code,



<a ng-click="SetClient(Name)" href="#">{{Name}}</a>

function SetClient(stateName)
{
$state.go(stateName);
}

The following configuration is used for redirection if any other url is provided.
$urlRouterProvider.otherwise("/home");

The Problem

When ever i clicked on the hyperlink, it redirected to home state rather than the state name that was passed to SetClient.

Solution

I found out that "#" hash sign is creating problem. i removed hash sign and it worked fine.
<a ng-click="SetClient(Name)" href>{{Name}}</a>
Hash sign was calling "$urlRouterProvider.otherwise".

Sunday, May 17, 2015

Could not load file or assembly ‘$SharePoint.Project.AssemblyFullName$’ or one of its dependencies. The system cannot find the file specified.

There are lot of scenarios where you can face this issue. I will explain scenario which i faced.

Scenario

I have all the solution deployed and running already on the production server. My task was to just change an html link in the .ascx control. I changed the control and rather than deploying whole solution, i copied .ascx control and put it in the 14 hive control templates. I restarted IIS and refreshed page, i found following exception,

Could not load file or assembly ‘$SharePoint.Project.AssemblyFullName$’ or one of its dependencies. The system cannot find the file specified.


Solution

Luckily error was pointing towards the new custom control that i deployed. I opened file and searched for "$SharePoint.Project.AssemblyFullName$". Replace this string with the actual assembly name, version and culture (If you deploy with visual studio, it changes it automatically but as it was manual deployment, i needed to copy control's assembly and replace with the "$SharePoint.Project.AssemblyFullName$"). Restart IIS and problem was solved.