Friday, June 7, 2013

ViewModel inheritance in knockout

Problem : ViewModel inheritance in knockout.


Problem Description: Knockout view models aren't as simple as a simple object that can be easily extended, specially when your view model can contain computed observable which should be executed immediately. If you are already familiar with various ways of inheritance in javascript using various frameworks or just plain javascript methods, then you probably must have already figured out why the immediate executing computed observable poses a major blockade in using them.


Solution

                var Person = function () {
                var self = this;
                self.Name = 'Base';
                self.KoName = ko.observable('Person Name');
                self.FullName = ko.computed(function () {
                    //alert('into koFullName');
                    //alert(self.Name);
                    
                    return self.KoName() + ' Full Name in base';
                });
                self.getName = function () {
                    return self.KoName();
                };
            };

            var Employee = function () {
                var self = this;
                Person.apply(self, arguments); //arguments if any
               
                Extend(self);

                self.KoName('Employee');
                
                self.JoinDate = ko.observable('01/01/2009');
               
                self.FullName = ko.computed(function () {
                    var baseName = self.base.FullName();
                    return baseName+ '  Employeeee Overridden: ' + self.KoName() + ' Full Name';
                });
                self.getName = function () {
                    return 'Employeeee : ' + self.KoName();
                };
            };

            var Client = function () {
                var self = this;
                Person.apply(self, arguments); //arguments if any
                Extend(self);
                self.KoName('Client');
                self.FullName = ko.computed(function () {

                    return 'Client Overridden: ' + self.KoName() + ' Full Name';
                });
                self.getName = function () {
                    return 'Client : ' + self.KoName();
                };

            };

            function Extend(self) {
                var base = { };
                base = { };
                for (propName in self) {
                    var propValue = self[propName];
                    base[propName] = propValue;

                }

                self.base = base;
                return self;
            }

        var employeeVm = new Employee();

        alert(employeeVm.FullName()); //this alerts  "Employee Full Name in base  Employeeee Overridden: Employee Full Name"
        var clientVm = new Client(); 
        alert(clientVm.FullName()); //this alerts "Client Overridden: Client Full Name"

As you can see here Person is the base ViewModel which is extended by Employee and Client. Each of these derived classes invokes the Person function to apply all the properties and functions from Person to itself. Then they call Extend function which basically is responsible to create base property in the viewmodel with the existing properties and functions. Remember that you must call this function exactly after invoking Person function and before adding or overriding the existing functions from base class. This is required to due to the way Extend function creates a new base property and copies the existing properties and functions to it. This Extend function  lets you access your base class using base keyword and still call it later if you want.
If you have a better solution to this then please let me know. I'd really really appreciate that.

Saturday, April 20, 2013

Running .net application as an Administrator using UAC

You probably are already familiar with the whole new concept of UAC(User Access Control) security model that Microsoft introduced since windows Vista. This security model restricts the application from making system level changes in your application and requires any application that need such kind of access control to go through UAC dialog for user's consent. User can then authorize or simply cancel the application execution.


My purpose here is not to go in-depth regarding UAC, however as an application developer building a desktop app that requires administrative privilage to make system changes, you and I both are interested in popping up this UAC dialog to run the application with Administrator privilege so that your application do not fail with security reason. Please remember that if your application do not perform any Administrative task then do not show this dialog for fun as It might irritate your user and this is something you always need to be careful about as a developer.

The solution is rather simple and shorter than all the bullshits that I bothered you with, above.

  1. Add "Application Manifest File" in your application using Add New Item Dialog.
  2. Find <requestedExecutionLevel /> element inside the manifest file. This is the only element in the whole manifest file we are interested in right now.
  3. If you have gotten this far and have properly read the comment inside the manifest file then you already know what to do next.
  4. If not change/add level attribute in this element to "requireAdministrator". By default it is "asInvoker".
  5. Now compile your application and run the exe inside "bin" folder. 
  6. After the changes your <requestedExecutionLevel /> element should look something like this <requestedExecutionLevel level="asInvoker" uiAccess="false" />
