Monday, June 15, 2009

Congratulations Pittsburgh Penguins!!



Congratulations to the Pittsburgh Penguins for beating the odds and winning the Stanley Cup!

Wednesday, June 3, 2009

A Look at CAML for SharePoint Development

Overview


For a quick summary of how CAML improved performance on a list items query, read this article from author and SharePoint development expert Sahil Malik.
The Collaborative Application Markup Language (CAML) is an XML-based language that is primarily used to perform data manipulation. In a number of cases, using CAML helps improve your program's performance.

CAML Tutorial


If you are unfamiliar with CAML syntax, Karine Bosch wrote a fantastic tutorial on writing CAML queries for SharePoint Magazine. She covers all the basics and the "gotchas" of CAML syntax in an easy-to-follow manner.

Helpful Tools


When I am writing code where I need to use CAML, I am partial to two FREE tools.
Screenshot of Stramit CAML viewer. Click on the image for a larger view.


The Stramit CAML Viewer is an "all-in-one" tool that not only lets you view and test CAML queries, but it also displays the GUIDs for lists and views. You can download this tool from CodePlex.

Screenshot of U2U Caml Query Builder. Click on the image for a larger view.


The U2U CAML Query Builder is a slick, time-saving tool that will automatically build the CAML query for you based on the information entered by you. It also has a feature where you can copy the built CAML query to the clipboard. You can download this tool from the U2U site.

CAML in Action: A Simple Query



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.


In this example, I have a list on my site called Doctor Who Episodes. I would like to get the list of episodes that starred Jon Pertwee as the Doctor, and I would like to order that list by the year. My application, written for demonstration purposes only (no "fancy" formatting, application usability design, or full performance evaluation was considered), will display the episode list in a text box the moment the application runs (Form_Load).
Click on the image for a larger view.


After creating a new C# Windows application called SimpleCAMLQuery, I added reference to the Microsoft.SharePoint library in my project and referenced the library in my code.

using Microsoft.SharePoint;


I added the following in the Form_Load event:
I defined three variables to hold the URL for the site, the list name, and the view name.

String siteName = "http://bogus/bogussite";
String listName = "Doctor Who Episodes";
String viewName = "All Items";


I then used SPSite, SPWeb, and SPList to open my list on my site. I also used SPQuery to open my view, since this class has the method/function to use for the CAML query.

SPSite site = new SPSite(siteName);
SPWeb web = site.OpenWeb();
SPList list = web.Lists[listName];
SPQuery query = new SPQuery(list.Views[viewName]);


I then build my CAML query using the StringBuilder class. You can also put the CAML query directly into a string object if you prefer. If the field has spaces or special characters in the name (as it does in my example - the field in my list is called "The Doctor"), you need to reference the hexadecimal values in the name.


System.Text.StringBuilder oSb = new System.Text.StringBuilder();
oSb.Append(" <Where>");
oSb.Append(" <Eq>");
oSb.Append(" <FieldRef Name=\"The_x0020_Doctor\" />");
oSb.Append(" <Value Type=\"Text\>Jon Pertwee</Value>");
oSb.Append(" </Eq>");
oSb.Append(" </Where>");
oSb.Append(" <OrderBy>");
oSb.Append(" <FieldRef Name=\"Year\" Ascending=\"True\" />");
oSb.Append(" </OrderBy>");

Note: The order in which the Where and the OrderBy tag blocks appear in the CAML doesn't matter. Some people place the OrderBy tag blocks before the Where tag blocks. I usually place the OrderBy tag blocks after the Where tag blocks because I'm used to writing SQL, and in SQL syntax, the Order clause comes after the Where clause.

I then called the Query method/function of the SPQuery object to set up the CAML query

query.Query = oSb.ToString();


Finally, I put the results in a SPListItemCollection object and looped through the object.

SPListItemCollection results = list.GetItems(query);
foreach (SPListItem i in results)
{
  txtResults.Text += i["LinkTitle"].ToString();
  txtResults.Text += Environment.NewLine;
  txtResults.Text += i["Synopsis"].ToString();
  txtResults.Text += Environment.NewLine;
  txtResults.Text += i["Year"].ToString();
  txtResults.Text += "----------------------------------------";
  txtResults.Text += Environment.NewLine;
  txtResults.Text += Environment.NewLine;
}



Here is the full code:
Click on the image for a larger view.


Additional examples of CAML in Action


If you would like to see additional examples of using CAML, view these previous posts on my blog:


If you have any questions, comments, or additional examples, please feel free to post a comment.

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 http://www.scribd.com/store/about.

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);
oWeb.Update();

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.

Thursday, April 30, 2009

Why Twitter is a Helpful Tool if You Are or Are Going to Be in IT

If you don't know what Twitter is, here is a link to the Wikipedia article on the service.

