Wednesday, May 27, 2009

The Scribd Store: Another way to expand your portfolio...AND your wallet

As you may have read on my blog, I am a fan of Scribd, and I am a regular user. Scribd has released an exciting feature. Scribd now has its own storefront that will allow you to sell your written works.

Not only is the store a way to earn money on technical and research documents that you have written, but it is a way to build your professional portfolio.

  • Published works are always great projects to have on a professional portfolio.

  • The analytics tools that come with the store displays sales and viewing figures that you can use to demonstrate your product's marketability to potential employers.

  • Having your own store demonstrates business skills - a secondary skill - because you have to manage the sales and marketing of your documents.

For more information, visit

Monday, May 18, 2009

SharePoint 2007: System.OutOfMemoryException in SiteData During Search Crawl

Lately at work we've been getting these errors on our SharePoint site. From what I was able to Google, it looks like others have this problem as well.

After doing some research, I found a post from Ranjan Banerji, who also experienced this problem in his environment. This post goes into deep detail as to how they worked around this problem. You may want to read through the post. I've found it to be very helpful.

In addition to the information mentioned in the post, we found that we had users who had abnormally long names for folders and files. As a result, it would make a path that was entirely too long for SharePoint to handle. From what we were able to tell in our logs, we noticed that the crawler would "choke" on these entries, which would then trigger the OutOfMemoryException.

If you have encountered this problem due to a reason not mentioned by me or Ranjan, please feel free to post a comment.

Friday, May 8, 2009

SharePoint Development Tutorial: Programmatically Create View on a List

While it is easy to create a view for a list in SharePoint, you may find a scenario where you need an easier way to create a view on a list. For example, I had a situation where I had to create over 30 views for one list. Creating the views using SharePoint can be time-consuming, and it could increase the possibility of user error because of the manual process. Using C#, the Microsoft.SharePoint library, and CAML, one can create a view for a list on SharePoint.

This illustration will demonstrate the simplest example of using C# code to add a view to a list (as a console application). You may want to make modifications for your needs, or use it as a foundation for a larger-scale application.

Note: This code will only work on a server where SharePoint is installed. For example, if you are going to add a view to a list that exists on http://bogus/bogussite, you need to have this code on the server that houses http://bogus.

The Scenario

In this illustration, there is a list on a SharePoint site called "Doctor Who Episodes". This list needs a new view called "JNT Era", which will display Doctor Who episodes made during the JNT era (1980-1989). This list has four fields:

  • Title

  • The Doctor

  • Synopsis

  • Year

See the example list

After starting a new project, you will need to add a reference to the Microsoft.SharePoint library in your code, and you will need to indicate that you will be using the library in your code.
See the example snippet

The next step is to define the SharePoint classes that you will need to perform the action. The four classes that you will need are:

  • SPSite - Collection of sites

  • SPWeb - The SharePoint web site

  • SPList - A List on a SharePoint web site

  • SPViewCollection - Collections of views on a list

In the code, you will need to access the website. You will define an instance of the SPSite class to get the site, then you will need to define an instance of the SPWeb class to have a reference to the actual site.

SPSite oSite = new SPSite("http://bogus/bogussite");
SPWeb oWeb = oSite.OpenWeb();

Alternative to SPList oList = oWeb.Lists["List Name"]

You can also access the list by the GUID. Use the following logic if you want to access the list by the GUID rather than the name:

Guid gui = new Guid("The List GUID");
SPList oList = oWeb.Lists[gui];

Then, you will need to access the list that will get the new view, and you will need to access the list's collection of views.

SPList oList = oWeb.Lists["Doctor Who Episodes"];
SPViewCollection oViewCollection = oList.Views;

Finally, you will need to define the new view name.

string strViewName = "JNT Era";

See the example snippet

We are ready to define the fields that will be displayed in this new view. In this example, the view will display all the fields. You will need to define an instance of the System.Collections.Specialized.StringCollection class to hold the names of the fields that will be displayed in the view.

System.Collections.Specialized.StringCollection viewFields =
new System.Collections.Specialized.StringCollection();

To add the fields, use the Add() function of the class. You do not have to use the special hexadecimal characters to represent spaces and special characters for the field names. You can use the literal field name.
See the example snippet

Helpful Links:

CAML Syntax

Stramit Caml Viewer

We have to define the criteria for the view using CAML. In our illustration, we want to display the Doctor Who episodes from 1980 until 1989, and we want to display them in ascending order. Your scenario may be different, so your CAML will be different. If you are unsure of CAML syntax, a link to a site that has documentation on the CAML syntax has been provided for you on this post. If you want to make sure that your query will work before you begin coding, you can use the Stramit Caml Viewer to test your CAML query. A link to the tool has also been provided for you on this post. In this illustration, I am using the StringBuilder class to hold the CAML query. Then, I am converting it to a String.
See the example snippet

Finally, we are ready to add the view. The Add() function of the SPViewCollection class adds a view to the list. The Add function
takes six parameters:

  • View Name - a string

  • Collection of View Fields - string collection

  • CAML query - a string

  • Row Count - an integer

  • Is this paged? - a boolean

  • Is this going to be the default view? - a boolean

You also have to call the Update() function of the SPWeb class to make sure the changes "take".

oViewCollection.Add(strViewName, viewFields, query, 5000, true, false);

See the example snippet

After completing, compiling and running the program, the list now has a new view!
See the results

If you would like a copy of the skeleton code, you can download the RTF file of the skeleton code here.

Please post your questions or comments, and I will answer your questions to the best of my ability.