If you are looking for a demo then you can download the sample code from here.



Saturday, April 6, 2013

Sending office document content from Office App to server side

So here goes another post for App for Office Api. This post will focus on providing you with some basic knowledge on sending the whole document from your app to your server side code.
The sample I tried on is built on Asp.net MVC4, but it necessarily doesn't mean that this thing is possible on Asp.net or Asp.net MVC only. Any server side language that can handle ajax calls and can convert base 64 string to byte array can do :)

Another thing to note down here is that this sample will use one of my javascript library that I named it "OffQuery". This library is just a wrapper on top of Office.js javascript api aiming at hiding most of the complexities for simple functionalities. It also supports jquery's promise giving you a more flexible and jquery way to handle Office.js asynchronous calls. For more on it I'd suggest visiting jquery site and get your hands dirty with couple of samples yourself.  "OffQuery" is still in it's infancy and hope to post some more examples , if I'm able to continue on it. But today let's focus on learning how can we send the whole office document from your app to server side and write on your disk.

I won't be going in detail on setting up your project for this tutorial, which obviously varies depending on the server side language you are familiar with. However in this sample I'll show you a way to get your document content using OffQuery and then a simple Mvc controller action that handles the ajax calls and write the content to disk. If you are an Asp.net MVC developer and want to convert your Asp.net MVC project to App for office project then you can visit my previous posts "Convert existing web project to office app using Visual Studio 2012" and "Convert App For Office Project to Asp.net MVC Project Type"

For this tutorial make sure you include MicrosoftAjax.js, Office.js, Jquery OffQuery.js. For jquery you can download the latest file from jquery site and for OffQuery you can visit https://github.com/ishwormali/OffQuery.
After that write javascript to handle Office.initialize function. Please make sure you do this before calling any of the Office or OffQuery api's







Next write your javascript to get the document content in byte array.

function saveContent() {

    $.when(OffQuery.getContent({ sliceSize: 50000 }, function (j, data, result, file, opt) {
        var temp = data;

    })).then(function (finalByteArray, file, opt) {
        //this function is called at the end.
        //FullData contains byte array.
        //file is the file object of Office.js
        //opt is the option supplied at the very beginning when calling OffQuery.getContent() function.
        var fileContent = OSF.OUtil.encodeBase64(finalByteArray); //encode the byte array into base64 string.
        sendFileMethod(fileContent)
     

    }).progress(function(j, chunkOfData, result, file, opt){
        //handle the progress here.
    });
}



This function basically leverages the jquery's new inbuilt asynchronous api $.when. The anonymous function provided in then function will be called after the asynchronous call to getContent function is complete. If you have been using office.js then you probably have noted that it's very simple to get the whole content at once using OffQuery rather than using the Office.context.document.getFileAsync() function, providing your own implementation to handle the recursive calls and all. The OSF.OUtil.encodeBase64() function converts your byte array to Base64 string ready for sending it to your server.

function sendFileMethod(fileContent) {

    $.ajax({
        url: saveContentUrl,
        data: { contentByte: fileContent },
        type: 'POST'


    }).then(function (a, b, c) {
        var tempa = a;
        var tempb = b;

    });
}


sendFileMethod doesn't do much than sending the content to server using jquery's ajax call.

public JsonResult SaveContent(string contentByte)
        {
         
            var byteArr2 =Convert.FromBase64String(contentByte);//convert the base64 string to byte array
            using (var fileStream = System.IO.File.OpenWrite(@"c:\tempfolder\tempfile.docx")) //open a file
            {
                fileStream.Write(byteArr2, 0, byteArr2.Length); //write the byte array as content in the file
            }

            return Json(new { success=true});
         
        }




The server side code is self explanatory. It simply converts the base64 string to byte array, opens a .docx file on disk and then writes the byte array. The code provided is in Asp.net MVC.

Download the sample code from here.


Saturday, March 30, 2013

Convert App For Office Project to Asp.net MVC Project Type


