10/07
2010

Let’s say you were coding up program that required a number of base 24, how would you go about it? Think now of how you would do it in your own programming language. No really… think about it and think about the resulting type. You’re returning a string right? Depending on your programming language, you might have gone for the approach of assuming a base 10 input, then using division and the remainder operator to go through a dictionary of strings a,b,c,A,B,C. The type of the result is a string which is cumbersome to work with. For example, how would you add 1 to it? I’m guessing you’d have to convert it back to base 10, add one, then reconvert. In my opinion that is a lot of (too much) hassle.

Well how about we think a little differently…

Read More >>

30/03
2010

Imagine you are creating a page that you want in place of the default.aspx homepage that resides at the heart and soul of every Site in SharePoint.  Here’s how you do it:

//pageLayout = a page layout
//pubWeb = PublishingWeb

PublishingPage publishingPage = pubWeb.AddPublishingPage("http://sharepointtest/justanothersite/Pages/new-default.aspx", pageLayout);
publishingPage.Update();

pubWeb.DefaultPage = publishingPage.ListItem.File;
pubWeb.Update();

const string checkInComment = "Initial Check In";
publishingPage.CheckIn(checkInComment);
SPFile pageFile = publishingPage.ListItem.File;
pageFile.Publish(checkInComment);
pageFile.Approve(checkInComment);

pageFile.MoveTo(file.ListItemAllFields.ParentList.RootFolder.Url + "/default.aspx", true);
pageFile.Update();
}

The new publishing page is added to the web with the temporary url of “new-default.aspx”. Next we Update() the publishingPage to commit the add.

As we are adding a homepage, we set the DefaultPage property of the publishing web to the listitem file of the publishing page.  Remember here we have to Update() the publishing web or our change will not be reflected.

Check in and approve the file (Only appropriate if you have some kind of approval workflow set up on the site)

As the new publishing page we’re adding is the homepage, we use the MoveTo() method on the file to rename the file to default.aspx while passing the boolean parameter ‘true’ to make sure it overwrites the old ‘default.aspx’ if it is there.

29/03
2010

The new microsoft office ribbon hides many things, here’s one that stumped me and even my attempts to google an answer failed!

If you’ve accidentally hidden the toolbox in SharePoint Designer 2010 or it’s not showing, here’s how to get it back:

  1. Go into page design view (edit a master page or page layout) and click on the ‘Insert’ tab on the ribbon.
  2. Go to the SharePoint drop down.
  3. At the very bottom you should see an option that says ‘Show Toolbox…’. Click it and the toolbox will reappear as if by magic!

25/03
2010

So there I was, whittering away my time on facebook when I noticed I could resize the textbox of which I was writing my useless comment.  My immediate thought was that facebook had opened up the ability to resize, but nay!  In the nightly build of firefox 3.7 (minefield) you now have the ability to resize textareas! Sweet!  I’ve always admired it in google chrome and now us firefox lovers have it too!

In the bottom right, you see it. The 6 dots of resizeable glory

I’m running build number 20100324040325 but this might have been available before now

If you’re running the latest version of firefox, check it out here

11/03
2010

In a recent project, I’ve begun to heavily use regular expressions to capture the data from a looong string.  This post will demonstrate how I used ExplicitCapture to make it really easy to use the captured data later on using the Match object.

The data is in the following format:

[Data I do not care about...]<textarea1><![CDATA[blahhhhhhh]]>
</textarea1> <textarea2 xmlns=""><![CDATA[bada bing bada boom]]>
</textarea2>[More data I do not care about]

I need to capture all the data inside those CDATA areas, and also the number of the textarea.  Even though the content of the CDATA may be HTML and therefore not a regular language, I am still using a regular expression here as the content outside of the textareas is regular.  If you need to extract data from HTML, I’d recommend using HTML Agility Pack.

Regex pattern = new Regex("<textarea(?<textareaid>\\d+)( xmlns=\"\"|)><!\\[CDATA\\[(?<content>.*?)\\]\\]></textarea\\d+>", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.ExplicitCapture);

Here I’m storing my regular expression in to a variable called pattern.  The expression is a little more complex than expected due to me having to escape my backslashes when I do actually want them represented in the regex pattern.  The interesting parts are the RegexOptions in the second parameter of the Regex constructor.

Of course I’m using ignore case as there may be instances where the case is different.

I’m using single line as I don’t want to take in to account line breaks, nor am I using ^ or $ to indicate the start or end of lines respectively.  This option treats the string as one long unbroken line with line break characters and so enables a kind of dotall functionality.

The final option I’m using is explicit capture. This allows me to specify which capture groups I’m actually interested in and giving them a name.  In the above example, you can see I capture the textarea id number by writing (?<textareaid>\\d+).  This matches one or more digits and calls it “textareaid”. Awesome.

Now to use that later on I use the following code:

MatchCollection matches = pattern.Matches(content);
string convertedContent = String.Empty;
foreach (Match match in matches)
{
    convertedContent += string.Format("<div id=\"textarea{0}\">{1}</div>", match.Groups["textareaid"].Value, match.Groups["content"].Value);
}

The string convertedContent will now contain as many divs as there are textareas and will contain the content held within the CDATA.  Using match.Groups["whatever"] is a really easy way to get at the match values.