Tuesday, December 16, 2014

Quickly! Create an excel sheet with many aligned shapes

That's why it's good to be familiar with various APIs: programmatic access allows the automation of the repetitive tasks. Sometimes you can save yourself from lots of grunt work (and/or save time). I have "always delegate to the machine what it can do" or ingrained in my value system.

I added around 50 checkboxes to an excel sheet quickly for some ad-hoc required document (that also could have been automated :). I just copied and pasted and the list of checkboxes ended up having a misaligned list as you can see in the following screenshot.

I was hoping I'll find an "align lefts" feature that will automatically align the lefts of all the shapes. Nope. Someone from the office cuckled: "do it one by one", knowing that this is one extraordinarily mundane task. I said to myself: Nope.

I immediately decided to write up something that will adjust them for me. It should be and it was trivial. The following piece of code for a Visual Studio Excel Addin project was a one minute task. Debug. Run. Mission accomplished without losing my vision.

private void button1_Click(object sender, RibbonControlEventArgs e)
    var shapes = ThisAddIn.ApplicationInstance.ActiveSheet.Shapes as Excel.Shapes;
    float firstLeft = -123; //magic
    for (var i = 1; i <= shapes.Count; i++)
        var item = shapes.Item(i);
        if (firstLeft == -123)
            firstLeft = item.Left;
        item.Left = firstLeft;

Tada! The check boxes are aligned. You can add more calculations to improve the layout by spreading the items vertically or even into a grid. You can do whatever you want depending on what you need.

Sunday, November 23, 2014

ORM comparison: NHibernate loses to Entity Framework

I think that NHibernate is better than Entity Framework in several ways. Especially when it comes to NHibernate's ability to change the Code First model so easily without dropping and recreating the database. No data migrations, if you add a column to your model, a column is added to your database.

So it's easier in many ways, different in others, a little harder in others, but today I found a situation I was surprised that NHiberate simply loses to Entity Framework. The support to convert String.Contains to a Like expression is never a problem in Entity Framework in which you can use String.Contains on an entity in a LINQ expression built using a query or extension methods.

In NHibernate, using the String.Contains method results in an exception that says: "Unrecognised method call: System.String:Boolean Contains(System.String)"

To my surprise I had to create NHibernate.Criterion.ICriterion expressions similar to LINQ expressions.

var search = session.QueryOver<PersonHBN>()
                        .Where(new NHibernate.Criterion.AndExpression(
                            new NHibernate.Criterion.EqPropertyExpression("OtherEntity.id"NHibernate.Criterion.Projections.Constant(id)), 
                            new NHibernate.Criterion.LikeExpression("name"text)));

That was so much for just x.name.Contains(text)! But it is good to see that in NHibernate, there is an expression system that is similar to that of LINQ expression which is very powerful.

Saturday, November 1, 2014

I promise I'll let you know where I am (HTML5 GeoPosition API)

One of my friends is such a sweetheart. Whenever I drop her home she's concerned about me especially because she knows how I drive so she asks me to promise her that I'll let her know when I reach my home. Sometimes I do, sometimes I forget, and sometimes she calls me to ask.

She came to my mind while I was playing today with jQuery's deferred and JavaScript 6 promises with the GeoLocation API, i.e. navigator.geolocation.getCurrentPosition(successcalback, failcallback, options);

Promises is a standard part of EcmaScript 6 and is natively supported in modern browsers. It is an alternate way to handling asynchronocity prevalent in modern web applications and much cleaner than callbacks in certain situations. Callbacks are better suited in other asynchronous situations (for example a UI event).

