Tuesday, 23 June 2015

Access the fields on Quick View Form – Dynamics CRM 2015


Quick view forms in Dynamics CRM 2013 allow you to display all the related entity information.
Is there a way to retrieve the field value on Quick View Form using JavaScript without additional OData call to the associated entity?
The answer is Yes and here is the sample of the form and JavaScript:
  Here is the Following steps to get the value from the QuickView form
    Sample Quick Form
    Eg: We need to get the attribute value called AccountNumber from QuickForm


Step 1:

Open the Customization->Entity->Form

   
Step 2:
 Double click the customer pane window to see the properties and note the quickformname,and related entity name.Click Edit.
     

Step 3:
  Our goal is to get the attribute name of AccountNumber.
  After Click Edit the following dialog box with Field Properties and note down the attribute name



Code:


below is the format
//QuickFormname_QuickFormname_EntityName_AttributeName
if(Xrm.Page.getControl('customerpane_qfc_customerpane_qfc_account_accountnumber') != null) {
            var caseCustomerPanectrl = Xrm.Page.getControl('customerpane_qfc_customerpane_qfc_account_accountnumber');
            strAccountNo = caseCustomerPanectrl.getAttribute('accountnumber').getValue();
        }


Well its is easily done without any stuff!!!!

   
  





Saturday, 20 June 2015

Auto Mapper in MVC

Auto Mapper in MVC:

When we code for a realistic actual environment, we encounter many challenges to refractor our code for a better understanding and to easily identify the bugs. We mainly focus on re usability and try to move as much code as possible to a common method so that development time and maintenance costs are reduced.
         In this article, I will try to cover a new concept in MVC:  AutoMapper is used to reduce the complexity we find when binding the model and communicating with entities.

The Real Problem: 
We often interact with our database entities and bind our models with it. What we end up with is somewhat like this:
//Return UserDTO Class
public UserDto GetUserById(string id)
{
//Create new Object for DTO class
var userDto = new UserDto();
//Create New Object For Entity(DB class)
using(var context = new EntityDBContext())
{
//Get the single row from DB using LINQ query
//It always use using statement for best practice to dispose object automatically
var user = context.Users.Where(ID => ID.user_id == id).FirstOrDefaul();
if (user != null)
{
//Fill DB object to UserDTO Class
userDto.UserId = user.user_id;
userDto.Name = user.user_name;
userDto.Address = user.user_address;
userDto.PhoneNo = user.user_phoneNo;
userDto.City = user.user_city;
}
}
return userDto;
}   

We see here that there are five properties in each class. And what we are doing here is binding the db context class to our model and then passing it to the view. Now, this looks quite simple in this case. The real problem arises when we have 25-30 fields in a record from the database and we need to repeat this same binding code all over in the project. It's awkward and painful. Copying and pasting even if it's only five properties.
                     More complexity arises when we have a separate ViewModel for each view (which is typically used in current MVC across the companies). Now you have double the amount of work, first you need to bind your model and then again you need to bind it to the ViewModel. Think of how tedious that job would be.
                To overcome this tedious situation AutoMapper is introduced. It not only reduces the effort but it also limits the execution time that has been taken by such a large number of lines to execute.

What is AutoMapper:

  AutoMapper is an object-object mapper. Object-object mapping works by transforming an input object of one type into an output object of a different type. What makes AutoMapper interesting is that it provides some interesting conventions to take the dirty work out of figuring out how to map type A to type B. As long as type B follows AutoMapper's established conventions, almost zero configuration is needed to map two types." Therefore, it provides the solution for our mapping issue.
   

   Firstly install the NuGet Package Manager in your Visual Studio IDE. Once done, go to:
     Tools -> Library Packet Manager -> Packet manager Console
Then in the console window opened at the bottom of Visual Studio, type:
                  PM> Install-Package AutoMapper
Press Enter, this will install AutoMapper and the next time you open an MVC application in Visual Studio, it will automatically add a DLL reference to the project.

Use of Automapper:

Now we will see where AutoMapper fits in our problem. Well, AutoMapper provides a CreateMap<T1,T2> method that provides mapping between two classes. So, the entire code can be replaced by the following single line:
There are two steps to use this Automapper

Step 1 :

//UserDTO-DTO Object Class//User-Model Class to bind the View//Fill UserDTO to User model

                           Mapper.CreateMap<User, UserDto>();
Step 2 :
                          Mapper.Map<User,UserDto >(user);
Where User is a DBContext class type and UserDto is our DTO class to map.

Finally:to map all the data we need to use the Map<T1,T2>() method of the AutoMapper class. So the final code will look like this:


public UserDto GetUserById(string id)
{
var userDto = new UserDto();
using(var context = new EntityDBContext())
{
var user = context.Users.Where(ID => ID.user_id == id).FirstOrDefault();
//Create Map for DTO and Model Object
Mapper.CreateMap<User,UserDto >();
//Fill the DTO object to Model Object and reduce the lot of code.
userDto = Mapper.Map<User,UserDto >(user);
}
return userDto;
}
So, we avoided the boring work of mapping properties in the code. Our CRUD operation is also simplified and organized. Just map the classes and then do Post actions. To get all the details, one change we need to make to the preceding code is that we need to add all the userDto objects to a List of userDtos.
   

ForMember() and MapFrom() in AutoMapper

