I learned something today while trying to add Top Navigation Bar navigation nodes in a SharePoint feature receiver. If you’re adding a node and also wanting to add child nodes of that node at the same time, make sure to actually add the parent node to the TopNavigationBar before trying to add the child nodes to the parent node.

In other words, this won’t work:

SPNavigationNode portalNode = new SPNavigationNode(“My Portal”, “/portal/Default.aspx”, false);

portalNode.Children.AddAsFirst(new SPNavigationNode(“Home”, “/portal/Home.aspx”, false));
portalNode.Children.AddAsLast(new SPNavigationNode(“Projects”, “/portal/Projects.aspx”, false));
portalNode.Children.AddAsLast(new SPNavigationNode(“HR/Benefits”, “/portal/HRBenefits.aspx”, false));
portalNode.Children.AddAsLast(new SPNavigationNode(“Management”, “/portal/Management.aspx”, false));

site.Navigation.TopNavigationBar.AddAsFirst(portalNode);

You have to do this:

SPNavigationNode portalNode = new SPNavigationNode(“My Portal”, “/portal/Default.aspx”, false);
site.Navigation.TopNavigationBar.AddAsFirst(portalNode);

portalNode.Children.AddAsFirst(new SPNavigationNode(“Home”, “/portal/Home.aspx”, false));
portalNode.Children.AddAsLast(new SPNavigationNode(“Projects”, “/portal/Projects.aspx”, false));
portalNode.Children.AddAsLast(new SPNavigationNode(“HR/Benefits”, “/portal/HRBenefits.aspx”, false));
portalNode.Children.AddAsLast(new SPNavigationNode(“Management”, “/portal/Management.aspx”, false));

If you do it the first way, the Navigation property of portalNode.Children will be null and it will cause an ArgumentNullException to be thrown. At least, I believe this was the cause of the problem. Adding the parent node (portalNode in my example) to the TopNavigationBar first causes portalNode.Children.Navigation to have a value and therefore no exception is thrown.

November 18th, 2009 | Tags: , , , ,

I was getting an error today while working on my company’s custom SharePoint theme on the webpart pages when in edit mode. In IE, you can move the webparts round between the different regions on the page by dragging the webpart headers around. To facilitate this, at some point along the line SharePoint calculates the real offset of the element using the MSOLayout_GetRealOffset javascript function.

For some reason, this function was failing and so the dragging and dropping was not working. To find the real offset, the MSOLayout_GetRealOffset function recursively travels up the DOM tree through each successive element’s offsetParent from the given element until either the given end element or the body element. Here’s the function:

function MSOLayout_GetRealOffset(StartingObject,OffsetType, EndParent)
{
var realValue=0;
if(!EndParent) EndParent=document.body;
for (var currentObject=StartingObject; currentObject !=EndParent && currentObject !=document.body; currentObject=currentObject.offsetParent)
{
realValue+=eval(‘currentObject.offset’+OffsetType)
}
return realValue;
}

As you can see, the end condition for the for loop is when it hits the end element or the body. In my case, there was no given end element, so the default body element was being used. The problem, I found out, was that the body element was getting skipped. When it got to the form element, the next offsetParent would be the html element, so the for loop would not end correctly and eventually currentObject would be null and the eval statement would fail. Eventually, I tracked the problem down to the form element being relatively positioned in our custom CSS. This, somehow, was causing the next offsetParent for the form to NOT be the body tag (even though the form element is a child of the body tag) and instead the html tag.

To solve the problem, I am overriding MSOLayout_GetRealOffset in Javascript with a slightly modified version:

MSOLayout_GetRealOffset = function (StartingObject, OffsetType, EndParent)
{
var realValue = 0;

if(!EndParent) EndParent = document.body;

for (var currentObject = StartingObject;
currentObject !=EndParent && currentObject != document.body && currentObject != null;
currentObject = currentObject.offsetParent)
{
realValue+=eval(‘currentObject.offset’+OffsetType)
}

return realValue;
};

The only difference is the check for currentObject being null.

November 16th, 2009 | Tags: , ,

I found a nice little online tool today to keep track of code snippets for future reference (which was sort of the original intent of this blog).

It’s called CodeKeep. You can add public and private code snippets that you want to reference in the future. You can also search for code snippets from other developers by language or author. It’s pretty nifty.

Make sure to check out the Visual Studio add-ins as well for direct access to your snippets (and others) from inside Visual Studio.

I added a link to my code snippets (which are few as of the writing of this post) on the right-hand side there.

September 24th, 2009 | Tags: , , , ,