When you create a new office app project Microsoft visual studio automatically creates two projects for you- one is a web project and the other one is the manifest project which essentially contains all the information about your app required by Microsoft office. The web project created for you is a simple web form project. As microsoft already stated that office app can be built on top of any server side language, or it may not have any server side language at all, provided that you have a manifest uploaded in any app catalog and your web is hosted in a valid http url.
However sometimes you may want to convert your office web project to a full blown Asp.net Mvc project and benefit from some server side technology. Here I provide you with some basic steps and tweakings required to convert your office app web project to Asp.net Mvc Project.

If you are looking for converting an existing MVC project to office app then you can follow this link



  1. Unload the project
    1. Right click the project and click on Unload Project Menu
  2. Now Right click the project again and click on Edit {Project Name}.


Now we will be doing few changes in project xml to get the project up as a MVC project type.


  1. Locate the <ProjectTypeGuids> within <PropertyGroup> xml element and replace the whole field with this

{E3E379DF-F4C6-4180-9B81-6769533ABE47};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}


  1. Add <MvcBuildViews>false</MvcBuildViews> inside <PropertyGroup> xml element.
  2. Add the following reference along with other references. Please note that we will be resolving these assemblies using Nuget later on. For now just include them in project file. Also remember that these are the standard references which come along when you create a new Mvc 4 project. Some of the assemblies here may not be required at all such as EntityFramework unless you decide to use EntityFramework as your ORM.



      ..\packages\EntityFramework.5.0.0\lib\net45\EntityFramework.dll
    
    
      True
      ..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll
    
    
      ..\packages\Newtonsoft.Json.4.5.6\lib\net40\Newtonsoft.Json.dll
    
    
    
    
      ..\packages\Microsoft.AspNet.WebApi.Client.4.0.20710.0\lib\net40\System.Net.Http.Formatting.dll
    
    
    
    
      True
      ..\packages\Microsoft.AspNet.WebPages.2.0.20710.0\lib\net40\System.Web.Helpers.dll
    
    
      ..\packages\Microsoft.AspNet.WebApi.Core.4.0.20710.0\lib\net40\System.Web.Http.dll
    
    
      ..\packages\Microsoft.AspNet.WebApi.WebHost.4.0.20710.0\lib\net40\System.Web.Http.WebHost.dll
    
    
      True
      ..\packages\Microsoft.AspNet.Mvc.4.0.20710.0\lib\net40\System.Web.Mvc.dll
    
    
      ..\packages\Microsoft.AspNet.Web.Optimization.1.0.0\lib\net40\System.Web.Optimization.dll
    
    
      True
      ..\packages\Microsoft.AspNet.Razor.2.0.20710.0\lib\net40\System.Web.Razor.dll
    
    
      True
      ..\packages\Microsoft.AspNet.WebPages.2.0.20710.0\lib\net40\System.Web.WebPages.dll
    
    
      True
      ..\packages\Microsoft.AspNet.WebPages.2.0.20710.0\lib\net40\System.Web.WebPages.Deployment.dll
    
    
      True
      ..\packages\Microsoft.AspNet.WebPages.2.0.20710.0\lib\net40\System.Web.WebPages.Razor.dll
    
    
      True
      ..\packages\WebGrease.1.1.0\lib\WebGrease.dll
    
    
      True
      ..\packages\WebGrease.1.1.0\lib\Antlr3.Runtime.dll
    



  1. Add the following Target above ProjectExtensions. Order is not mandatory. However make sure this element is at the same element as <ProjectExtensions> element is.



    
  



  1. Right click the project file and click on Reload menu.


