23 July, 2014

Brushing up on PowerShell

Over the last couple of days, I have been working with PowerShell on a project. My usage of PowerShell in the past has been mostly for one off tasks or for a tasks that I need to get done really quickly so I can move on to the next. My current project caused me to think about PowerShell as a tool for creating re-usable scripts.

To load additional modules and snap-ins use the commands:
 Import-Module  
and
 Add-PSSnapIn  
.
If you try to load the same snap-in more in the same session an error occurs. One way around is to ignore the error.
 Add-PSSnapin -Name "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue  

Great care must be taken when using variables. A simple typo can lead to a lot of confusion. It is also worthwhile to read up on variable scope.
 help about_scope  

PowerShell allows you to create methods or functions within a script. The basic syntax is
 function  { script block }  

A quick example of creating a function to add two numbers together and calling the function.
 function Add ([int] $x, [int] $y) {   
   $results = $x + $y;  
   Write-Host "Results $results";  
 }  
 Add 3 4  

In the above example, I am doing a few small things that are more personal preference than anything else. There are multiple ways to create parameters. I like to create parameters on the same line as the function name. I also like to add the datatypes. This just gives a me a bit of clue as to what the function would like to have for data. I am also using semi-colons at the end of each line. This comes from my history of working with C, C++, Java, JavaScript, and C#.

You will notice in calling the function, "Add 3 4", parameter are separated by spaces.

Single line comments start with the # sign.
 #A simple one line comment  

Block comments are encased in <# #>
 <#  
 This is a multiple line comment  
 or block comment  
 #>  

Accessing the help system within PowerShell is straight-forward. There are multiple key words that can be used.
 Get-Help  
,
 Help  
, or
 Man  

Calling the help system on the cmdlet Get-Content.
 man Get-Content  

The help system also comes with a set of switches for showing the full listing.
 man Get-Content -full  




18 July, 2014

C++ header file location

This morning I was working a tutorial on openFrameworks. The tutorial called for the creation of a new class. I was a bit careless in the creation of the files. The files were created outside of the src folder. I used Visual Studio 2012 (VS) to move the files to correct location. When I went to use the new class in the code, VS could not find the files. VS did not move the file on the hard drive. I had to manually move the files myself.

Today's lesson is that I must be careful when creating new class files (.h and .cpp) to make sure there are created in the correct location. The other lesson is when moving source files around it is worth checking to make sure the files are moved on the hard drive.


17 July, 2014

openFrameworks - Notes on Getting Started

The other day, I learned of the existence of openFrameworks. According to the openFrameworks website, openFramework is a "toolkit designed to assist the creative process by providing a simple and intuitive framework for experimentation." openFrameworks runs on multiple platforms (Windows, Mac OS X, Linux, iOS, and Android).

I am learning and work with openFrameworks for a few reasons. I would like to increase my knowledge of C++. I am hoping that the libraries that are included with openFrameworks will take some of the pain out of learning C++. My next reason for learning openFrameworks is gain experience outside of business programming. I make my living by writing code for businesses.

Steps

1.) Download the framework (v0.8.3) - http://openframeworks.cc/download/.
2.) Unzip the files. I choose to put the files at c:\openFrameworks.
3.) Create the first solution.
3a.) Run projectGenerator.exe
3b.) Change the value of Name field
3c.) Accept the default path. Older versions of the openFramework relied on relative paths to find libraries. I choose not to risk change the location for my first project.
3d.) Leave the Addon alone for now. I am looking forward to playing and experimenting in the near future.
3e.) Click 'Generate Project'. Watch the screen carefully. The process runs very quickly and it is easy to miss the done message.
4.) Open the project with Visual Studio 2012. I learned this the hard way. I was unsuccessful with building the project in VS2013. Additional research is needed to figure out why.
5.) Run the project. The first build takes a while to compile all the parts. Once it is done, you should see a gray window.

The next step is now to start working my way through the tutorials.

17 April, 2014

Keeping an Eye on Rosyln

Over the past few days there has been a bit of news about Roslyn. I am working on getting my head around what is Roslyn and why do I care?

What is Roslyn?

"The .NET Compiler Platform ("Roslyn") provides open-source C# and Visual Basic compilers with rich code analysis APIs. You can build code analysis tools with the same APIs that Microsoft is using to implement Visual Studio!" - http://msdn.microsoft.com/en-us/roslyn

