Category Archives: VS 2012

JavaScript Visual Studio snippet for is object defined check

Since I write a lot of JavaScript currently in Visual Studio these days, I lack one of the very common checks in JavaScript and that is testing if object exists.

I mean this check :

if (typeof (someObject) !== 'undefined') {

someJSCodeHere();

}

If you want to use this code but not type it each time over and over again and speed your work up you probably thought a snippet that would save your keystrokes would come handy, but there is none in vanilla VS. So, you can use my snippet ,just import it to Visual Studio from Code Snippet Manager window (in VS 2013 – CTRL + K + B) and use it with typing def and then tab (or use CTRL + K + X and pick one).

Enjoy your speedup while working with JavaScript.

PS : do you know you can share snippets with your team mates?

http://blogs.msdn.com/b/zainnab/archive/2010/08/19/sharing-snippets-with-your-team-vstiptool0075.aspx

http://blogs.msdn.com/b/saraford/archive/2007/12/14/did-you-know-how-to-share-code-snippets-with-your-team.aspx

PS2: If you want to edit snippets in you VS, this plugin might be a good idea : http://snippetdesigner.codeplex.com/

How to build Visual Studio solution even if some of TypeScript files are not compiling

The problem :

If you are working on some ASP.NET project and you need to build it even if some files are unbuildable/will not compile (currently I apply this on TypeScript files but this solution is general) , because you for example need to change some other functionality and than continue with TypeScript work than you have a problem.  VS stops building the app and you will have only Yellow Screen Of The Death note with “Parser Error” and “Could not load type – your project name” which I suppose is caused by not generating the assembly.

So, the solutions are (among others) :

  • Exclude erroneous .ts file (which will make it not compile),
  • Say to build system to not compile particular file with something different.

The solution :

It really is simple : just set Build Action from TypeScriptCompile to None and you are done.

ts-build-actions

The strange thing is, that my Visual Studio 2013 will anyway say I have errors, but the build process is not stopped and I am able to run the app.

If you have any other solutions than feel free to post them in the comments. Thanks in advance.

PowerShell Tools for Visual Studio 2012 problem – .ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies

Solution to your problem :

For some reason,  account on which you run VS on and on which VS hosts PowerShell has restricted ExecutionPolicy (you can type Get-ExecutionPolicy , select the text and hit CTRL+F8 and should see Restricted as the answer).

So the solution is simple (should be): override the settings and set it to RemoteSigned with this command :

Set-ExecutionPolicy RemoteSigned

select the text, hit CTRL+F8. Now hitting F5 should make your PS behave.

(Alternatively Unrestricted is also viable option, but this is considered unsafe. More on this here : http://technet.microsoft.com/en-us/library/ee176961.aspx)

More on the topic :

I wanted to create some PowerShell script and I thought there has to be something better than PowerShell ISE (sry, I am just too used to VS and debugger and all that goodness I have at my disposal) so I discovered this wonderful plugin for VS 2012 – PowerShell Tools for Visual Studio – http://visualstudiogallery.msdn.microsoft.com/c9eb3ba8-0c59-4944-9a62-6eee37294597 and all worked as expected, until (as far as I remember) I restarted OS and had this strange error in VS in my output window :

Error: File C:\users\your_name\documents\visual studio 2012\Projects\PowerShell Script Project1\PowerShell Script Project1\Script.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.

The program ‘PowerShell Script’ has exited with code 0 (0x0)
(Small side note here to make you more puzzled but this might also help you in some way : I work in a company where admins takes security seriously and our “normal” day to day accounts are restricted and don’t have access to this and that so we have another accounts to install sw, make changes to the system, etc. I need to run my VS under this account, not my own.)
Great, no what? There is a link to documentation about what execution policies are and how to set them, etc, I changed it from vanilla restricted state to RemoteSigned and the very same .ps1 file worked after I set RemoteSigned for my and my admin account. It looks like PS stores policies per user in registry. More on this here : http://dusan.kuzmanovic.net/2012/01/04/powershell-execution-policy/

 

Than I had “brilliant” idea to try PowerShell commands in NuGet Package Manager Console because it is using PowerShell under the hood, I was just looking for some prompt to query the ExecutionPolicy. And the magic happened, after query the PowerShell worked!!!! Brilliant but this is just workaround and not the solution.

 

After this the best idea seemed to contact the author so I mailed directly Adam Driscoll (apologies for distraction) which led to few discoveries like :
  • Writing some text in the .ps1 file in VS and selecting some code and right click reveals Execute selection option that is able to run your code event if it fails running it F5,
  • Adam downloaded in his spare time NuGet sources and it looks like : “NuGet sets the process scoped execution policy to RemoteSigned when it is initialized. “

So next logical step would be to try to run Set-ExecutionPolicy from VS to set it for account that VS is using to run PowerShell. And it looks like it is working.

Enjoy guys and thanks Adam for your plugin and your time.