Now your project file is ready so that visual studio can recognize it as an MVC project with all the contextual features specific to MVC.


  1. Remember the assemblies that we included in our project file’s xml? It’s time to resolve them using nuget. Some of the assemblies might not be resolved. In my case some assemblies such as Antlr3.Runtime, EntityFramework, Newtonsoft.Json, System.Web.Optimization and WebGrease assemblies are not resolved.
  2. Right click the references and Click on Manage Nuget Packages.
  3. Under Online tab search for Microsoft.AspNet.Web.Optimization for System.Web.Optimization and click on Install Remember while installing this assembly other assemblies such as WebGrease, Antlr3.Runtime are automatically resolve due to it’s dependencies.
    1. Similarly resolve Newtonsoft.Json and EntityFramework. Remember Newtonsoft.Json is actually displayed as Json.net in Nuget Package manager.



  1. Now let’s structure our project similar to what a typical MVC 4 project looks like. Remember I won’t be going into details on this as this might take me ages to do so. Just grab or create a new mvc4 project and copy them into this project.
    1. Add Controllers, View Folders.
    2. Add BundleConfig.cs, FilterConfig.cs, RouteConfig.cs and WebApiConfig.cs(if required).
    3. Add Global.asax file and call Register functions on the above classess.
    4. Change your root web.config file to include these configuration elements



    
    
    
    
    
  

    
    
    
    
    
      
        
        
        
        
        
        
        
      
    
  
 
    
    
      
      
      
      
      
      
    
  

    
      
        
        
      
      
        
        
      
      
        
        
      
    
  



  1. Also make sure you are not missing anything from the below configuration elements in your web.config file inside views folder. If you don’t have web.config inside views folder, then add one.


    
      


  1. Now start adding your controllers, views and necessary js files and all to get working
  2. Last but not the least, go to your office app manifest and change the Source location to an appropriate route to your application. After everythings is complete don’t forget to close the project and reopen it again.

Hope this helped you to understand how flexible and easy it is to get started with building Apps for Office. Apps for Office for now have very few features/apis, however I am really hoping that Microsoft is working on extending Microsoft office web apps and adding new functionalities that developers can leverage to create stunning and powerful apps.


I am really excited to try out this new technology and hopefully will be able to continue further with my journey exploring and digging deep into Office App apis.

Saturday, March 9, 2013

Convert existing web project to office app using Visual Studio 2012


Converting an existing web project to office app in Visual Studio 2012 is as simple as a piece of cake. Simply follow the instructions below to get started and see it for yourself.


  1. Right click on your web project.


  1. Click on “Add App for Office Project”. After this you will be given a dialog asking you for few options which are similar to the ones when you create a new “App for Office” Project.
  2. After this you can see that a new project {YourProjectName}.office is added into your solution. This project holds the manifest file used by Microsoft Office to determine the app’s information.
  3. After that don’t forget to check the Source Location under manifest settings.
  4. Now save your solution and then fire up your application clicking on the start tool bar button.


Sunday, February 3, 2013

Execute Razor Helper Method from Code File

