Fluent in Code http://www.fluentincode.com functionally literate. posterous.com Mon, 15 Aug 2011 07:46:00 -0700 Updating SharePoint 2010 site properties http://www.fluentincode.com/updating-sharepoint-2010-site-properties http://www.fluentincode.com/updating-sharepoint-2010-site-properties

Recently I helped convert a SharePoint farm to the 2010 version from 2007. The SharePoint user interface has substantial changes between the two versions, so to make it easier on the end users, Microsoft allows sites to keep the 2007 look and feel. Once you migrate a farm with sites that are in the 2007 look and feel, you can upgrade the sites to the 2010 look and feel at your convenience, when the end user is well prepared. So that's what we did, and now I'm converting a small number of sites to the 2010 look and feel every day.

Microsoft makes it possible to convert sites to the new look and feel through the web browser, but it involves a lot of clicks and is prone to errors. So I wrote a PowerShell script to change the UIVersion property on a site. I've been running this script, converting sites, with no problems. Then today I found out that the script hasn't been fully converting sites to the 2010 look and feel, it's been converting them to the 2010 preview, which includes a yellow reminder bar in the settings pages to fully convert the site to 2010. It turns out you need to set UIVersionConfigurationEnabled = $false in addition to UIVersion = 4 to not convert to the preview mode.

So here's my quick and dirty one-liner to loop through all the site collections, then loop through all the sites in each site collection and fixes any site with UIVersion of 4 and UIVersionConfigurationEnabled = $true. I've added line breaks to my script to make it more readable, but when I was writing it I just typed this as one line in the SharePoint 2010 PowerShell Management Shell.

I love the pipeline concept in PowerShell. It's a very functional concept - each stage of the pipeline gets a collection of objects passed to it, operates on those objects, and passes the results to the next stage in the pipeline. So the script starts by gathering all the site collections, then it uses those to find all the sites, and then it uses the Where-Object cmdlet to filter out only the matching sites. The last stage of my pipeline is where PowerShell breaks the functional model, because the Foreach-Object cmdlet directly updates the sites, but breaking the functional model here makes writing PowerShell scripts so much easier. If I had to create a copy of the sites and then send that copy to a IO function, that would add unnecessary complexity to what is a very useful language for quickly modifying collections of things.

Get-SPSite -limit ALL | 
        Get-SPWeb -Identity $_ -limit ALL | 
            Where-Object { 
                $_.UIVersion -eq 4 -and 
                $_.UIVersionConfigurationEnabled -eq $true 
            } | 
                Foreach-Object { 
                    $_.UIVersionConfigurationEnabled = $false; 
                    $_.update() 
                }

Permalink | Leave a comment  »

]]>
http://posterous.com/images/profile/missing-user-75.png http://posterous.com/users/heOPIkJ5gUDTY Steven Tomcavage fluentincode Steven Tomcavage
Thu, 11 Aug 2011 09:29:36 -0700 PHASE meeting http://www.fluentincode.com/phase-meeting http://www.fluentincode.com/phase-meeting

Last night I attended the Philadelphia Area Scala Enthusiasts (PHASE) monthly meeting. The meeting was devoted to disruptors, which is a programming pattern used by LMAX for high-performance trading software. LMAX wrote their version in Java and open-sourced the pattern, and last night's talk was 80% introduction to the pattern and 20% looking into Jamie Allen's Scala implementation of disruptors.

I was amazed that there are people out there trying to get high performance from Java. They are writing with the computer architecture in mind and doing tricks like padding a variable with 7 longs on each side to ensure that it's the only variable on a cache line. That is, anytime the CPU pulls data from a cache, it gets a chunk of bits from memory (either from registers, one of the caches on the processor, the cache outside the processor, or from RAM). The chunk of bits the CPU retrieves is always the same size, and the idea behind the disruptor is that ensuring that the chunk will only ever contain one variable will prevent the contention for resources that might arise if two variables on the same cache line were needed by different CPU cores simultaneously.

