Microsoft Articles

How can I speed up Visual Studio 2015 IDE?

After jumping from Visual Studio 2013 to Visual Studio 2015 you might have experienced a big difference in time it takes for the 2015 IDE to open up. If you use DiskMon it will tell you it’s trying to load a bunch of folders / files in various locations. After deleting files from following folders there’s a night and day difference in load times:

C:\Users\<user_profile>\AppData\Local\Microsoft\WebSiteCache

C:\Users\s<user_profile>\AppData\Local\Temp\Temporary ASP.NET Files\vs

C:\Users\<user_profile>\AppData\Local\Temp\Temporary ASP.NET Files\root

 

Also, perform these steps to further speeding up the IDE perform following steps:

1. Tools > Options

2. In Environment > General configure checkboxes as shown in following snapshot

image

No more long coffee breaks while waiting for the IDE to load.

Happy and Fast coding

No Comments

Demystifying ViewBag in ASP.NET MVC

In ASP.NET MVC you must have encountered ViewData and ViewBag. ViewData is the older implementation where as ViewBag is newer strict tpe checking based implementation. We can add items to ViewBag dynamically like this:

ViewBag.ProductId = 1;

ViewBag.ProductTitle = "Windows 10";

Even though we haven’t declared these properties before but yet we are able to declare them and assign values dynamically. How does it let us do that? To demystify it we have to understand ‘var’, ‘dynamic’ and ‘ExpandoObject’. So, let’s get started.

var

C# allows us to implicitly declare variables by using ‘var’ keyword. E.g.

int a=0; //explicitly declared variable

var b =0; //implicitly declared variable

We can also use ‘var’ keyword to create anonmous types / objects. E.g.

var bob = new { EmployeeId = 1, EmployeeName = "Bob", Department = "Sales" };

Even though we don’t have an Employee class we are still able to create Employee based objects by using Anonymous Type concept. If you write the above line in Visual Studio and hover mouse over ‘var’ keyword it will tell you that it’s an Anonymous Type. We can pass anonymous types to other. Anonymous objects can be passed to functions as parameters by using dynamic data type. E.g.

dynamic

void DisplayEmployeeDetails(dynamic employee)

{

string message = string.Format("EmployeeId:{0}, EmployeeName:{1}, Department:{2}", employee.EmployeeId, employee.EmployeeName, employee.Department);

}

DisplayEmployeeDetails(bob);

This is a very powerful concept since it lets us create dynamic objects on the fly and we can accept these in any function dynamically.

ExpandoObject

We can take this concept to next level by using another .NET Framework object called ExpandoObject which lets us create properties dynamically. E.g.

dynamic mixedBagObject = new ExpandoObject();

mixedBagObject.PlayerId = 1;

mixedBagObject.PlayerName = "Crosby";

mixedBagObject.CustomerId = 1;

mixedBagObject.CustomerDOB = new DateTime(1,1,1970);

mixedBagObject.ProductId = 1;

mixedBagObject.ProductTitle = "Windows 10";

Even though we have declared any class we are still able to create properties and assign them values on the fly.

So, ViewBag is actually a dynamic ExpandoObject.

No Comments

Must have plugins / utilities for Visual Studio 2015

 

Visual Studio is a great IDE that comes with plenty of productivity  features OOB e.g. code snippets, page inspector etc. In this post I am going to list some must have plugins / utilities that every developer should use to further enhance productivity.

1. Web Essentials for Visual Studio

If you don’t have it already then go and get it right now then read rest of the post. It’s free. http://vswebessentials.com/

