Wednesday, September 4, 2013

SharePoint Custom Field - Regex Validator

Finally!!.. I have published my first project on codeplex. Here is a brief about that project:

Project Description
This is a SharePoint 2010 custom field type project that provides Regex (regular expression) validation on a text field.

In few words
This field provides some inbuilt validation options like Email, SSN, Phone no., IP Address and Alphanumeric. Besides this, it also provides an option to specify your own regular expression with an error message.

DecodingSharePoint_Regex_Field.jpg

When a user will try to add/edit list item, this custom field will check if the value matches the regular expression and it will throw an error, if not matched.

validation screenshot.jpg

How to add custom regex options
If you want to add some more inbuilt regex options like Email or SSN then you can make changes to the following files as described below :
  • In ControlTemplates\DS_Regex_FieldEditor.ascx
    • Add new item in "rbnRegexType" RadioButtonList control
      • e.g. <asp:ListItem Text="Alphanumeric XXX" Value="Alphanumeric"></asp:ListItem>
  • In DS.Regex.Common.cs
    • Create a new string and its value must be equal to the value specified in the previous step i.e. in DS_Regex_FieldEditor.ascx file
      • e.g. : public static string Alphanumeric = "Alphanumeric";
  • In DS.Regex.ValidationRule.cs
    • Add regex in StandardRegularExpressions Dictionary using the constant defined in DS.Regex.Common.cs file.
      • { DS_Regex_Common.RegexType.Alphanumeric, @"^\w+$"}
    • Add error message in StandardErrorMessage Dictionary using the constant defined in DS.Regex.Common.cs file.
      • { DS_Regex_Common.RegexType.Alphanumeric, "Please enter valid input"}

Installation
  • Add-SPSolution -LiteralPath <Path>\DecodingSharePoint_Regex_Field_Type.wsp
  • Install-SPSolution -Identity DecodingSharePoint_Regex_Field_Type.wsp -GACDeployment

You can download the source code and deployment package from here :
https://spregexfield.codeplex.com/

Saturday, August 24, 2013

Insert Item in SharePoint list with specific ID

I recently came across with a problem that someone deleted an item from a list and then cleared the recycle bin. Now that item was previously referred in many lists as lookup field, so they all became null referenced lookup and as a result of that, site got crashed.

So I had 3 choices to correct this :
1) To correct all references in all lists by building a utility or do it manually (time consuming process)
2) To correct the code to handle these type of issues (time consuming process)
3) To insert the item with that specific id.

I tried to find the solution to 3rd option, but had no luck. So, I dig little deeper and came up with this solution. Although it is not a very good idea to set internal fields of sharepoint explicitly. But sometimes we have to do it. :)

Here is the solution :

private static void InsertItemWithSpecificID(int itemID)
{
    string siteUrl = "http://www.contoso.com";
    string listName = "Employee";
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {    
        using (SPSite site = new SPSite(siteUrl))
        {
            using (SPWeb web = site.OpenWeb())
            {
                SPList list = web.Lists.TryGetList(listName);

                if (list != null)
                {
                    // The ID field is readonly field, set to to read and write mode.
                    list.Fields[SPBuiltInFieldId.ID].ReadOnlyField = false;
                    list.Update();

                    SPListItem item = list.AddItem();
                    item["Title"] = "This item Id's is not auto-generated by SharePoint, it is specified explicitly.";
                    item[SPBuiltInFieldId.ID] = itemID; //item[SPBuiltInFieldId.ID] = 3;
                    item.Update();
                    list.Fields[SPBuiltInFieldId.ID].ReadOnlyField = true;
                    list.Update();
                }
            }
        }
    });
}