This is just one of the ideas implemented by LMAX in their disruptors pattern. The whole things is very slick and looks like Java that you would never write in production, but they did it because it runs very fast on their architecture. This is code that's usually reserved for C or assembly, a low-level language where you can directly address memory. The fact that LMAX is mixing low-level memory manipulation with a high-level language like Java is mind blowing. I've always thought of Java as too slow for high performance computing, let alone to high-level for memory manipulation. But LMAX has shown me otherwise.

The Scala implementation is still a work in progress, so there wasn't much talk of it other than showing a couple lines of code and pointing to the Github repository. The ideas presented in the meeting were fascinating, and they were a good reminder that even though I don't need to care about 100-ns versus 10-ns access times, it's always good to write software that responds rapidly.

Permalink | Leave a comment  »

]]>
http://posterous.com/images/profile/missing-user-75.png http://posterous.com/users/heOPIkJ5gUDTY Steven Tomcavage fluentincode Steven Tomcavage
Mon, 08 Aug 2011 09:02:00 -0700 First contribution to Scala http://www.fluentincode.com/first-contribution-to-scala http://www.fluentincode.com/first-contribution-to-scala

I know it's small fries in the Open Source world, but I recently had some code accepted into the main Scala tree. You can even see it over at Github. I'm trying to get more involved in the Open Source world, and getting over the hurdles of working with Git and having code accepted into a project feels like a major accomplishment. Now that I have a handle on the process of getting changes into the main tree, maybe I can take the next step of making more complicated fixes.

 

Permalink | Leave a comment  »

]]>
http://posterous.com/images/profile/missing-user-75.png http://posterous.com/users/heOPIkJ5gUDTY Steven Tomcavage fluentincode Steven Tomcavage
Mon, 08 Aug 2011 08:36:00 -0700 Resetting default Reusable Content in SharePoint 2010 http://www.fluentincode.com/resetting-default-reusable-content-in-sharepo http://www.fluentincode.com/resetting-default-reusable-content-in-sharepo

SharePoint 2010 comes with default bits of code that the end user can snap into any page they are editing. The default items are Byline, Copyright, and Quote and they are accessible through the Insert set of commands on the Ribbon when editing a page. The default items all live in a list named "Reusable Content" and each site collection has its own reusable content list. So, that means on the web application with 128 site collections which I upgraded from SharePoint 2007 to SharePoint 2010, there are 128 instances of the resuable content list, all with defaults that don't make any sense in my organization.

So of course, I found a way to code around the problem. Below is my PowerShell script which loops through all the site collections, finds the "Reusable Content" list in the root site of each site collection, and then changes the default items in the reusable content list. I left the Quote default as-is, but removed the Copyright item since we don't use copyrights on individual pages, and I changed the byline to something which hopefully will trigger the user to remember to change it to their name. Items in the reusable content list can contain HTML code, but it has to be in the "Reusable HTML" column of the list.

One last note: because SharePoint lists are immutable, deleting an item from them changes the list size as soon as the delete call is complete. Because of this, I couldn't send the reusable content list through the ForEach-Object function to modify the list because ForEach-Object breaks if the list its operating on changes underneath it. To get around that, I just used an old-fashioned for loop like you'd see in C, Java, etc.

#------------------------------------------------------------------------------#
#       Program: Update-ReusableContent.ps1
#        Author: Steven Tomcavage
#          Date: August, 2011
#   Description: Globally changes the default reusable content, which is content 
#                that can be inserted from the edit page's Ribbon in SharePoint
#------------------------------------------------------------------------------#

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue"