Two important functions in AutoMapper play an important role in object mapping. Suppose our model/viewmodel class has a property FullName, and from the DTO we want to add the First Name and Last Name of the user to make it a full name and bind it to the model. For these kinds of scenarios ForMember() andMapFrom() come in handy. See below code:
   Mapper.CreateMap<LearningMVC.User, LearningMVC.Models.User>().ForMember(emp => emp.Fullname,
map => map.MapFrom(p => p.FirstName + " " + p.LastName));

Here we are saying that ForMember FullName in our model class maps properties from FirstName and LastName of User DTO.

The code itself is self-explanatory. This kind of mapping is also called Custom Mapping.




Friday, 19 June 2015

Parse JSON file and insert data into SQL server

Parse JSON file and insert data into SQL server
When there any requirement, insert json data into sql server
 the following blog will explain step by step to insert the data using sql server


Sample JSON :
     First I'd restructure the JSON so it made more sense. You said a "person" can have multiple "people", so structure it that way. A "person" has three attributes (i.e. i_date, i_location, and i_summary) and a collection of people.

{
"person":{
    "i_date":"2014-03-20",
    "i_location":"test",
    "i_summary":"test test",
    "people":[
      {
        "first_name":"first name test1",
        "last_name":"last name test1"
      },
      {
        "first_name":"first name test2",
        "last_name":"last name test2"
      },
      {
        "first_name": "first name test3",
        "last_name":"last name test3"
      }
    ]
  }
}
Now you can declare some .NET classes that represent the structure


  public class Person2
  {
    public string first_name { get; set; }
    public string last_name { get; set; }
}

public class Person
{
    public string i_date { get; set; }
    public string i_location { get; set; }
    public string i_summary { get; set; }
    public List<Person2> people { get; set; }
}

public class RootObject
{
    public Person person { get; set; }
}

The above class can be easily generated by using the following url
                              http://json2csharp.com/
When you give the sample JSON it will convert .net class automatically.
   var root = JsonConvert.DeserializeObject<RootObject>( json );
 
 You need to use Newtonsoft Json download from Nuget Package from Visual studio and make use of above feature.
  With Above Object "root" u can perform the Database logic.

Wednesday, 17 June 2015

Reusable web Part in Sharepoint 2013 using Visual studio

Creation of Webparts and Resuse the User control in Sharepoint 2013
  There is small difference in Visual WebPart Sharepoint 2013 and Sharepoint 2010

 Overview:
SharePoint 2010, there would be a WebPart class and a UserControl Class in a Visual WebPart. The associated UserControl (.ascx) file will be loaded during execution in WebPart class through CreateChildControl Method. Since two different classes are involved, the properties created in WebPart class has to be passed to UserControl Class to make use of those values during execution. But in SharePoint 2013 Visual WebPart, no separate UserControl class is created for the .ascx file and only WebPart class is available. The controls that are placed in ascx file are automatically converted in to c# code and placed in a file with a name [Name of Web Part].ascx.g.cs. This is a partial class of the original WebPart class that is inherited from “System.Web.UI.WebControls.WebParts.WebPart” class and created in the name of Visual WebPart

From the above we can use Visual Webpart as a Resuable componets any where in the page,but the use of user control in webpart is important as we can resuse the Controls any where in the webpart as well as in pages in sharepoint with webpart.

Step 1:
Create Visual Webpart from sharepoint 2013 project
once u created the folder structure as follow as














Step 2:
Create User Control by adding new item choose(User Control-Farm Only)
Note: The user control will work only for farm only not suitable for sandbox deploy
Once User control created in the project the user control creates under Folder "Control Templates" shown as below.














Step 3:
Once after create the user control, we can use sharepoint List to pull all the data and bind into simple Gridview(control) to res use in the webpart.

The below simple code is to pull the data from sharepoint list and bindin the simple gridview control of asp.net.















Step 4:

Deploy user control to use in the Webpart
Right Click the Project->Deploy.

All user control is deployed in the Path "C:\Program Files\Common Files\Microsoft Shared\Web ServerExtensions\15\TEMPLATE\CONTROLTEMPLATES\WebPartProject\SiteUserControl.ascx"
Step 5 :

Register User control in Webpart
Below code to register the User control in web part
1.Mention the path of user control reside(It will Reside on @"~/_CONTROLTEMPLATES/15/WebPartProject/SiteUserControl.ascx)
Note:Sharepoint 2010 user control reside on 14 hive folder
Sharepoint 2013 user control reside on 15 hive folder

2.Override the CreateChildControls method to load the user control on the webpart page
3.Override the Render Content to render the Html in the Visual Web part


















Step 6:

Use Webpart in the Page in Sharepoint 2013

Once after registering the user control in the Webpart build the Project and deploy the same.

In below screen explain sthe how to add the webpart in the page

1. Go to Page tab navigation and click edit
2. Click on Insert tab
3. Click Web Part
4. In Categories Choose Custom folder(all customize webpart deployed in this folder)
5. Choose the Visual webpart on the right under Parts Caption
6. Click add to View Your webpart with Resusable user control in the Page
7. Similar way u can create the Page and resuse this webpart any where













Resusable control with Visual Webparts

SharePoint Event Receiver Before Properties,After Properties

Before Properties,After Properties and ListItem in Sharepoint 2010 Event Recievers
  


 Event recievers are common in Sharepoint development so its better to understand the data available in each events. Sometimes as a developer we jump into coding before thinking about contextual data availability.One more imprtant thing to notice list event reciever  and document libraray event reciever are different interms of contextual data avaialability.Following Table will give you a clear picture about the contextual data in each events.

  



Resolving Dynamics 365 Contact Sync Issues in Outlook – ExchangeSyncIdMapping Overload Introduction Recently, our project encountered a ...