If you haven't been living in a cave for the past six months, you've probably noticed that Twitter (http://www.twitter.com) has become the newest Internet application star. Twitter is a way for people to send updates via the web, a mobile phone, or an application on a PC or Mac. I use Twitter, and I find it to be a very useful tool in my line of work. By streamlining how one uses twitter, it can be a useful tool for all IT professionals and aspiring IT professionals. Here's how:


  • Twitter is a way to expand your technical knowledge.

    I follow a number of tweeple (people who use Twitter) who make informative posts about what's on in IT. Not only do I get instant technology news, but I also get posts containing: links to technology how-tos and tips; links to training and webinars; and career tips. It saves me a lot of time from sifting through websites and search engines to find information.

  • Twitter is a way to get assistance with your IT issue.

    Most forums are good tools to use to post questions. Twitter is another tool that you can use to post questions. Based on my experience, I've posted questions to the Twitter community, and more often than not, I got an answer to my question relatively quickly.

    If you've already asked a question in a forum, and your question is not getting a response, you can use Twitter to post a link to the question. If the regular readers of the forum can't answer your questions, perhaps someone in the Twitter community can.

  • Twitter is a way to get publicity for your professional work.

    If you're looking to expand your audience, Twitter is a good tool to use. I've recently started to use Twitter to publish links to my tutorial posts on this blog, and I've noticed a slight increase in my traffic. Other things that I've seen other tweeple post: links to custom applications and SharePoint web parts that they have written; links to web sites that they have designed; and podcasts and videos in which they were involved.

  • Twitter is a way to expand your professional social network, as well as assist with your job search.

    There are a number of IT professionals who use Twitter, and a large number of those tweeple post IT-related content 95% of the time. You can build your network by re-tweeting informative posts, as well as answer questions that other tweeple have posted.

    I've also noticed that a number of small business owners and IT job recruiters use Twitter to post job openings. You can use Twitter to keep abreast of new opportunities.



Do you use Twitter? Do you like Twitter? Can you see a use for Twitter for your profession? Please feel free to post any comments or questions that you have.

P.S. If you use Twitter, and if you are interested, you can follow me on Twitter

Wednesday, April 22, 2009

SharePoint Development Tutorial: Programmatically Deleting All Items from a List

Microsoft Reference Documentation: Microsoft.SharePoint namespace

Sometimes you need a quick way to delete all items from a SharePoint list. You can manually delete each item from the list without writing code, but this can be time-consuming, especially if you have a high quantity of items on the list.


Click the link to download the source code for this post

Licensing and Warranty

You may use the code as you wish - it may be used in commercial or other applications, and it may be redistributed and modified. The code is provided "as-is". No claim of suitability, guarantee, or any warranty whatsoever is provided. By downloading the code, you agree to defend, indemnify, and hold harmless the Author and the Publisher from and against any claims, suits, losses, damages, liabilities, costs, and expenses (including reasonable legal or attorneys' fees) resulting from or relating to any use of the code by you.




You can write code to programmatically remote items from a SharePoint list using the classes in the Microsoft.SharePoint library (namespace). There are two ways that you can delete all the items from the SharePoint list programmatically:

  • Using the DeleteItemById() function of the SPList class

  • Recommended for performance:Building CAML and using the ProcessBatchData() function of the SPSite class



I will demonstrate both approaches. The skill sets required for these approaches are:

  • C# knowledge

  • A general knowledge of the classes in the Microsoft.SharePoint library/namespace

  • A general knowledge of CAML (for approach #2)



Note: This code will only work on a machine on which SharePoint is installed.


Approach #1: Using the DeleteItemById() function of the SPList class


The function DeleteItemById() of the SPList class expects a numeric parameter ID, which is the ID of the item to delete. This code snippet, which is a slightly modified version of a function in Keith Richie's SPPurgeList (http://www.codeplex.com/sppurgelist), demonstrates how to delete all items by capturing the IDs in a hashtable, reading the hashtable, and calling the DeleteItemById() function. In this example, we will be running this on a site called http://mySharePoint/bogus on a list called BogusList. If you want to use this code, you would substitute the site and list names with your respective site and list names.

Sample: Using DeleteItemById() [Opens In Another Window]

The issue with Approach #1 is performance. While it is negligible on a list with a small amount of item, it becomes more noticeable on a list with hundreds of items. Each call to DeleteItemById() takes about 1 second. If you have hundreds of items, this could take a while to run.

Approach #2: Building CAML and using the ProcessBatchData() function of the SPSite class


Passing CAML to the ProcessBatchData() function of the SPWeb class runs faster than caling the DeleteItemById(). The following code snippet demonstrates how to build the CAML (using the StringBuilder) and pass the CAML to the ProcessBatchData() function to delete the items. In this example, we will be running this on a site called http://mySharePoint/bogus on a list called BogusList. If you want to use this code, you would substitute the site and list names with your respective site and list names.

Sample: Using CAML and ProcessBatchData() [Opens In Another Window]

A Note About Using CAML to Delete Items from a Document Library

The code snippet above will work for the lists except for document libraries. If you try to run the code snippet on a document library, you will get an error about file names. The document libraries require an additional parameter called owsfileref. The following link to the code snippet demonstrates how to build the CAML for document libraries.
Sample: CAML for document libraries [Opens In Another Window]

If you have any questions or comments, please feel free to post them, and I will answer the questions to the best of my ability.