The way I am understanding this is Roslyn allows the C# code to help write C# code. A few others has commented that Roslyn would allow for the scripting of code plus the ability to compile/execute that code on the fly. 

Should I care about Roslyn?

The short answer is yes. 

Roslyn holds the promise of changing the way code is written, the way code is refactored, and the way code is analysed. 

It seems to me that the inline declaration expressions will take a little getting use to and will help produce more readable code.

I am rather excited to see what new features and tools people will develop to help with refactoring code. The example, I see the most showing off Roslyn's refactoring capabilities is the inline rename. 

It seems to me that Roslyn can help with Aspect Oriented Programming (AOP). In the AOP world, logging is the use case people point to for the need of AOP. Logging is common utility that is needed through out a program. It can get rather ugly and boring to write a logging statement in each method. It would be great to have a tool, script or the compiler automatically add logging to each method and to not clutter up the code with logging statements.

References


04 April, 2014

I was struck by the blog post 'The 30 second habit with lifelong impact.' The heart of the article is about taking 30 seconds after an important meeting, lecture, or experience to write the most important points.

My typical day includes several meetings, reading articles, writing code, learning new technologies, and increasing my knowledge of technologies. I encounter a lot of information each day. This idea of taking a few seconds to write down key points seems like it would be a valuable way to sort through all the information I encounter.

I have been keeping a journal. I really should rephrase, every few days I write something in what I am calling a journal. I don't have a good habit of writing each day. Taking a few seconds at the end of the day to write down a key point or two about the day would help to give me something to write about in the journal.

25 March, 2014

jsFiddle - A helpful tool

While researching a problem, I came across a comment on a forum that talked about jsFiddle. jsFiddle has become on of my favorite go to tools for testing, experimenting, and learning web technologies like HTML, CSS and jQuery. 

jsFiddle uses a four box grid layout. Each box allows you to enter either HTML, CSS, code and to see the results. It was been a great tool for testing out snippets of HTML, CSS and JavaScript. The tool also allows the you to pick different libraries like jQuery. 

I have personally found this tool to be incredibly useful. I am now using this tool almost exclusively for my learning projects. I frequently use it to test JavaScript and jQuery code that I find in blog posts.  

Resources

20 March, 2014

Listing User Profile Accounts from SharePoint 2013

Today, I am doing a thought experiment / learning exercise. The goal is to come up with a few different ways to display data from SharePoint’s User Profile Service.

Thought A – Manage User Profiles within Central Admin.


From the Manage User Profile page within Central Administration, I can search for a user or a group of user. For example, if SharePoint is setup to use claim based identity then a search phrase “i:0” will give list of users.


While using this method is fast and easy trying to get a list of all the accounts or to create a file with all the accounts in it is difficult.

Thought B - PowerShell



With a simple PowerShell script, it is easy to get a list of all the user profiles. What is really nice about this method is it is very simple to pipe the results of the GetEnumerator method to filter the results. PowerShell has built-in functionality that makes creating a file with the account information in it a snap.


add-pssnapin "Microsoft.Sharepoint.Powershell"

$siteList = Get-SPSite 
$serviceContext = Get-SPServiceContext($siteList[0])
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext)

foreach($usrProfile in $profileManager.GetEnumerator()) {
    Write-Host $usrProfile.AccountName "|" $usrProfile.DisplayName
}

Thought C – Visual Studio


Referencing the correct libraries and little bit of code, it very simple to get back a list of all the accounts within User Profile. With a little bit of extra code searching the accounts, searching for a specific account or writing the accounts to a file is no problem.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System.Diagnostics;

namespace UserProfileList
{
    class Program
    {
        static void Main(string[] args)
        {
            var siteList = new SPSite("http://sp2013");
            SPServiceContext svcCtx = SPServiceContext.GetContext(siteList);
            var profileMgt = new UserProfileManager(svcCtx);
            var userProfiles = profileMgt.GetEnumerator();

            while (userProfiles.MoveNext())
            {
                UserProfile userProfile = (UserProfile)userProfiles.Current;
                
                Console.WriteLine(userProfile.AccountName);
            }

            Console.ReadLine();
     }
    }
}


Wrap Up

