What are you blogging today?

As you may have noticed I recently went through a steep learning curve on SharePoint. The best part was it turned out to be a liberation on my blogging mindset.

I started blogging back in 2003 after being inspired by Scoble and Winer. The type of blogging I was doing back then is now coined a link blog with a few of my own reflections thrown in.

If you’ve been subscribing to blogs you tend to come across posts that tell you how to write good posts, what you should have in your post and what not to post. Subconsciously these posts made me raise the bar higher and higher on the type of stuff I should be posting about.

Not anymore, bah humbug to all of those posts that tell you how to blog. I’ve since realised that every small thing I learn each day is going to help some poor soul in the future. I was that poor soul when I was (am) learning SharePoint and boy did those short tips from others really help out.

So remember no post is too small or irrelevant in the eyes of the beholder who searches for it in Google.

So show some love to your abandoned blog today.

What are you blogging today?

Making use of the ‘??’ operator in C#

The ?? operator was introduced to C# in 2.0 and I made a mental note to myself to use it when possible.

Recently I had to do some tinkering with good ole Request.Form and Request.QueryString and I kept trying get the neurons to connect and figure out the shorter way of doing it. I knew there was another way to do it than write all this.

   1: string filter = Request.QueryString["Filter"];
   2: if (filter == null)
   3: {
   4:     filter = "";
   5: }

And Google is no help when you have no keyword to search. This is when ReSharper came in handy and prompted to replace the above code with this succinct version.

   1: filter = Request.QueryString["Filter"] ?? "";

BTW: If you are coding in VS 2008 check out the ReSharper 4.0 nightly builds, it's awesome.

Making use of the ‘??’ operator in C#

Write CAML queries in code

In my very first webpart that I wrote I needed to write some CAML queries to filter and sort the lists.

Being the type of guy who hates seeing strings littering my code I searched the net for an API for CAML queries. Thankfully John Holliday had already done the hard work and created CAML.NET. Check out the clean syntax for the CAML code.

   1: string typeName = "My Content Type";
   2: string simpleQuery =
   3:     CAML.Query(
   4:         CAML.Where(
   5:             CAML.Or(
   6:                 CAML.Eq(
   7:                     CAML.FieldRef("ContentType"), 
   8:                     CAML.Value(typeName)),
   9:                 CAML.IsNotNull(
  10:                     CAML.FieldRef("Description")))),
  11:         CAML.GroupBy(
  12:             true,
  13:             CAML.FieldRef("Title",CAML.SortType.Descending)),
  14:         CAML.OrderBy(
  15:             CAML.FieldRef("_Author"),
  16:             CAML.FieldRef("AuthoringDate"),
  17:             CAML.FieldRef("AssignedTo",CAML.SortType.Ascending))
  18:     );

Here again you might run into the issue of adding an extra assembly to your deployment, the simple alternative that I took was to include the CAML class directly into me source tree. But remember to keep the copyright and license header in place as stated by the Ms-CL license.

Hopefully Microsoft will release a Linq to CAML library soon, there is already one at CodePlex by a Microsoft employee but what would be better is if it comes with SharePoint.

Write CAML queries in code

Migrated from dasBlog to BlogEngine.NET

After 5 years, starting from the humble BlogX that ChrisAn wrote to Clemens and then ScottH's dasBlog, I finally moved over to BlogEngine.NET today. My main reasons for moving over included a brand new codebase, more features, better themes and frequent releases.

The other platforms that I had in mind were WordPress and Grafitti CMS, I gave up on WordPress due to my hoster being on Windows and didn't want to go through the trouble of figuring out how to get url rewrites for PHP working on IIS under my hoster's limitations.

Grafitti would have been my choice but it choked when I tried to import my blog posts. I opened a ticket with Telligent Systems and the really good guys mailed me today that the fix would be out in the next release this Friday. But in the end I thought it best to go with an open source platform and one that focused solely on blogging.

Okay now to the gory details of migrating my blog over. Which actually took a total of less than 10 minutes to setup on my machine and less than 30 minutes to upload everything to my hoster.

This is what I did to get my posts over to BlogEngine.NET

1. Setup BlogEngine locally on my machine. This just involved unzipping the file and pointing IIS to the folder.

2. Exported dasBlog to BlogML

