Wednesday, September 11, 2013

Developer Tip: Avoid random numerical constants

There always comes a time when we just need to use a single number or set of numbers in our code…. but please avoid typing something like:

if (permissionID == 3){
    // do something
}

Instantly I thinking

What is 3?

Is there a 1 and 2?

Is there documentation somewhere that can help?”

You can add a comment, that would be a bit helpful. But I believe a better solution is either an Enum or Class of constants. These solutions make your code more readable, and give more context around what “3” is.

Enum:

enum Permissions : int{
    None = 0,
    Read = 1,
    Write = 2,
    FullControl = 3
}
 
// .....
 
if (permissionID == (int)Permissions.FullControl){
    // do something
}
Class of Constants:
public static class Permissions {
    public static const int None = 0;
    public static const int Read = 1;
    public static const int Write = 2;
    public static const int FullControl = 3;
}
 
// .....
 
if (permissionID == Permissions.FullControl){
    // do something
}

Obviously these aren’t the only solutions… simply making a variable with a descriptive name might be enough…. but DO NOT just type a constant in the middle of your code.

Thursday, September 5, 2013

A must have SharePoint Management Tool

I know I’m late to the game on this, but I thought I would share for others that are unaware.

If you manage or develop against SharePoint you should look into

SharePoint Manager 2010

image

It’s a great tool for finding feature ID’s, managing lists, pretty much anything you need to know about your SharePoint server and everything in it.

Wednesday, May 1, 2013

C# code to force the debugger to launch

There are times where you just can’t manually attach the Visual Studio debugger to your code. Like when you’re trying to debug Sharepoint feature events (FeatureActivated, FeatureDeactivating, etc.) when Visual Studio is deploying your SharePoint project.

For those situations you can use this line of code to attach Visual Studio to your process.

System.Diagnostics.Debugger.Launch();

image

image

Just make sure to remove it before you ship your software.

Friday, March 1, 2013

HTTP Request Headers Testing

This is a trick I’ve been using for a while, but I wanted to share.

Sometimes third party software requests your Web Pages/Services and pass data via the HTTP Request Headers.

If you want to simulate these requests you can do so with Fiddler. Fiddler acts as a “man in the middle” for all the requests coming from your browser. Therefore, it has the ability to alter your HTTP requests before they are passed along to the appropriate destination.

Here is how you add a header to your browser requests.

Launch Fiddler and select Rules > Custom Rules…

Find the function called

static function OnBeforeRequest(oSession: Session)

Within this function you can add items to your request header by assigning key/value combination to the “oSession.oRequest” array.

Here’s an example:

static function OnBeforeRequest(oSession: Session)
{ 
    oSession.oRequest["Custom_Header_Key"] = "Custom Header Value";
}

Now any request from a browser will contain that value. Including localhost requests

2013-03-01_1427

For more information about Custom Rules, I encourage your to visit the Fiddler Script Cookbook

Wednesday, February 6, 2013

What is the “Mythical Man Month”

Well, there is a book out there explaining it.
The Mythical Man-Month: Essays on Software Engineering, Anniversary Edition (2nd Edition)
Though a great source, it’s one dry read.

I actually recommend the cliff notes.

The basic gist is this:

If a developer gives an estimate for a project that might take 320 hours or “2 Man Months.” Some might think 2 developers can complete the same task in half the time. 

This might work in a manufacturing world, but not so well in a software world because of two factors, partitioning and communication to maintain the partitions.

The reality of the matter is, software depends on other software. Some things can’t be independently partitioned. When they can’t, developers must constantly communicate to make sure their pieces will work together. This communication takes additional time.  The more developers you add, the more communication you need, and therefore the more time you’ll have to spend.

There is a “sweet spot,” but unfortunately, there isn’t a perfect way to calculate it. When writing software, every project is different.

-Justin Kohnen

For a better explanation I encourage you to read the book, or at least the cliff notes.

Monday, February 4, 2013

How to use Window 8 on a PC

So you’re thinking about installing Windows 8 (or you’ve already installed it) and you’re wondering “It’s changed so much, How the heck do I use it?”

I wanted to share an instructional video with you that will answer just that question.