This was a fun experiment in figuring out different ways to do the same task. It showed me a bit about the effort it takes to get a result. Using the out of the box functionality got me to a result very fast and it should cover 80% of the needs around looking up a user or a small group of users. PowerShell makes it simple and easy to get a list of users. 



10 March, 2014

Using CoffeeScript within SharePoint 2013


The other day I was reading about CoffeeScript and I was rather intrigued by how neat and simple CoffeeScript makes writing JavaScript. Since CoffeeScipt compiles down to JavaScript than it should be rather easy to use CoffeeScript with SharePoint 2013.

I have a simple development environment setup. It contains three servers, one server is the domain controller, one server hosts SharePoint, and the last server is for the SQL Server. I am using Hyper-V to host these virtual servers. Visual Studio 2013 is installed on the SharePoint sever. To make working with HTML, CSS, JavaScript, TypeScript, CoffeeScript or LESS easier the Web Essentials extension was installed.

My plan is rather simple write a little HTML to created two boxes and when a user clicks on one of the boxes the box should change color.

I will start with setting up a new Visual Studio project using a Empty ASP.NET Web Application template in the Visual C# folder.


Next I will add an HTML Page to the project. The code is really straight-forward.

 <!DOCTYPE html>  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head>  
   <title>CoffeeScript Playground</title>  
 </head>  
 <body>  
   <h4>The Boxes </h4>   
   <div class="holder" style="width: 500px;">  
     <div id="box1" style="border:1px solid black; width:200px; height:200px; float: left; background-color:lightblue; ">Box 1</div>  
     <div id="box2" style="border:1px solid black; width:200px; height:200px; float: right; background-color: lightgreen;">Box 2</div>  
   </div>  
   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>  
   <script src=" mycoffeescript.js"></script>  
 </body>  
 </html>  

Now to add a CoffeeScript file to the project. Again the code is straight-forward.

 # CoffeeScript  
 $j = jQuery  
 box1 = $j '#box1'  
 box2 = $j '#box2'  
 $j ->  
   box1.click ->  
     color= box1.css('background-color')  
     if color == "rgb(173, 216, 230)"  
       box1.css('background-color', 'blue')  
     else  
       box1.css('background-color', 'lightblue')  
   box2.click ->  
     color= box2.css('background-color')  
     if color == "rgb(144, 238, 144)"  
       box2.css('background-color', 'green')  
     else  
       box2.css('background-color', 'lightgreen')  

Once I had everything working it was time to port it over to SharePoint. For this, I wanted a way to quickly setup and test. I began by adding a Content Editor part to a page.

 <h4>The Boxes </h4>   
 <div class="holder" style="width: 500px;">   
   <div id="box1" style="border: 1px solid black; width: 200px; height: 200px; float: left; background-color: lightblue;">Box 1</div>   
   <div id="box2" style="border: 1px solid black; width: 200px; height: 200px; float: right; background-color: lightgreen;">Box 2</div>   
 </div>  


My next step was to add a Script Editor part to the page. I simple copied the code JavaScript code from Visual Studio and dropped it in the part.

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>  
 <script type="text/javascript">  
 (function() {  
  var $j, box1, box2;  
  $j = jQuery;  
  box1 = $j('#box1');  
  box2 = $j('#box2');  
  $j(function() {  
   box1.click(function() {  
    var color;  
    color = box1.css('background-color');  
    if (color === "rgb(173, 216, 230)") {  
     return box1.css('background-color', 'blue');  
    } else {  
     return box1.css('background-color', 'lightblue');  
    }  
   });  
   return box2.click(function() {  
    var color;  
    color = box2.css('background-color');  
    if (color === "rgb(144, 238, 144)") {  
     return box2.css('background-color', 'green');  
    } else {  
     return box2.css('background-color', 'lightgreen');  
    }  
   });  
  });  
 }).call(this);  
 </script>  

After saving the page, I was ready to test.