The next version of dasBlog is scheduled to include this tool out of the box but right now you'll need to download the zip that Paul Van Brenk has made available. You'll need to run the code from Visual Studio and it exports all your posts into the handy BlogML format (If anyone want's this in a nice UI let me know and I can upload it). Edit: I've released this now, see http://merill.net/post/2008/03/DasBlog-to-BlogML-Converter.aspx 

3. Import BlogML into BlogEngine.NET

BlogEngine starts up a fancy ClickOnce app when you click the Import button, just feed it the BlogML file and presto all your posts and comments are migrated over.

I went with the Files and Images Moved Manually option which meant that I didn't have to mess around with moving any of the images or changing the paths.

4. Upload content

I simply deleted all the folders on my host server except for the ones that had the images and uploaded files which would usually be the content folder. Even here you could all the xml files that hold the posts.

Next I uploaded the whole BlogEngine folder that I had locally and remember to get your hosting provider to allow write permission on the App_Data folder.

That's all!

5. Configure settings

Obviously you'll want to play around in the control panel of the new blog and do things like put in your Google Analytics and other tracking script (which you had to edit by hand manually), redirect to the feedburner url so that your readers get your messages.

There are some disadvantages though like the incoming links being broken but since I don't have anything noteworthy that people link to this isn't going to be a problem for me.

Migrated from dasBlog to BlogEngine.NET

Getting ModifyUserPropertyByAccountName to work

The documentation for the ModifyUserPropertyByAccountName method of the UserProfileService in the SharePoint Server SDK has a bug.

You’ll notice that although there are no errors reported the profile doesn’t get updated. The fix is quite simple though and is actually present in the other examples. Setting the IsValueChanged to true does the trick.

Here’s how the corrected code looks like.

   1: namespace ModifyUserPropertyByNameSample
   2: {
   3:     class Program
   4:     {
   5:            
   6:         static void Main(string[] args)
   7:         {
   8:             //Instantiate the Web service. 
   9:             UserProfileService userProfileService = 
  10:                 new UserProfileService();
  11:  
  12:             //Set credentials for requests.
  13:             //Use the current user log-on credentials.
  14:             userProfileService.Credentials =
  15:                 System.Net.CredentialCache.DefaultCredentials;
  16:  
  17:             PropertyData[] newdata = new PropertyData[1];
  18:             newdata[0] = new PropertyData();
  19:             
  20:             newdata[0].Name = "FirstName";
  21:             newData[0].IsValueChanged = true;
  22:  
  23:             newdata[0].Values = new ValueData[1];
  24:             newdata[0].Values[0] = new ValueData();
  25:             newdata[0].Values[0].Value = "Mark";
  26:  
  27:             // TODO 
  28:             // Replace "domain\\username" with valid values.
  29:             userProfileService.ModifyUserPropertyByAccountName
  30:                 ("domain\\username", newdata);
  31:  
  32:         }
  33:     }
  34: }

 

For what it’s worth I’ve uploaded a working solution to the MSDN Code Gallery that allows you to view the current profile data and make changes by providing the name/value pair through the query string. You can download the SharePoint Profile Updater here.

Getting ModifyUserPropertyByAccountName to work

Efficiently Delete / Purge All Items from a SharePoint List

If you want to delete or remove all the items in a SharePoint list you need to iterate through each item and perform a delete. Most of the examples on the web iterate through each item to do the delete. The problem there is that each delete action makes a request to the server and a huge list is going to take a long time. You could always delete the entire list and recreate it but that would mean recreating the structure as well as the guid being changed.

The better method would be to make a single request with all the delete commands batched together as shown in the code sample below.

   1: private static StringBuilder BuildBatchDeleteCommand(SPList spList)
   2: {
   3:     StringBuilder sbDelete = new StringBuilder();
   4:     sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
   5:     string command = "<Method><SetList Scope=\"Request\">" + spList.ID + 
   6:         "</SetList><SetVar Name=\"ID\">{0}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar></Method>";
   7:  
   8:     foreach (SPListItem item in spList.Items)
   9:     {
  10:         sbDelete.Append(string.Format(command, item.ID.ToString()));
  11:     }
  12:     sbDelete.Append("</Batch>");
  13:     return sbDelete;
  14: }

I’ve published a working solution on the MSDN Code Gallery at http://code.msdn.microsoft.com/SharePointListDelete.

Efficiently Delete / Purge All Items from a SharePoint List

Uploading Videos

I’ve been playing with uploading videos today and MSN Video seems to outshine the other when it comes to uploading as well as viewing video.

The biggest strength being the uploader that comes built in to Windows Live Photo Gallery. With YouTube you need to wait for the whole video to upload which can take quite a lot of time, if not you need to muck around with some compression program and fine-tune the settings before doing the upload. I don’t have the patience for that.

The Photo Gallery uploader seems to compress the file as it uploads making it extremely fast.

PublishVideoOnSoapbox

UploadingToSoapbox 

Then Windows Live Writer makes it as easy as Insert->Video to allow you to pick the video that you uploaded.

Insert Video

Uploading Videos