I try to avoid setting styles in my code as much as possible.  However, I ran across a case using a DevExpress PageControl control where there was no possible way to set a certain border by using CSS.  This is because DevExpress wisely (read sarcasm) uses “border-color: #whatever !important” in the element’s style attribute.  So, there is no way to override that style except by setting it on the control in code (or your aspx page).

The particular border I needed is the ContentStyle.Border border of the ASPxPageControl and I needed to set the BorderColor property which is a System.Drawing.Color object.  There isn’t any straightforward way to get a Color object from a hexadecimal color using the Color object itself.  You have to use the ColorTranslator (also in the System.Drawing namespace) which gives you a FromHtml method and returns the correct Color object.

Color hexColor = ColorTranslator.FromHtml(“#666666″);

Thanks to this guy for the quick answer: http://www.akxl.net/labs/articles/converting-a-hexadecimal-color-to-a-system.drawing.color-object/

September 1st, 2009 | Tags: , , , , ,

I’m not very familiar (which I am actually very happy for!) with VB.NET and so I had a hard time figuring out how to trim newline characters from a string in a Reporting Services report I was working on.

Turns out none of the “regular” methods really work.  The Trim function only removes spaces and anything like Replace(“string”, “\n”, “”) didn’t work.

You have to use the Chr(13) or Chr(10) functions to actually get the character values for the newline characters.  So, my solution looked like this:

Replace(Replace(stringVariable, Chr(10), “”), Chr(13), “”)

Thanks to this forum post for providing the clues: http://www.daniweb.com/forums/thread54797.html

July 14th, 2009 | Tags: , , , ,

If you ever need to search a configuration file (or any other kind of file for that matter) for a parameter in order to replace the value, you can use `sed`.  For example, if there was an entry of

Host=a.b.c.d

But you want to replace it with:

Host=a.b.c.e

Instead of opening the file, finding the parameter, changing it, and save and exiting, you can use sed to do the work for you:

echo ‘Host=a.b.c.d’ | sed -e ’s/^Host=.*/Host=a.b.c.e/’

That is the basic statement, but it can be modified to search and replace in a text file (let’s say it’s called ‘file.txt’):

sed -e ’s/^Host=.*/Host=a.b.c.e/’ file.txt > /tmp/file.txt && mv /tmp/file.txt file.txt

Now, what if you want to keep the current value of Host and append more values to it so it will read:

Host=a.b.c.d,a.b.c.e

Just use this sed statement:

sed ’s/^Host=.*$/&,a.b.c.e/’

July 8th, 2009 | Tags: , ,

I was on a quest to find a way to test the length of ssh public keys to ensure they all meet our requirements.  Because we have several users with keys, I needed to write a script to make things far quicker.  After searching google and manpages, I found a combination of `ssh-keygen` and `awk` fit the bill.

The contents of one authorized_keys2 file with two public keys for two users looks like:

ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA4zink1RnYErVk2M6CWqJxmanplyvMyFKWOkECk50IeUUR5zV6zrZMznVOhRKEa69fwLGoDSRelipiuh+55ntaO0p3c2WrDYZamla5qCcmgvG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h-keygen`tool in order to get the key length in bits and then use `awk` for pretty output:

$ while read i; do echo $i > /tmp/check; echo $i | awk ‘{printf $3 “: “}’ && ssh-keygen -l -f /tmp/check | awk ‘{print $1}’; rm /tmp/check; done < ~/.ssh/authorized_keys2

User1: 2048
User2: 1024

So, as you can see, User1 has a 2048-bit key and User2 has a 1024-bit key.

I was looking for a way to find all the foreign keys to a primary key in a certain table in a certain database today at work.  A co-worker of mine found this link:

http://sqlserver2000.databases.aspfaq.com/schema-how-do-i-find-all-the-foreign-keys-in-a-database.html

It gives a script to use on the database that finds all the primary/foreign key relationships in the database.  You can limit it to just a particular table in the WHERE clause.

This is very useful!

May 27th, 2009 | Tags: , , , , , , ,

I’ve run across the problem every so often where I need to delete a lot of folders at various directory depths on my OS X system. For example, I recently wanted to quickly clear out all my “.svn” folders in a working copy I had. Well, you can’t search hidden folders with Spotlight, so it wasn’t immediately apparent what I should do.

I eventually found this nice solution from a combination of information online, using the command line, of course:

find ./ -name “.svn” | xargs -I {} rm -R “{}”

The “find” command gets all the right folders (good idea to run that alone first to make sure it’s picking up everything you want and ONLY what you want!) and then passes the results to “xargs” which splits its input into multiple arguments by spaces and newlines and passes them as arguments to the given command. In this case, we’re passing them to “rm” which then removes all the “.svn” folders.

TOP