It turns out that working with CoffeeScript and SharePoint is fairly straight-forward. It took a few tutorials to get an idea of the syntax and I had to work a few examples. As I wrote this blog post it dawned on me that if I used a different reference in the Script Editor, I could have used the CoffeeScript instead of the JavaScript. Change the Script Editor part to use the following:

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" ></script>  
 <script src='http://coffeescript.org/extras/coffee-script.js' type='text/javascript'></script>  
 <script type='text/coffeescript'>  
 $j = jQuery  
 box1 = $j '#box1'  
 box2 = $j '#box2'  
 $j ->  
   box1.click ->  
     color= box1.css('background-color')  
     if color == "rgb(173, 216, 230)"  
       box1.css('background-color', 'blue')  
     else  
       box1.css('background-color', 'lightblue')  
   box2.click ->  
     color= box2.css('background-color')  
     if color == "rgb(144, 238, 144)"  
       box2.css('background-color', 'green')  
     else  
       box2.css('background-color', 'lightgreen')  
 </script>  

Using the Content Editor and Script Editor web parts makes sense in a development or learning environment to explore a concept or test out an idea.

03 March, 2014

A speedy way to add JavaScript and jQuery to SharePoint 2013

The fastest way I know to add JavaScript and/or jQuery to the SharePoint site is through the use of the Script Editor web part.

JavaScript 

The steps

  1. Edit the page
  2. Add the Script Editor web part
  3. Add the Script
  4. Save your work
Here are a few screens shoots.

Added the Script Editor to the page

Added the JavaScript 

The Results


jQuery

Adding jQuery to a SharePoint site is just as simple.

The steps

  1. Edit the page
  2. Add the Script Editor web part
  3. Add a reference to jQuery
  4. Add the Script
  5. Save your work
Adding the Script Editor Part


Adding the reference to the jQuery package and the jQuery script
The Results
In this example, the code is making a reference to the jQuery's content delivery network (CDN) that contains the jQuery package we are interested in. In this case, I am making a reference to the 1.11.0 minified package. It is also acceptable to download the jQuery and load it into SharePoint Library like the 'Style Library'.

Words of Caution

Please remember that JavaScript and jQuery execute with the client's browser. It is possible that the script being added can interfere with the interface. It is highly advisable to test JavaScript and/or jQuery scripts in a development environment first.

This approach to adding JavaScript and jQuery is very fast. There is a downside to this approach. When using jQuery you need to reference the jQuery with each site and page. When it comes time to update the jQuery package to a newer version you will have to check each page and make the change. Another problem with this approach is it can lead to multiple versions and multiple references of the jQuery package. Having multiple reference to the same package within the same page can lead to performance issues because you are downloading the same information multiple times when the page gets called. Having references to multiple versions of the jQuery library leads to confusion and makes debugging harder.

Update - 7 March 2014

I encountered a strange behavior. My script would work when the page was in edit mode and not when the page is in it normal mode. It took a while to sort it out the issue. I needed to turn off the feature 'Minimal Download Strategy'.

24 February, 2014

Claims Encoding for SharePoint 2013 and 2010

On a recent SharePoint 2013 project a question came up "How do you read those funny looking user ids?" After a bit of research, the claims identity breaks down in the following format:
<IdentityClaim>:0<ClaimType><ClaimValueType><AuthMode>|<OriginalIssuer (optional)>|<ClaimValue>

How it breaks down

For the IdentityClaim acceptable values are:
  • "i" for identity claim
  • "c" for any other claim
For the ClaimValue acceptable values are:
  • “#” for a user logon name
  • “.” for  an anonymous user
  • “5” for an email address
  • “!” for an identity provider
  • “+” for a Group security identifier (SID)
  • “-“ for a role
  • “%” for a farm ID
  • “?” for a name identifier
  • "\" for a private personal identifier (PPID)
For the ClaimValueType acceptable values are:
  • “.” for a string
  • “+” for an RFC 822-formatted name
For the AuthMode acceptable values are:
  • “w” for Windows claims (no original issuer)
  • “s” for the local SharePoint security token service (STS) (no original issuer)
  • “t” for a trusted issuer
  • “m” for a membership issuer
  • “r” for a role provider issuer
  • “f” for forms-based authentication
  • “c” for a claim provider
The option field of OriginalIssuer tells us who the original issuer of claim is

The value for ClaimType is the the user id when the IdentityClaim is "i".

Example

i:0#.f|mymembershipprovider|wick

I am reading the above example as Identity Claim ("i") that contains the username ("#") of type string (".") with an authmode of Forms Based Authentication ("f"). The member provider being used is "mymembershipprovider" and the user id is "wick".

Resources / References

Challenging myself to learn something new

I have recently set a big challenge for myself. I want to know about Machine Learning . To add to the challenge, I am trying out usin...