Tuesday, June 28, 2011

Validate a SharePoint list column with regular expression

Column validations is in SharePoint 2010 a feature for lists, this is great when you need to validate a column like compare if a number is between some interval or if an end date is later than a start date.
Regular expression is not available out of the box as a feature in SharePoint 2010 column validation so how can we do this? If it´s about a smaller implementation like just one list form then jQuery would be the alternative. What about the third-party market? Take a look at Bamboos product called Bamboo validate column. If you need regular expression as an inbuilt feature in all lists in you environment, this product is definitely something to looking into.
How to use regular expression for validation with jQuery then? Let´s first take a look at the jQuery plugin called jQuery.Validate and how we can use this in SharePoint. This plugin offers a lot of options for customizations. JQuery validate has been around since early 2006 and is written by Jörn Zaefferer, lead developer of the jQuery UI team and it.

In following steps we will link our friend the Content Editor Web Part (CEWP) to a jQuery script stored in a document library at the same site. This apporach could be done by refering the master page, the page layout or as a packed web part if you prefer to.

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.4.js"></script>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>

<script type="text/javascript">
$(document).ready(function() { 

$("#aspnetForm").validate({
submitHandler: function(form) {
}}); 

$.validator.addMethod(
      "regex",
      function(value, element, regexp) {
          var check = false;
          var re = new RegExp(regexp);
          return this.optional(element) || re.test(value);
      },""
);

// VALIDATE TITLE
$("input[title='Title']").rules("add",{
 required: true,
 regex:"^[A-Za-z0-9- _]{1,100}$",
 messages: {
 regex: "<br />One or more special charachters.."
}
});

// BLUR -  Set focus after alert
$("input[title='Title']").blur(function() {
if (!$("input[title='Title']").valid()) {
this.focus();
}
}); 

// SAVE - Cannot save if regex rules adds
$("input[value='Save']").focus(function(){
if (!$("input[title='Title']").valid()) {
alert("You cannot save this Item..");
$("input[title='Title']").focus();
}
});

});
</script>

Let’s start:

  1. Copy the script above and save it to a text file (.TXT)
  2. Add this text file to Share Documents in the same site as the list. You can create a new document library if you like to this script separate. Copy the shortcut to this file.
  3. Create a custom list named Validate
  4. Create a column named Email with the type ‘single line of text’
  5. Go to the Validate Lists New Form page. Click the List tab in the ribbon and select Default New Form.
  6. Click Add a Web Part. Select Media and Content to the left and select Content Editor. Click Add.
  7. Click at the arrow to the right of the web part and select Edit Web Part. Paste or type the link to the text file you uploaded in step 2. Set the link to be relative like, e.g. /RegEx/Shared%20Documents/ValidateWebPart.txt
  8. Type a title in the Content Editor Web Part and select ‘none’ for the chrome type to and set the web part to be hidden. Click OK.

More things you can do

This example refers to latest jQuery, if you already call the jQuery from the master page make sure to not call it twice. If you are going to set this in production, make sure to use the same approach for the edit form page.
You can do a lot more with this plugin; let´s try to use some other validation methods like URL and Email validation. If you followed this example you created the column named Email, let´s validate this column so we can be sure that the user type a valid Email address here.
Just add this section in the same pattern as the script above. Paste this between validate title section and the blur section. You can add Email to the blur and save as well.

// VALIDATE EMAIL
$("input[title='Email']").rules("add",{
 email:true,
 messages: {
 email: "<br />Not a valid Email address.."
}
});
Check out this page for more information about validation and regular expression
About the validation plugin
http://docs.jquery.com/Plugins/Validation
Bamboo column validator
http://store.bamboosolutions.com/pc-149-1-bamboo-validator-column.aspx
8 Regular Expression you should know
http://net.tutsplus.com/tutorials/other/8-regular-expressions-you-should-know/

JQuery integration in SharePoint | SharePoint 2010

Follow the steps below to integrate JQuery into SharePoint.
  • Open your SharePoint site in SharePoint designer.
  • It’s always better to organize your data and files in good structure. So create a folder for placing all scripts named “Scripts” if it does not exist.
  • Now copy the Jquery script file to this folder. I am using the file jquery-1.3.1.js.
  • Create an ASPX page in your pages folder of the site if it is a published web site template otherwise create a page in the root of site. [However, any place it works.]
  • This page is not a web part page, we are just creating a simple ASPX page for JQuery integration.
  • Here, add a reference to the JQuery java script file to the head tag of the page.
  • Add the below code to test the Jquery functionality to body of the page.
  • <script type="text/javascript">
        $(document).ready(function() {
            $("#cb").live('click', function() {
            $("#lblMessage").text("you clicked on CheckBox, selected = " + $("#cb").attr('checked'));
            });
        });
    </script>
    <input type="checkbox" id="cb" />
    <label id="lblMessage"></label>

  • We just wrote a very small piece of code snippet for testing the JQuery functionality. This post main goal is to integrate the JQuery plug-in for SharePoint. The same way you can add reference to the master page of the site to get the advantage of JQuery in all pages of the web site.
  • We can apply the master page to the current ASPX page by following this post. This will give you the same look and feel as other pages.
  • We have plenty of ways to do this. For example, for simple integration purpose i explained you to place the JQuery file in scripts folder of the root of the web site. But good way of doing is, placing the file in Layouts folder of 12 hive in SharePoint system. This way you can access the file in any site and on any page through out the SharePoint. Because Layouts is the common sub site exist for all the sites.
  • Adding script reference to all the pages in a site:
  1. Add the script reference to the <HEAD> tag of the master page of the site. So that all pages have the reference to the JQuery script and you can use it any where.
  2. Syntax: <script type=”text/javascript” src=”/_layouts/scripts/jquery-1.3.1.js”></script>
  • Add script reference to specific pages:
  1. For this we have a good and nice web part to add html/script/css. That is nothing  but Content Editor Web part. We can add a content editor web part on the page [most probably on the top of page] and we will add the script reference code to it. Now the JQuery is available only for the pages where you added the code.

