Tuesday, August 18, 2009

How to avoid recursive calls in list item event receivers in SharePoint

I wanted to insert a value into a column in a list when adding and updating items in the same list.

First I registered the item event receiver into the list.

SPList list = web.Lists[listName];
list.EventReceivers.Add(eventType, assemblyName, className);

Then in the actual event receiver class I override the “ItemAdded” and “ItemUpdated” methods.

public override void ItemAdded(SPItemEventProperties properties)
SPListItem item = properties.ListItem;
string itemStatusValue = string.Format("Item added @ {0} by {1}", DateTime.Now.ToLocalTime(),
item["columnName"] = itemStatusValue;


public override void ItemUpdated(SPItemEventProperties properties)
SPListItem item = properties.ListItem;
string itemStatusValue = string.Format("Item updated @ {0} by {1}", DateTime.Now.ToLocalTime(),
item["columnName"] = itemStatusValue;


But when I debug this in VS2008 I noticed that, as the item get updated or added it always calling to the “ItemUpdated” method.Of course that is understandable since I am updating a column in the same list.

But I didn’t want that to happen.Then I found out that you can enable/disable event firing.

So the solution for that is ,


It is pretty straight forward once you get to know it.

More references on this : Visit BinaryJam

Friday, August 14, 2009

How to avoid inline codes in user controls in SharePoint web parts

When I was at the beginning of developing web parts for SharePoint , I wanted to find a way of reducing the time consume to develop web parts using Web User Controls (*.ascx) as web parts.Otherwise you will have to write all the mark ups in the CreateChildControls method.

Check here how to use web application projects to develop web parts.

So I was very fond of that method till I figured out, this is not going to inline with the SharePoint deployment techniques.Especially if you are using wsp solution packages and 12 hive structure in Visual Studio with WSPBuilder.

Recently in a project that I was working with, I used a slightly different approach to which I was used to.That is not using web application projects but I used the user control(*.ascx file) with inline codes for the web part in the templatecontrol folder.

There are pros and cons having inline codes.

The code is visible to a person who has access to the server directly.

If it is a complex web part your lines of code will be very high and which will lead to readability and maintainability issues.

Today I managed to figure out how to move inline code into your main assembly of your SharePoint solution and I thought of sharing that with you.

SampleWebPartControl.ascx (user control)

What is important here is that the entry in the RED box in above image.That is the way you specify where the code behind can be found for this user control.

SampleWebPartCOntrol.cs(code behind)

The initialization of user controls you are using is paramount(check the RED box in this image).I had all sorts of problems till I sorted that out.

Solution Structure

Check where the items in the RED box in this image.

Thursday, August 13, 2009

Subscribe to RSS feed of my blog

Now you can subscribe to my blog using RSS.


  • Go to this RSS Feed.
  • Select the “Subscribe to this feed using” as you need.My recommendation is “Microsoft Office Outlook”.
  • Click on the “Subscribe Now” button.
  • Click on the Advanced… button.
  • Change the details as you want.
  • Click Ok and then click on Yes.

Now you have my blog feeds at your finger tips.

OR you can add the "Feed" gadget into your blog with the given feed url.

Friday, August 07, 2009

List content type troubleshoot

Recently I had a problem with the default content types that are bind with a custom list.

I created an Announcement list and I have bind my own content type into it.Then I wanted to hide the default content type which is the “Announcement”.Initially I used the name to get the content type from the list but later I had to re factor it simply because, if we are to support multilingual we should not use names but id’s.

Then I thought this should be a piece of cake and I re factored it like bellow.

SPList list = web.Lists[listName];
SPContentType item = list.ContentTypes[SPBuiltInContentTypeId.Announcement];
item.Hidden = true;

To my amazement , as I activated the updated feature I got the error “Object reference not set to an instance of an object”.

And then I open this list from the SharePoint Manager and checked the ID of the content type “Announcement” and it is totally different one to the default content type id which is the “0x0104”.

Then I got some expert help on this and got to know that this is because what ever the bind content types in lists are a copy of the site content type.Thank you Bjorn for letting me know about it.

So here is the new code of doing it.

SPContentType item = list.ContentTypes[list.ContentTypes.BestMatch(contentTypeId)];

It was that simple once you got to know it.

So what you need to understand here is that adding a content type to a list means that you actually adding a copy of the content type.