Sunday, 7 April 2013


SharePoint SPLongOperation

Sometimes the actions triggered by users can take a while to process. This can for example happen when you create a site collection. At that time you:

  • Don’t want your users to continue clicking on the button, the action should finish first. With the example of the creation of a site collection, you don’t want two attempts of creating a site collection with the same URL.
  • Users must be made aware that the action might take a while and have the idea something is really happening behind the scenes.

 Microsoft provided some nice ways to have implement those long running operations. One is SPLongOperation which shows the user the “Operation in Progress” screen and it is actually pretty easy to implement.

Here is the way to use this. 


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
protected void btnSubmit_Click(object sender, EventArgs e)
 {
 
     using (SPLongOperation longOperation = new SPLongOperation(this.Page))
     {
         // Provide the text displayed in bold
         longOperation.LeadingHTML = "";
    // Provide the normal formatted text
         longOperation.TrailingHTML = "Please wait your changes are processed";

         longOperation.Begin();
   
         
         string URL = SPContext.Current.Web.Url + "/Pages/OperationDone.aspx";
         longOperation.End(URL);
     }
 }
This will give you a screen like below : 


Thank you !! 

Sunday, 3 March 2013

Creating a simple Custom Field Type for SharePoint 2010 using Visual Studio


Email Validation Field
1.      In Visual Studio, create an Empty SharePoint Project. Give a suitable name for your project and here its “CustomNewFileld 
2.      Right-click the project name in Solution Explorer and select Add | New Item
3.      In the Add New Item dialog box, select Visual C#
4.      Select Class from the template and give name as EmailField.csn 
5.      Coding EmailField.cs file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Text.RegularExpressions;
namespace CustomNewFileld
{
    public class EmailField : SPFieldText // Base Class
    {
        public EmailField(SPFieldCollection fields, string fieldName)
            : base(fields, fieldName)
        { // Default Constructor
        }
        public EmailField(SPFieldCollection fields, string typeName, string displayName)
            : base(fields, typeName, displayName)
        { // Default Constructor
        }
       // If you need default value in NewForm you can use the below method
       /*public override string DefaultValue
        {            get{ return "Test"; }        }*/

·         GetValidatedString is used for data serialization logic and for field validation logic that is specific to a custom field type to convert the field value object into a validated, serialized string.
·         GetValidatedString method check whether the field is required and, if it is the overridden method throws an SPFieldValidationException exception when the value is null or an empty String.
·         SPFieldValidationException when the value is not valid, causing an error message to appear beneath (under) the invalid field.
        public override string GetValidatedString(object value)
        {
            String Email = (String)value;
            bool result = isEmailID(Email);
            if (Email == "")
                throw new SPFieldValidationException("Field should not be empty");
            else if (result != true)
            {
                throw new SPFieldValidationException("Enter valid Email");
            }
            else
            {
                return value.ToString();
            }
           
        }
        public static bool isEmailID(string inputEmail)
        {
            string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                  @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                  @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
            Regex re = new Regex(strRegex);
            if (re.IsMatch(inputEmail))
                return (true);
            else
                return (false);
        }
    }
}
6.      In Solution Explorer, right-click the project name and select Add, then SharePoint Mapped Folder 
7.      Use the tree control that opens to map the folder to TEMPLATE\XML and click OK.
8.      Right-click the new XML folder (not the project name) in Solution Explorer and select Add | New Item.
9.      In the Add New Item dialog box, select Visual C# and select an XML file; give the name as Fldtypes_EmailField.xml.


<?xml version="1.0" encoding="utf-8" ?>
<FieldTypes>
       <FieldType>
              <Field Name="TypeName">Email</Field>
              <Field Name="ParentType">Text</Field>
              <Field Name="TypeDisplayName">Email</Field>
              <Field Name="TypeShortDescription">Email Validation</Field>
              <Field Name="UserCreatable">TRUE</Field>
              <Field Name="ShowOnListCreate">TRUE</Field>
              <Field Name="ShowOnSurveyCreate">TRUE</Field>
              <Field Name="ShowOnDocumentLibraryCreate">TRUE</Field>
              <Field Name="ShowOnColumnTemplateCreate">TRUE</Field>
              <Field Name="FieldTypeClass">           CustomNewFileld.EmailField,$SharePoint.Project.AssemblyFullName$</Field>
              </FieldType>
</FieldTypes>

CustomNewFileld - Namespace name (Mostly the project name).
EmailField - .cs Filename which has the default constructors.
Email Validation – Name to be displayed in creating a New Column.
10. Right-click the References node in Solution Explorer, click Add Reference, and select PresentationFramework.dll on the .NET tab in the Add Reference dialog box. Click OK.
11. Finally Build and Deploy solution.