#----------------------------------------------------------------------------
function Fix-ReusableContent {
#----------------------------------------------------------------------------
    param ([object]$list)
    
    $listItems = $list.Items
    $numListItems = $listItems.Count
    
    # Deleting a list item changes the list data structure, so it has to
    # be done inside of a loop which counts down
    for ($i = $numListItems - 1; $i -ge 0; $i--) {
        $item = $listItems[$i]
        switch ($item.Title) {
            Byline { 
                $item["Reusable HTML"] = "<strong>By <em>&quot;Author Name&quot;</em>"
                $item.Update() 
                Write-Host "Updated Byline item in " ($list.ParentWeb).Title
                break 
            }
            Copyright { 
                $item.Delete()
                Write-Host "Deleted Copyright item in " ($list.ParentWeb).Title
                break 
            }
            default { 
                # do nothing
                break 
            }
        }       
    }
}

#----------------------------------------------------------------------------
# MAIN SCRIPT STARTS HERE
#----------------------------------------------------------------------------

# Each line this this set of commands get the result set from the previous line
Get-SPSite -Limit ALL | 
ForEach-Object { ($_.rootWeb).Lists["Reusable Content"] } | 
ForEach-Object { Fix-ReusableContent($_) }

Permalink | Leave a comment  »

]]>
http://posterous.com/images/profile/missing-user-75.png http://posterous.com/users/heOPIkJ5gUDTY Steven Tomcavage fluentincode Steven Tomcavage
Thu, 21 Jul 2011 09:39:00 -0700 JavaScript and Microsoft: So Happy Together http://www.fluentincode.com/javascript-and-microsoft-so-happy-together http://www.fluentincode.com/javascript-and-microsoft-so-happy-together

Last night I attended the July meeting of philly.NET, the local Microsoft User's Group. First of all, I have to say how impressed I was by the turn-out. I'm a semi-regular attender of Philly CFUG (a ColdFusion user group), PHASE (a Scala user group), and Philly Lambda (a functional programing user group), but I've never seen more than 20 people at any of those meetings, and often much less, but last night 100 people turned out for philly.Net. And philly.NET delivered.

The first presentation was by Amanda Silver, who is the Principal Program Manager for the JavaScript team at Microsoft. In the past, Microsoft has understandably been less-than-willing to fully support JavaScript, since it was a competitor with their own web scripting language, VBScript. Amanda came very close to owning up to that in her presentation last night. Granted, most of her 10 years at Microsoft has been spent working on the Visual Basic language, and she only recently moved over to the JavaScript team, so she really didn't have anything to do with Microsoft's JavaScript support in the past. 

With the development of Internet Explorer 9, Amanda has been leading the effort to re-write Microsoft's JavaScript engine. They took a look at how many CPUs the average IE user has and found out it's over 2.4. So their new JavaScript engine was written to take advantage of two cores, where in the past their engine ran on a single core. I've heard quite a bit of talk in the past about moving to multi-core programming, but this is the first I've heard a widespread practical application of it.

The new engine runs a JavaScript interpreter on the first core, which in addition to interpreting and running the JavaScript on the page, analyzes the JS code for large functions, functions with a lot of external references to them, functions with a large number of iterations, and other sections that can be highly optimized by a compiler. The first core then sends those sections off to the compiler, which lives on the second core and which creates a highly optimized byte code that it then sends that back to the first core to run. The second core is also responsible for garbage collection of unused objects created by the interpreter, so the first core is free to focus exclusively on interpreting and running the JavaScript. The idea is that offloading garbage collection and running compiled code when possible will make the JavaScript intrepreter much faster.

Another advance Microsoft has made is the use of deferred parsing. Since a lot of sites are sending the entire jQuery library down the line to your browser, but are only using bits and pieces of it, Microsoft's interpreter only runs a syntax check on the JS to make sure there are no obvious errors. It then parses functions and bits of code as they are called, so it doesn't waste time parsing, compiling, and optimizing code that's never used in the page. A great advance. Someone asked Amanda about handling common JS libraries, such as jQuery, where there's really no need to run a specific version through the interpreter and compiler more than once in the lifetime of the browser. She said Microsoft has plans for that type of long-term bytecode caching, but they haven't implemented it yet as of IE 9.