Here is the 25 minute version

And here is the “3 minute” version

Friday, February 1, 2013

Preparing a SQL database for CLR assembly loading

A few times, I’ve had to install CLR assemblies into a Microsoft SQL database.

In order to do so, the deploying user must be granted certain permissions.

Here is the script I use.

/*===========================================================
  THIS SCRIPT WILL PREPARE A SERVER FOR LOADING AN ASSEMBLY 
  BY GRANTING RIGHTS FOR THE SPECIFIED USER TO LOAD
  ASSEMBLIES OF ALL PERMISSION SETS
  AND SETTING THE DATABASE TO TRUSTWORTHY TO 
  ALLOW THE ASSEMBLY TO BE LOADED INTO CLR
  NOTE: MAKE SURE YOU REPLACE THE <Username> PLACEHOLDER
    WITH THE USERNAME YOU WISH TO ELEVATE PERMISSIONS FOR
  NOTE 2: A USER CANNOT ELEVATE THEIR OWN PERMISSIONS. THIS SCRIPT
    SHOULD BE RUN BY A DIFFERENT USER THAN THE ONE BEING ELEVATED
  NOTE 3: RUN IN DATABASE YOU WANT TO LOAD THE ASSEMBLY
  ===========================================================*/
       DECLARE @user NVARCHAR(50)
       DECLARE @tsql NVARCHAR(MAX)
       DECLARE @error INT
 
       SELECT @user = '<Username>'
 
       PRINT 'STEP PREPARE SERVER AND DATABASE FOR ASSEMBLY LOADING'
       PRINT 'SERVER  :   [' + @@SERVERNAME + ']'
       PRINT 'DATEBASE:   [' + db_name() + ']'
       PRINT 'USER    :   ' + @user
       PRINT 'START TIME: ' + convert(varchar(max), getdate())
 
       PRINT ''
       PRINT 'GRANT PERMISSION TO LOAD EXTERNAL_ACCESS ASSEMBLIES'
       SET @tsql = 'USE MASTER ' + char(13) + char(10) + 
                            'GRANT EXTERNAL ACCESS ASSEMBLY TO ' + @user + ''
 
       EXEC sp_ExecuteSQL @tsql 
       SET @Error = @@Error
       IF @Error <> 0 
              BEGIN
                     PRINT 'ERROR OCCURRED: ' + CONVERT(varchar(50), @error)
                     PRINT 'GRANTING EXTERNAL_ACCESS LOADING PERMISSIONS'
                     PRINT 'TO ' + @user
              END
       ELSE
              BEGIN
              PRINT 'GRANT PERMISSION TO LOAD UNSAFE ASSEMBLIES'
              SET @tsql = 'USE MASTER ' + char(13) + char(10) + 
                                   'GRANT UNSAFE ASSEMBLY TO ' + @user + ''
              
              EXECUTE sp_ExecuteSQL @tsql 
              SET @Error = @@Error
              IF @Error <> 0 
                     BEGIN
                           PRINT 'ERROR OCCURRED: ' + CONVERT(varchar(50), @error)
                           PRINT 'GRANTING UNSAFE LOADING PERMISSIONS'
                           PRINT 'TO ' + @user
                     END
              ELSE
                     BEGIN
                     PRINT 'MARK DATABASE ' + db_name() + ' SO ASSEMBLIES CAN RUN'
                     SET @tsql = N'USE MASTER ' + char(13) + char(10) + 
                                          'ALTER DATABASE ' + db_name() +
                                         ' SET TRUSTWORTHY ON '
                     EXEC sp_Executesql @tsql 
                     SET @error = @@ERROR
                     IF @error <> 0 
                           BEGIN
                           PRINT 'ERROR OCCURRED: ' + CONVERT(varchar(50), @error)
                           PRINT 'MARKING DATABASE ' + db_name() + ' SAFE TO RUN ASSEMBLIES'
                           END
                     END
              END                        
       PRINT ''
       PRINT 'STEP COMPLETED'
       PRINT 'END TIME: ' + convert(varchar(max), getdate())

I post this in hopes that someone else will find value in it.

Cheers,

Justin