It comes with various shortcuts for writing code (C#, JavaScript, CSS …).

 

2. Bundler & Minifier

If you do web development then you must be maintaining several .js / .css files. Bundler & Minifier reduces the footprint of such files which improves performance.

https://visualstudiogallery.msdn.microsoft.com/9ec27da7-e24b-4d56-8064-fd7e88ac1c40

 

3. Web Compiler

If you are using LESS for for writing css files and CoffeeScript for writing JavaScript then you should use web compiler.

https://visualstudiogallery.msdn.microsoft.com/3b329021-cd7a-4a01-86fc-714c2d05bb6c

 

4. Reflector / dotPeek / ILSpy

Useful for reverse engineering .exe and .dll files to look at the source code. It can convert MSIL to C# / VB.NET and other languages. Be careful, if you can look at someone else’s code then they can do the same to yours too. That’s where you should use the next utility…

5. Dotfuscator / Crypto Obfuscator

It obfuscates the code. Think of it as a paper shredder machine. No it doesn’t erase the code but it does make it so confusing that it gives person a bad headache when trying to understand the code.

6. Resharper / CodeRush

I am personally a bigger fan of Resharper. Other folks prefer CodeRush. It’s not free but it’s a must have plugin. It’s useful for refactoring code.

 

7. IDE – Visual Studio 2015 / Visual Studio Code / SharpDevelop

Well IDE is not a tool but you need a tool that comes with IDE. Visual Studio is definitely the best choice. There’s no excuse to not have Visual Studio now especially since there’s a free Community Edition which is more full featured than Express edition. If you want to use it on Mac or Linux then you can go for Visual Studio Code

8. LINQPad

If you like LINQ then LINQPad can be very handy.

 

9. XMLPad

This is to XML what LINQPad is to LINQ

 

10. Linqer

A very nice utility for converting T-SQL to LINQ.

There are several more plugins / utilities but that’s for some other day.

No Comments

What’s new in Visual Studio 2015

Visual Studio 2015 introduces several exciting features. Here’s a list of some of them:

  1. Cross Platform Development.

This is perhaps the biggest and most exciting feature. Although part of it was introduced in Visual Studio 2013 as well but 2015 takes it to next level. Now applications for Windows, Linux, iOS can be created in Visual Studio 2015. It bundles both Apache Cordova and Xamarin Starter Pack. And thanks to windows 10 the apps can run on Windows desktop, tablet, mobile and XBOX One. Pretty soon apps will run on Microsoft HoloLens and Surface Hub as well.

2. ASP.NET 5

ASP.NET uses CoreCLR which allows the web apps to run on Windows, Linux and Mac. MVC wise it comes with version 6. Visual Studio 2015 supports dynamic web development which means no recompilation of the code is required to see the rest of changes made to the source code. Simply save the code and refresh the browser to see the changes.

3. Open Source

.NET Framework went open source quite a while ago even before Visual Studio 2015 hit RTM. Microsoft went with GitHub as the platform for hosting open source .NET Framework Base Class Libraries as well as the compiler (Roslyn).

,

No Comments

Future of Windows OS

In the past Microsoft focused on releasing a new major version every few years. It has been a risky model since some versions were a major success, like Windows 95/XP/7, where as others have been somewhat of a miss, like Windows Vista/8/Windows Mobile/Windows RT. Also, some organizations used to skip a version in between due to cost and lack of program compatibility.

Based on news it seems like Microsoft has decided to ditch the traditional development cycle and going to stick with Windows 10 for a very long time come. Windows 10 is a platform that will run on a desktop,laptop,tablet,mobile and XBOX. Microsoft plans on adopting a more agile approach which means Windows 10 will see more frequent and mostly smaller changes(iterations). There will still be major changes(releases) but not as huge as coming out with a brand new OS. That’s a more akin to Linux / MAC OS / iOS which should encourage an average consumer and enterprise organization to remain more up to date than things have been in the past. Windows 10 is around the corner and getting shipped sometime this summer. It’s next update us code named Redstone which will come out in two waves in 2016.

,

No Comments

What’s new in C# 6.0

C# has seen several changes since it got launched when .NET Framework came out. e.g. C# 2 had closures (anonymous methods), generics, C# 3 introduced LINQ, C# 5 saw task-based asynchronous pattern and now we have C# 6.0. Although you can enable C# 6.0 in Visual Studio 2013 but it’s not recommended since not all the new features will be available in the older IDE. If you still want to persist with 2013 then the plugin can be downloaded from https://github.com/dotnet/roslyn. The best way to use C# 6.0 is to get Visual Studio 2015. Currently, RC is available for download but the final version isn’t far away.

Here are some of the newer features in C# 6.0

1. Number Literal Formats

How many times did we have to write some number and then count the digits to see if it’s thousand, hundred thousand or million. E.g. in the past we declared numbers like this:

Thousand Comma Underscore

var thousand = 1000;

var tenThousand = 10000;

var hundredThousand – 100000;

var million = 1000000;

I despise counting digits to ensure it’s the right number. C# 6.0 to the rescue now we can declare them like this:

var thousand = 1_000;

var tenThousand = 10_000;

var hundredThousand – 100_000;

var million = 1_000_000;

As you can see an underscore (_) is doing the trick here.

Binary Literals

Binary numbers are easier to write. So, we can use something like this:

int binary_1 = 0b00_00_00_00_00_00_01; // 0x0001
int binary_2 = 0b00_00_00_00_00_00_10; // 0x0001
That will make things a lot easier when it comes to using bit shifting operators. e.g.
i
nt one = binary_2 >> 1; 

2. Declaration Expression

This essentially allows you to declare variables in certain scenarios. E.g. let’s examine int.TryParse
var convertedValue;
var isConverted = int.TrParse(“123”, out converted);
Here, we had to declare “converted” variable upfront before using it in int.TryParse. 
In C# 6.0 now it’s possible to do the same thing in a single line like this:
var isConverted = int.TryParse(“123”, out var converted);
As you can see we were able to use var, or we can use explicit type, along with out keyword.

3. Static Using Statements

We have several static class / functions in C#. e.g. Console, File etc. 
Let’s see how code was written before C# 6.
   1:  using System;
   2:   
   3:  public class Program
   4:  {
   5:      public static void Main()
   6:      {
   7:          Console.WriteLine("Please enter your name");
   8:          var name = Console.ReadLine();
   9:          Console.WriteLine("Welcome, " + name);
  10:      }
  11:  }

In C# 6.0 it’s possible to use static using statements so we don’t have to use static class name over and over again. So, the new code will look like this:

   1:  using System;
   2:  using System.Console; // this line is doing the magic
   3:   
   4:  public class Program
   5:  {  
   6:         public static void Main()
   7:        {
   8:                WriteLine("Please enter your name"); // no need to write Console
   9:                var name = ReadLine(); // no need to write Console
  10:                WiteLine("Welcome, " + name); // no need to write Console
  11:         }
  12:  }

4. Auto Properties with Initializers

auto properties were available in the past. So, we were able to do some thing like this:

   1:  class Country
   2:  {
   3:    public int CountryId {get; set;}
   4:    public int CountryName {get; set;}
   5:  }
   6:   

Auto properties used to have problem when it used to be an array or collection. So, for a collection we had to create a private backing field or we had to use a constructor. With backing field it used to look like this:

   1:  class Country
   2:  {
   3:    public int CountryId {get; set;}
   4:    public int CountryName {get; set;}
   5:    public List<string> Provinces {get; set;}
   6:   
   7:    public Country()
   8:    {
   9:       Provines = new List<string>(); // initializing collection in constructor
  10:    }
  11:  }
  12:   

Now, in C# 6.0 it’s possible to use auto property syntax and initialize it as well in a single line. New code looks like this:

   1:  class Country
   2:  {
   3:    public int CountryId {get; set;}
   4:    public int CountryName {get; set;}
   5:    public List<string> Provinces {get; set;} = new List<string> ;
   6:  }
   7:   

There are several more new features in C# 6.0, like indexed members, element initializer and primary constructors.

,

No Comments

SQL Server 2016

SQL Server is evolving rapidly. Just last year we received SQL Server 2014 and 3 years ago we received SQL Server 2012 and now we are already expecting SQL Server 2016. SQL Server 2012’s main features were Always-On, and Contained databases. SQL Server 2014’s main features were In-Memory OLTP and integration with Windows Azure. Here are some of the main features of SQL Server 2016:

1. Always Encrypted

It’s a security related feature, as obvious from the name. In the past we did have TDE for encrypting the entire database which required creation of encryption keys. In Always Encrypted seems like the encryption key will reside on the application side. More will get know when the preview software gets released.

2. Support for JSON

JSON has become a power house when it comes to data format used for data transfer. XML is already supported by SQL Server as a native data type with XQuery / XPath syntax. Now JSON is getting added to SQL Server 2016 which means a lot smaller footprint, compared to XML.

3. Advanced Analytics, PolyBase, Mobile BI

Business Intelligence is getting more love from Microsoft in SQL Server 2016. PolyBase will allow folks with T-SQL skills to extract value from structured and unstructured data. Data visualization tools will be available on Windows, iOS and Android mobile devices. Exactly how the applications will look like? We will find out soon.

4. Stretch Database

Storage is getting cheaper but still there are cases where we struggle and run out of storage. Stretch database feature allows us to archive data that’s not part of day to day transactions. So we can call the important data “warm transactional data” and rest of it that we want to archive can be called “cold transactional data”. Cold transactional data can be archived to Windows Azure cloud storage. It seems like reports can still be created based of warm as well as cold transactional data.

There are several more features but above features. Waiting anxiously for the preview release which is expected sometime this summer.

, , , , , ,

No Comments

SharePoint Development– Object Model

For SharePoint development we rely on a bunch of classes. These classes are collectively known as Object Model. By using these classes we can perform various activities e.g. create site collections / sub-sites, libraries, lists, upload documents, add list items, delete items, create taxonomy / metadata / content types etc.

 

Object model is of two main types:

  1. Server Object Model
  2. Client Object Model

Server Object Model

Server Object Model means we get to create program in C# / VB.NET and the code is deployed to SharePoint server as a wsp package.  In short the code runs on the same server where SharePoint is deployed. We can also use PowerShell for utilizing server object model.

Here’s the server object model hierarchy:

* SPFarm (enumerate services, solutions, CurrentUserIsAdministrator)
    * SPService (represents a service. e.g. Excel Service, InfoPath Form service etc.)
        * SPWebApplication (represents the IIS web application)
            * SPSite (represents site collection)
                * SPWeb (represents site / sub-site)
                   * SPList (represents list / library)
                        * SPListItem (represents list item or document)

Here are some more useful classes:

SPField[type] (represents field / column in a list or library. e.g. Link, Choice, …)
SPContentType (represents content type)
SPUser (represents SharePoint user)
SPSecurity (security,  elevated permissions, exception handling)
SPQuery (used for running CAML queries for querying lists and libraries)
SPContext (easier way to retrieve current site)
SPGroup (represents SharePoint security group)
UserProfileManager (useful for manipulating user profiles and mysite)
Taxonomy (Term Store. Hierarchical metadata)
    * TaxononmySession
    * TermStore
    * Group
    * TermSet
    * Term

Client Object Model

Client Object Model allows us to write code in  C# / VB.NET / JQuery etc. The code can reside on any machine and we can still access SharePoint located on a remote server.  Here are some useful client object model classes:

ClientContext (specify the SharePoint site we want to connect to. Pass credentials to the site)
Web (represents an existing site / sub-site)
WebCreationInformation (used for creating a new site /sub-site)
NavigationNodeCreationInformation (used for configuration navigation / top link bar)
List (represents an existing list or library)
ListCreationInformation (used for creating a new list or library)
ListItemCreationInformation (used for creating a new list item)
ListItem (represents an existing list item)
FileCreationInformation (used for creating a new document in a library)

, ,

1 Comment

SharePoint–Some useful OOB Application Pages

SharePoint comes with a boatload of application pages that reside in 14/15 (depending on SharePoint version) hive folder structure.

 

e.g. In case of SharePoint 2010 you can find them in

%systemdrive%\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS
 

In this hive folder you will find a bunch of .aspx pages. These pages are used by “Site Settings” etc. You can also create your custom application pages as well by using Visual Studio. Business case for creating custom application page would be you want to create a form that allows end-users / site admins to submit request for creating departmental sub-site or bunch of libraries / lists with custom content types.

Here are some useful OOB application pages along with sample scenario where you can use them.

Signing out the user

Say you want to create a custom link that allows the user to sign out. You can do so by using this url:

http://<site>/_layouts/signOut.aspx

Signing in as a different user

http://<site>/_layouts/closeConnection.aspx?loginasdifferentuser=true&Source=http://<site>/SitePages/Home.aspx

Display access denied page

Say you have some code in Visual Studio and the operation you are trying to perform isn’t valid for the current user due to lack of permissions. We can redirect the user to access denied page.

http://<site>/_layouts/accessdenied.aspx

Removing faulty web parts from a web page

When you add web parts to a page sometimes due to unhandled exceptions or configuration issues a web part can cause the whole page to become inaccessible. To remove the faulty web part we can use following application page:

http://<site>/_layouts/spcontnt.aspx?url=/SitePages/Home.aspx

Here /SitePages/Home.aspx the relative url of the page that contains the faulty web part.

No Comments

SharePoint–Displaying Site Collection Size Using PowerShell

Let’s say we have to display SharePoint site collection size using PowerShell. We can easily use Client Object Model and PowerShell to accomplish that. Let’s do it step by step:

 

PS > $spsite = Get-SPSite “http://sharepoint:100”

PS > $spsite.Usage

It will display data like this:

size

Hmmm, interesting. It shows the size and also some other user information like visits and bandwidth. But, we just care for the size. So let’s modify the code:

PS > $spsite.Usage.Size

Now the output looks like this:

3514033

Ok, we have the size but it’s in bytes. Let’s convert that to MB.

PS > $spsite.Usage.Size / 1000000
or alternatively we can use this code:
PS > $spsite | select @{Name=”Size”; Expression={$_.Usage.Size / 1000000 }}

The output looks like this:

3.514033

Still doesn’t look good. Now we can use the expression formatting to make it human friendly:

PS > $spsite | select @{Name="Size"; Expression={"{0:N2} MB" -f ($_.Usage.Storage/1000000)}}

Bingo, now it shows up as:

3.51 MB

0:N2 is the standard .NET based format. 0 would means the argument position and N2 means number format with 2 decimal places. –f is used for pass the argument to the format.

No Comments