Skip to main content

Posts

Showing posts from December, 2009

Limiting number of Attachments per SPListItem

Here is an example on how we can limit the number of attachments per SPListItem. We need to write an event handler on the event "ItemAttachmentAdding". Lets straight away get into the example: public override void ItemAttachmentAdding(SPItemEventProperties properties) { if (properties.ListTitle == "List Name") { //Do not allow more than one attachment per List Item if (properties.ListItem.Attachments.Count >= 1) { properties.Cancel = true; properties.ErrorMessage = "More than one attachment is not allowed per List Item!"; } } } For the particular List, it checks for the number of attachments. If the ListItem already contains an attachment (Count = 1), it cancels the event and displays a custom message to the user.

Instead of custom error message, it displays %1!.512s!

I was working on some event handlers. I was trying to display a custom error message whenever a condition is met. So my code went like this: properties.Cancel = true; properties.ErrorMessage = "Custom Error Message"; Even though, the event was getting canceled, but to my surprise, the SharePoint always displayed a message as "%1!.512s!". I spent so much of time to figure out what was wrong. Finally, when I did googling, I was taken to this KB article where it is mentioned that it is a bug! :( Well, it was time to bang my head for some other reason!

Restore error due to different versions (RTM and SP1)

One of my friends faced an error during the restoration of Site Collection from one server to another server. Error: Your backup is from a different version of Windows SharePoint Services and cannot be restored to a server running the current version. The backup file should be restored to a server with version '12.0.0.6219' or later. Cause: The source server was installed with SharePoint SP1 (12.0.0.6219). The destination server was installed with SharePoint RTM version (12.0.0.4518). To find out the SharePoint version installed, follow these simple steps: Open Central Administration Click on Operations Click on Servers in Farm Under "Farm Information", you can see the version. Resolution: It is a normal practise to ensure that Dev, and Staging servers are in sync with Production server. In the above case, either the source server should be made same as the destination server or destination server needs to be upgraded to SP1. The former approach is better as it involv...

Calculated field for Date column

In a SharePoint list or document library, we can create a calculated field. This field becomes handy when we have to display a formatted/calculated value based on the existing column. We often hear the requirements wherein we have to display a formatted value of the Date column. Ex., Display only Month value of a date column, display only Month and year part of the date column and so on. Let me show you how we can achieve this: For this example, create a list having a date field. When you create a list, by default, SharePoint adds two date fields "Modified" and "Created". So you can use any of these columns or you can create a custom Date field. After creating a list, now create a column of type "Calculated". Under the Formula text box, we need to enter the formula which will fetch us the desired value. In my example, I'm going to use a custom date field "StartDate". I want to display Month of the StartDate column in 3 characters like Jan, Fe...