Book Image

Microsoft SharePoint 2010 Development with Visual Studio 2010 Expert Cookbook

By : Balaji Kithiganahalli
Book Image

Microsoft SharePoint 2010 Development with Visual Studio 2010 Expert Cookbook

By: Balaji Kithiganahalli

Overview of this book

Microsoft SharePoint 2010, is the best-in-class platform for content management and collaboration. With the combined capabilities of Sharepoint and Visual Studio, developers have an end-to-end business solutions development IDE. To leverage this powerful combination of tools it is necessary to understand the different building blocks. This book will provide necessary concepts and present ways to develop complex business solutions and take them further.SharePoint 2010 Development Cookbook With Visual Studio 2010 is an instructional guide for developing and debugging applications for SharePoint 2010 environment using Visual Studio 2010. The cookbook approach helps you to dip into any recipe that interests you, you can also read it from cover to cover if you want to get hands on with the complete application development cycle.With this book you will learn to develop event handlers, workflows, content types, web parts, client object model applications, and web services for SharePoint 2010 in an instructional manner. You will discover the less known facts behind debugging feature receivers, deployment of web parts, utilizing free toolkits to enhance the development and debugging experience. You will learn the newer development approach called Visual Web Parts, how to develop and deploy Silverlight applications that can be used with Silverlight web part. You will also explore SandBoxed deployment model and its usage. You will create your own web services for SharePoint and the Client Object Model introduced in SharePoint 2010. All in all, you will develop Sharepoint solutions in an instructional manner that eases the learning process.
Table of Contents (15 chapters)
Microsoft SharePoint 2010 Development with Visual Studio 2010: Expert Cookbook
Credits
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
Index

Adding a custom error message to the Event Receiver


When we created our Event Receiver in the previous recipe, we saw that if we cancelled the insertion of an item into a list (due to a data validation error), an ugly error message appeared indicating that an Event Receiver had cancelled the request. It is difficult for users to comprehend this error message, it does not indicate that the error is due to data validation and that there is nothing wrong with SharePoint. In this recipe, we will customize this error message that indicates what the error is.

Getting ready

The solution from the previous recipe is used for further modifications. You should complete the previous recipe successfully to follow this one.

How to do it...

  1. If you have closed your Visual Studio IDE, launch it now as an administrator and open the solution file that we created in the previous recipe.

  2. Open EventReceiver1.cs and add the following two lines of code just before the properties.cancel = true line in the workphone regular expression match "if statement":

    string sErrMsg = "Business Phone is not in correct format";
    properties.ErrorMessage = sErrMsg;
  3. Do the same for the Email regular expression match if statement. Your ItemAdding method should look like the following code bundle:

    public override void ItemAdding(SPItemEventProperties properties)
           {
               base.ItemAdding(properties);
             
               string sWorkPhone = properties.AfterProperties["WorkPhone"].ToString();
    
               string sEmail = properties.AfterProperties["Email"].ToString();
    
               if (!string.IsNullOrEmpty(sWorkPhone))
               {
                   if (!System.Text.RegularExpressions.Regex.IsMatch(sWorkPhone, @"^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$"))
                   {
                       string sErrMsg = "Business Phone is not in correct format";
                       properties.ErrorMessage = sErrMsg;
                       properties.Cancel = true;
                   }
               }
    
               if (!string.IsNullOrEmpty(sEmail))
               {
                   if (!System.Text.RegularExpressions.Regex.IsMatch(sEmail, @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$"))
                   {
                       string sErrMsg = "Email is not in correct format";
                       properties.ErrorMessage = sErrMsg;
                       properties.Cancel = true;
                   }
               }
    
           }
  4. When you build and run this solution, as done previously, you should be directed to the site that you provided in the solution creation wizard.

  5. Enter a new contact with an improper phone format for the Business Phone field. You should see the same error screen as shown previously including a custom error message that you coded as shown here:

How it works...

The properties object that you use in the ItemAdding method has a property called Error Message. It is null by default. When SharePoint encounters a null value, it just uses the default error message. Adding a custom error message to this field will cause SharePoint to display your error message.

There's more...

If you want to just cancel the insertion of an item into the list to list without displaying the error page as done previously, just set the properties object's status property as shown in the following code. This will cancel the insert, but will not display the error page.

properties.Status = SPEventReceiverStatus.CancelNoError;

More information on SPEventReceiverStatus can be found on MSDN (http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.speventreceiverstatus.aspx)

More information – How to get field names

In the Event Receiver we have used field name WorkPhone for the Business Phone and Email for the Email Address fields. How did I figure these names? In Visual Studio 2010 there is a new tool called Add SharePoint Connection. You can access it from menu Tools | Add SharePoint Connection

It will ask the URL to connect. Provide URL information for your site and it will list all the information pertaining to your site in a tree structure. This information is read-only and you cannot make any changes through the IDE. You can navigate to the Contacts List and to the fields as shown here:

You can select the Business Phone field and in the Properties window look for InternalName. This is the field name that we used in the code to get the values. Just go through the different properties of the field in the properties window, and you will find some interesting properties like Field ID which is a GUID, whether the field is hidden or not, and so on.

See also

  • Adding an Application Page to an Event Receiver recipe