Monday, June 27, 2011

Hiding the Quick Launch in SharePoint 2010

Hiding the Quick Launch in SharePoint 2010

At times one would really feel no need of the Quick Launch section on few pages or sites i.e. the Team Site, one would really love to use the full width of the page for this one. Luckily the Content Editor Web Part trick which worked in SharePoint 2007 still works in 2010.

To hide the Quick Launch
1. Add a new Content Editor webpart. (Doesn’t matter where you add it)
2. Edit the HTML Source for the newly added Content Editor Webpart

3. In the HTML Source add the following and Click OK.
#s4-leftpanel{
display:none
}
.s4-ca{
margin-left:0px
}
[Do not forget to add the style open and close tag as seen in below image due to rendering issues its not showing up]

4. Save the Page. The Quick Launch section should not be visible now.
5. Edit the Just added Content Editor web Part and Under Layout Section Tick the Hidden Check box and Click OK, Which will hide the Webpart from Normal View.

6. This works on any SharePoint 2010 or Foundation 2010 Page and site.

Source: http://thecommunicator.co.cc/2010/04/07/hiding-the-quick-launch-in-sharepoint-2010/

Turn off Tags and Notes in SharePoint 2010

There are two ways to turn it off using the graphical interface :
At Farm Level :
  • Go to Central Administration > System Settings >  Manage Farm Features.
  • Look for Social Tags and Note Board Ribbon Controls and Deactivate it.
Farm Level deactivation of tags and notes
Farm Level deactivation of tags and notes
Ae User Group Level :
  • Go to Central Administration > Manage Profile Service: > User Profile Service Application > People
  • Click on Manage User Permissions and you will see a screen similar as below :
Permission for User Profile Service Application
Permission for User Profile Service Application
  • Here you can disable Use Social Features to turn off  Tags and Notes for that particular user, group or type.  As you can see here personal Feature, Personal Site and Social feature are three different things here

Hide / Remove Title or any column from Sharepoint List, Sharepoint 2010

(1)Go to Your list settings on which you want to hide/remove title column.
(2)Go to advanced settings of that list.
(3)Click on yes at very first option. Allow management of content types.
(4)Once you do this, one more setting panel becomes visible in advanced settings options. check out the figure below.

(5)Now click on that Item content Type.
(6)As you can see here you will find all columns are listed along with Title column.
(7)Click on that Title column. you will see a page similar to this.

(8)Select the last radio button which is Hidden.
(9)save all settings and then go back to list and click on new Item and see… now Title column is no more there….

Add Event Receivers to SharePoint 2010 Lists

Open the List Definition Project

In this task, you open the list definition project in Visual Studio 2010.

To open the list definition project

  1. Start Visual Studio 2010
  2. On the File menu, point to Open, and then click Project/Solution.
  3. Navigate to the list definition project file (Bugs.sln) and then click Open.

Add the Event Receiver Code

In this task, you create and attach an event receiver to the list definition. This event receiver prevents the deletion of bug items and displays a message.

To add the event receiver to the list definition

  1. In Solution Explorer, right-click the Bugs node, point to Add, and then click New Item.
  2. In the New Project dialog window, in the Installed Templates section, click Visual C#, click SharePoint, and then click 2010.
  3. From the project items, click Event Receiver.
  4. Type BugListItemEvent in the Name box, and then click Add.
  5. In the SharePoint Customization Wizard, select the An item is being deleted option, and then click Finish.
  6. In Solution Explorer, expand BugListItemEvent, and then open the Elements.xml file.
  7. To make sure that the event receiver only binds to the custom list, ensure the ListTemplateId attribute in the Receivers element is set to 10001.
  8. In Solution Explorer, expand BugListItemEvent and then open BugListItemEvent.cs. You should see the overridden ItemDeleting method.
  9. Insert the following code into the body of ItemDeleting method after the base.ItemDeleting(properties); statement. This code prevents the deletion of an item and display an error message.

    try
    { 
       properties.Cancel = true;
       properties.ErrorMessage = "Bugs can only be resolved not deleted!";
    }
    catch (Exception ex)
    {
       return;
    }
    finally
    {
       this.EventFiringEnabled = true;
    }
    
  10. In Solution Explorer, right-click the Bugs node and then click Deploy. If you get a deployment error message, click Resolve Automatically.
  11. Open the website you specified previously.
  12. On the Home page, in the left navigation pane, click the Bugs list.
  13. On the List Tools tab, click Items, and then on the New Item drop-down list, click Bug Item. The Bugs - New Item screen is displayed.
  14. To create a bug, type information into the applicable boxes, and then click Save.
  15. Next, try to delete the item. You should receive a message similar to the one shown in Figure 1.


    Figure 1. Error message triggered by trying to delete the item

    Error message when trying to delete

Test the Solution

In this task, you first create an item in the custom list and then trigger the event receiver by trying to delete the item.

To test the solution

  1. Open the website you specified in the list definition project.
  2. On the Home page, in the left navigation pane, click the Bugs list.
  3. On the List Tools tab, click Items, and then on the New Item drop-down list, click Bug Item. The Bugs - New Item screen is displayed.
  4. To create a bug, type information into the applicable boxes, and then click Save.
  5. Next, try to delete the item. You should receive a message similar to the one shown in Figure 1.
Source : http://msdn.microsoft.com/en-us/library/ff728093.aspx