Just to try promises out (native and jQuery's deferred), I'll ask the user for their location as a promise. I might get the location, a declination, or I will wait forever (until the browser's page is closed).

The only native geolocation API relies on callbacks as you can see from the signature above with the success and failure callback parameters. To use promises, I'm going to use a jQuery deferred object (an object that is eventually resolved), and abstract the geolocation API with a function that returns a promise of the geolocation (or a declination, and otherwise we'll just wait forever). The function locationPromise below does so.

If you often ask the user for their location using getCurrentPosition, you might want to use a promise-styled API just like the one below.

function locationPromise() {
    var def = $.Deferred();
    var gotLocation = function (geopos) {
    var notLocation = function () {
        def.rejectWith("Wonder where you are!");
    window.navigator.geolocation.getCurrentPosition(gotLocation, notLocation, {});
    var myPromise = def.promise();
    return myPromise;

We use this function as follows:

function getLocationPromise() {
        .done(function () {
            //this will be the value resolved or rejected
        .fail(function () { alert("sorry weren't able to get pos"); });

The usefulness of this depends on the architecture of your application. You base your architecture on common functionality that handles promises with metadata (for example as how to respond to success or failure) instead of callbacks. So this examples shows how you can convert an asynchronous HTML5 operation (geolocation) to a promise.

Sunday, September 21, 2014

Just a thought: A PDF document must be opened with PdfDocumentOpenMode.Import to import pages from it.

The solution for the problem is very obvious as outlined below, so this is a reflection (a thought) on developer behavior and API friendliness.

Update (Dec-4-2014):

Same with an error message while dealing with NewtonSoft.Json deserialization. This is

Message: "Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'ListOfPumpkins' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. 
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object."
This is an actual error message, so much beyond the common COM Exceptions that .NET developers who only deal with managed APIs face which go something like: "COM Exception: 1234, deal with it". This actually shows to what extent the developers of the library care for their follow developers. Once they saw and expected them that they might do something incorrectly and that their code run into an exceptional situation, they communicate it with a good exception like the one above.

Original Post:
This is not a logical bug: a lot of times programmers are not faced with algorithmic challenges. In the code snippet below, there is nothing much to this for loop that add pages from a PdfDocument to another using PDFSharp.

foreach(var reportStream in reportStreams)
    var pdfDoc = PdfReader.Open(reportStream);
    foreach (PdfPage page in pdfDoc.Pages)

Developers do not scratch their heads to write that piece of code, but sometimes the simplest things take time and effort because developers do not understand how APIs work. We live in a software development world that is filled with APIs whether on the web or local and for most using APIs is a natural part of their jobs which is good until the benefits of using the API e.g. avoid reinventing the wheel and making use of others who studied the object of the API more in depth, are outweighed by the disadvantages: API learning curve as APIs involve learning concepts of the API and a sophisticated object structure, going around things that are not implemented by the API, and so on.

In this particular case, the API that I was using, the PDFSharp library, threw an exception that told exactly what was expected. This is an example of excellent API communication. It told me that I have opened a PDF Document in a mode that did not allow me to import PDFs.

So simply adding PdfDocumentOpenMode.Import solves the issue:

foreach(var reportStream in reportStreams)
    var pdfDoc = PdfReader.Open(reportStream, PdfDocumentOpenMode.Import);
    foreach (PdfPage page in pdfDoc.Pages)

I've had experience with other API that throws the most obscure exceptions even at locations where it is hard to trace where your erroneous API usage originated. Or doesn't even through exceptions but does not do what is required, e.g. doesn't add pages to the main doc so it leaves it up to you to guess what went wrong: the loop was not entered for some reason, reportStreams is empty, the page elements are null, etc?

I can hardly consider fixing such an issue as a programming challenge because APIs are designed by humans, or even if they are "engineered" by humans, there are a lot of things that are just arbitrary regarding how APIs are built. Of course there are choices that makes sense more than others such as naming an API method as GetPurchaseOrders(DateTime, DateTime) rather than GetSome(DateTime, DateTime). The choices increase exponentially from here (Parameter numbers and types, overloads, type structure, complexity, error messages, returning null on failure, empty objects, etc...).

I wonder about the ways to actually "engineer" or systematize the process of "designing" an API? 

Wednesday, September 17, 2014

SharePoint CustomAction Features can be used for 3 things

Well I have a very short post because I want to note this simple fact in SharePoint customization.

Custom Action features (CustomAction XML element) is used with for both ribbon actions, site settings page section and I think list item context menus. The naming matches directly for ribbon buttons and context menus but not as much for site settings section.

And all you need to do is set the attribute Location="Microsoft.SharePoint.SiteSettings" in the CustomAction element, to have it as a site settings action. To create an item for the ribbon set Location="CommandUI.Ribbon"

Example Elements.xml of a feature:

     Title="Configuration Section Title"
     Description="blah blah"

    Title="SharePoint custom configuration page (application page)"

Thursday, August 7, 2014

Developer doing administration because of SharePoint

Even if you are on the code side of things, i.e. a programmer, you are exposed to consider a lot of things that usually belong to the administration and the infrastructure domains because a lot of what you need to use as a develop in SharePoint, relies on things that are configured by administrators.

One first initial example is that SharePoint must run within a domain, this means that you will need to create a server with Active Directory Domain Services, called ADDS. This server is called the domain controller.

Virtually everything else relies on administration related work. If you want to configure search, you will need the administrator to create a domain account for the search service. In my company developers do not have administration rights, so they must ask the administration team's help. If they are busy or not even convinced that you should do what you want to do (even on a test server), then you're out of luck. I wonder where is my negotiation skills?! Anyways, that was so annoying for me that I decided to create my own environment on my machine. Luckily I had 16GB of physical ram and an SSD. Not bad and could be used for testing purposes.

Windows Server VM, SQL Server, Domain Controller with a static IP, internet connection? Etc..

As someone whose administration skills are "me can creates adhoc network" this opens up a whole new world for me. I am learning a lot of curious things.

Wednesday, August 6, 2014

Restoring deleted Site Collections in SharePoint 2013

Useful MSDN resource: http://technet.microsoft.com/en-us/library/ff607925(v=office.15).aspx

I just learned what's the identity of a site. At least according to what Restore-SPDeletedSite knows.

This PowerShell SharePoint command is the only way to restore a deleted site collection. In SharePoint 2013 there is no UI in central administration, or even SharePoint designer as far as I have searched that allows you to do this.

To get the list of Site Collections that were deleted and are restorable, use the Get-SPDeletedSite command that will return SPDeletedSite objects.

For example (on a test server, hence http not https) gets you the following information:

WebApplicationId   : 9fa68842-c64d-493b-8102-15d4929d2fb6
DatabaseId         : dcacbbf9-719b-41b7-b7f9-526bce8100ff
SiteSubscriptionId : 00000000-0000-0000-0000-000000000000
SiteId             : b1110b1f-822b-4d70-aace-09dcba30aeca
Path               : /sites/test3
Scheme             : Http
Url                : http://msesrv30/sites/test3
DeletionTime       : 8/6/2014 8:05:41 AM

But which one of these is the site's identity? Sometimes, or a lot of times depending on how new your are to SharePoint, the terminology that is used is confusing. Anyways after a few tries someone realized that they can use the site Path as the identity while invoking the Restore-SPDeletedSite PowerShell command.

Don't forget the preceding forward-slash (/). Using sites/test3 will not work in this example.

Restore-SPDeletedSite -Identity /sites/test3