Finally, something that really doesn't affect me, Microsoft has removed the creation of COM objects from the JS engine. With their previous JS engine, COM objects were created with the thought that the JS engine would connect with other pieces of Microsoft code and COM is a good way to shuffle objects back and forth across applications. Now Microsoft has backed off of the idea of applications external to IE making use of JS objects, so they've removed the step of creating COM objects, which in turn speeds up the JS engine.

These are all very exciting developments from Microsoft, but I wonder if it's too little too late. Most geeks have migrated away from IE towards Firefox, Chrome, and Safari. I'd like to see these ideas find their way into those browsers. The great part about these developments from Microsoft is that I do foresee other browsers picking up these techniques, which is beneficial to all of us.

 

Permalink | Leave a comment  »

]]>
http://posterous.com/images/profile/missing-user-75.png http://posterous.com/users/heOPIkJ5gUDTY Steven Tomcavage fluentincode Steven Tomcavage
Sun, 17 Jul 2011 11:41:00 -0700 Attracting and retaining contributors in Open Source http://www.fluentincode.com/attracting-and-retaining-contributors-in-open http://www.fluentincode.com/attracting-and-retaining-contributors-in-open

I’m currently in the “Attracting and Retaining Contributors” presentation at Scalathon 2011. The talk is being given by Asheesh Laroia from OpenHatch. Here’s my summary of his presentation:

  • There are a lot of people who want to contribute to Open Source software, but most projects have 1 person committing to the codebase
  • Fedora’s design team’s goal is to attract at least one contributor every 3 months; they achieved this by creating design contents where the prize is being granted contributor access to the project
  • The best way to get new contributors is to help new users who show up on email lists; this creates a relationship which can be extended into them being contributors
  • Pointing new users to online documentation or to lists of open bugs is not effective
  • Asking people at conferences to join your project is very effective; the human contact is much more effective than the online contact; for example, instead of telling someone how to file a bug, tell them they will receive a postcard if they file a bug
  • Host “Build It” events online where you help people through the product compilation phase or through finding, submitting, and fixing bugs
  • Once you have people on your project, maintain the human contact with them by acknowledging their specific efforts
  • Answer emails within 4 days, even if it’s just to say I’m too busy to review your fix/package right now, but I wanted to let you know I received your email; this may encourage others in community to pick up the slack

Permalink | Leave a comment  »

]]>
http://posterous.com/images/profile/missing-user-75.png http://posterous.com/users/heOPIkJ5gUDTY Steven Tomcavage fluentincode Steven Tomcavage
Sun, 17 Jul 2011 08:41:00 -0700 Functional Programming 101 http://www.fluentincode.com/functional-programming-101 http://www.fluentincode.com/functional-programming-101

I'm in the Functional Programming 101 session at Scalathon. I've been doing functional programming for about a year, but it's nice to get a refresher. Something interesting which I've forgotten is that writing to the screen is a side-effect, and a purely functional language eschews side-effects. This is why Haskell has the monad concept. I've been guilty of putting plenty of debug statements in my functions as well as outputting from functions instead of outputting after the value has been returned from the function. My half-New-Year's-Resolution (this being July) is to try to write more purely functional functions.

According to the presenter, to do purely functional IO, assume that an IO function, myIO, accepts "The Universe" as a state and then get values from the universe (to read input) or create a new universe with our new value (to output) and then return the Unit value back from the IO function. Then, have another function which calls unsafePerformIO(myIO), which use the IO function as its input value. Finally, call the unsafePerformIO(myIO) as the final call of the program. In the case of web-servers, this means that most of the code will be in the IO function and the main program will just be a single call to unsafePerformIO(myIO). This seems like a bit of hand-waving, but maybe it will make more sense over time.