Problem: I really really hate myself when I have to write html syntax in code file using TagBuilder. All those html syntaxes <div>I hate myself </div> don't really belong in class file. It really should be in a html file or in my case some .cshtml file. And yes I am aware that these declarative helper functions in app_code are ultimately converted to classes, however it's much cooler to write html in cshtml file rather than in cs file(Of course it's just my opinion :) )

Solution: It really really is simple. After doing a lot of digging into some source code I came up with this simple solution using BuildManager and some reflection. Here it goes:


public static string RenderHelperMethod(string virtualPathOfSomePage,string functionName, params object[] paramsToPass)
        {

            var typ = BuildManager.GetCompiledType(virtualPathOfSomePage);
            if (typ != null)
            {
                HttpContextWrapper wrapper = new HttpContextWrapper(HttpContext.Current);
                var tm = typ.GetMethod(functionName);
                var inst = Activator.CreateInstance(typ);
               var webpage = inst as System.Web.Mvc.WebViewPage;
                webpage.Context = wrapper;

                var result = tm.Invoke(inst, paramsToPass);
                if (result != null)
                {
                    return result.ToString();
                }

            }

            return null;
        }

BuildManager  provides some other static methods that may interest you. Please dig into this more if you are really interested in doing some research on dynamic compilation and all. And of course don't forget to drop in some links if you find anything more on this topic.

Monday, January 28, 2013

Remove write/edit protection/enforcement from Microsoft word document from code.


This is not a kind of solution that I usually like bragging about the whole day. However as I couldn't find anything as such in the internet, I thought it would be nicer to add one more scarce resource to be available in the internet, if in case somebody else is looking for it.

Problem: How to remove a password from password enforced word document using c# code(or may be any other code) when you already have a password. Before getting in to the solution let's see what kind of password protection/enforcement I'm talking about.

To enforce write/edit protection in a word document you can simply follow the following steps.
  1. Create a new word document.
  2. Go to Review ribbon tab.
  3. Click on Protect Document.
  4. Check the Formatting restrictions if you want users not to be able to apply extra formatting on the document.
  5. Check the Allow only this type of editing in the document if you want users to be able to apply certain kind of changes to the document. To do so select the kind of changes you want to allow the user.
  6. Click on Yes, Start Enforcing Protection button.
  7. Save and close.



Disclaimer: Please note that this is not a cracking, not even closer to something that can be called cracking.

Solution:

public void RemovePasswordFromDocument(string documentPath, string password)
        {
            var app = new Application();
            app.Visible = true;
            object originalFile = documentPath;
            object oFalse = false;
            object missingObject = Missing.Value;
            try
            {
                object generatedPwd = password;
                var doc = app.Documents.Open(ref originalFile,
                                             ref missingObject, ref oFalse,
                                             ref missingObject,
                                             ref generatedPwd, ref generatedPwd,
                                             ref missingObject,
                                             ref generatedPwd, ref generatedPwd);

                if (doc.ProtectionType != WdProtectionType.wdNoProtection)
                {
                    doc.Unprotect(ref generatedPwd);
                }

                doc.Save();

            }
            finally
            {
                app.Documents.Close(ref oFalse, ref missingObject,
                                    ref missingObject);

                app.Quit(ref oFalse, ref oFalse, ref oFalse);
            }

        }

That's it. Code is pretty straightforward. Only thing you need to keep in mind is that your project needs to reference two assemblies Microsoft.Office.Interop.Word.dll and office.dll and these are something that are installed when you install Microsoft office application itself. So no worries.

Monday, January 7, 2013

Thought on Preview of Immutable Collections

Did you ever think of having immutable collection in your .net code - a collection that is really thread-safe and gurantees no other thread will ever change it while one thread is sort of reading it? I thought ReadOnlyCollection<T> and ConcurrentBag<T> were to my rescue, but surprisingly they aren’t.

Early this morning while having my regular cup of coffee “keep me updated” time, I came across this news "Preview of Immutable Collections Released on NuGet". Well apparently this immutable collection is something to think about, and not surprisingly Microsoft is already on it. I’m assuming that you are already familiar with the “immutable” concept in .net. If not you can take string as an example. String is one of a kind immutable collection of char, which means whenever you add or remove a character from a string object you get a new one. The state of immutable objects cannot be changed. When you do so, you get a new object instead. The older one is still intact. For more explaination you can always refer to wiki.

Immutable collections behave in a similar way. If you have grabbed the concept of immutability(getting a different one after changing the original), then you surely must be thinking that this thing is going to cost you some memory and for a huge list- millions of byte. On the contrary, immutable list are stored as immutable binary tree structures instead of flat arrays. This has a huge benefit as the new list returned after changes share the same memory space with the original immutable list.

So what if you want your list to be mutable for an instance, make the necessary changes-addition and removal of the list items and then return back the immutable list once again. This is going to save you some overhead. Bingo....Immutable lists do have a method called ToBuilder() that gives you it’s corresponding Builder class similar to StringBuilder. You can then mutate the collection, and use ToImmutable() function of the builder to return Immutable list again. The concept is very much similar to String and StringBuilder, so won’t be that hard to get used to it.

Immutable collection seem to be worth trying out. Haven’t done any benchmarking test on it yet, but hopefully somebody will come up with it one day. :) . Do check it out, but make sure you read this post by Eric Lippert as well.