As a side note, this session is quite crowded, so I wonder if that's indicative of how many Scala programmers are writing with an object oriented style versus how many are writing in a purely functional style. If Scala is a good language to learn how to transition from OO to functional, how long will that transition take for the larger programming community to make? And will it be made if there's the fall-back of OO style, which seems much easier to grasp after 30 years of widespread OO programming?

Permalink | Leave a comment  »

]]>
http://posterous.com/images/profile/missing-user-75.png http://posterous.com/users/heOPIkJ5gUDTY Steven Tomcavage fluentincode Steven Tomcavage
Sun, 17 Jul 2011 08:05:00 -0700 Resources from Scalathon 2011 http://www.fluentincode.com/resources-from-scalathon-2011 http://www.fluentincode.com/resources-from-scalathon-2011

I’m posting this from Scalathon 2011, a Scala hackathon at the University of Pennsylvania. There are a lot of great resources being mentioned and I just wanted to grab them all and save them for future reference. Here goes:

Possibly more links as the day continues!

Permalink | Leave a comment  »

]]>
http://posterous.com/images/profile/missing-user-75.png http://posterous.com/users/heOPIkJ5gUDTY Steven Tomcavage fluentincode Steven Tomcavage
Fri, 08 Jul 2011 06:43:00 -0700 SharePoint Developer Dashboard script http://www.fluentincode.com/sharepoint-developer-dashboard-script http://www.fluentincode.com/sharepoint-developer-dashboard-script

I’ve been working quite a bit with SharePoint 2010 recently. SharePoint has a very useful feature called the Developer Dashboard, which shows you want happens when you load a SharePoint page, but Developer Dashboard is not enabled by default, so I wrote this short PowerShell script to globally enable or disable it:

#------------------------------------------------------------------------------#
#      Program: setDebugging.ps1
#         Author: Steven Tomcavage
#            Date: June, 2011
#   Description: Turn the debugging on or off for a SharePoint Web
#------------------------------------------------------------------------------#

param($level = $(throw "Level is required.  Level is On, Off or OnDemand"))

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue"

$dashboard = [Microsoft.SharePoint.Administration.SPWebService]::ContentService.DeveloperDashboardSettings;
$dashboard.DisplayLevel = ([Enum]::Parse([Microsoft.SharePoint.Administration.SPDeveloperDashboardLevel], $level));
$dashboard.TraceEnabled = $true;
$dashboard.Update();

Write-Host ("Developer Dashboard Level: " + $dashboard.DisplayLevel)

Permalink | Leave a comment  »

]]>
http://posterous.com/images/profile/missing-user-75.png http://posterous.com/users/heOPIkJ5gUDTY Steven Tomcavage fluentincode Steven Tomcavage
Wed, 20 Apr 2011 12:25:00 -0700 Getting Started http://www.fluentincode.com/getting-started-0 http://www.fluentincode.com/getting-started-0

I’m just getting started with blogging in Posterous, so as a way of testing out the posting methods and HTML markup, here’s a little piece of Clojure code that I wrote to solve problem 12 over at http://projecteuler.net:

(ns euler12
  (require [clojure.contrib.math :as math]))

(defn divisor-count
  "Gets a count of all the divisors of a number, including the number itself"
  [n]
  (* 2 (count (filter #(zero? (mod n %)) (range 1 (math/ceil (math/sqrt n)))))))

(defn euler12
  "Finds the first triangular number with over 500 divisors"
  []
  (letfn
    [(test-tri-num [n tri-num]
       (cond
         (> (divisor-count tri-num) 500) tri-num
         :else (recur (inc n) (+ (inc n) tri-num))))]
    (test-tri-num 1 1)))

Permalink | Leave a comment  »

]]>
http://posterous.com/images/profile/missing-user-75.png http://posterous.com/users/heOPIkJ5gUDTY Steven Tomcavage fluentincode Steven Tomcavage