Tuesday, December 13, 2011

Expand-collapsible FAQ for your Publishing Pages

I have come across an expand-collapsible FAQs requirement as part of publishing pages a few times. While there are many ways to achieve this including a custom field or custom control/web part accessing external list, in my most recent project, I thought about and implemented a much simplified solution. The solution looks like:

1. Have a FAQ field of type: “Full HTML with Formatting and Constraints of Publishing”

2. Provide custom two custom styles to choose from: Question and Answer

3. In display mode, hide the Answers using jQuery when the page loads

4. In display mode, attach click event on question to toggle the answer

5. And finally, provide required training to your content authors so that they can author FAQs on publishing pages.

In following sections, I will expand the steps above and provide necessary technical details.

I am assuming that you have a column of type “Full HTML with Formatting and Constraints of Publishing” added to your Publishing Page Content Type and corresponding Page Layout has been provisioned to Master Page Gallery.

We need two CSS files provisioned (preferably) in “Style Library”.

FAQDisply.css

SNAGHTML2b1380

FAQEdit.css


SNAGHTML2c3c64 


Note: Feel free to use different styles but make sure that you follow the naming convention of styles as required by the Rich HTML editor, failing to do so will NOT make the styles available to you in the editor toolbar.


Also make sure that in your page layout, above CSS files are included in “PlaceHolderAdditionalPageHead” content placeholder. FAQEdit should be included only in edit mode and it should be referred after FAQDsply.css file as shown below:


image


Now, let’s look at the required jQuery code (assuming jQuery library is already plugged in your Page Layout/Master Page). The code is commented below for an understanding:


SNAGHTML3a3ed4





Once above infrastructure is in place, go ahead and create a new publishing page using your Page Layout. If everything is working fine, you should see Two styles available when you edit the rich HTML field as shown below:


SNAGHTML416671


Typing Question/Answer and choosing appropriate styles may look like below. Note that answer is not hidden in Edit mode because 1) our Jquery does not execute in edit mode and 2) In edit mode, we are making sure that the DIV tag containing the answer has display=block style.


SNAGHTML49787a


When you view the page in Display Mode, the answer will not be displayed until you click on the answer.


Hope this helps you implement similar requirements…

Thursday, September 22, 2011

Quickly Testing if JQuery is referenced properly on your page

I have observed a lots of developers quickly test the jQuery or SPServices references by putting a script like and make sure it works:

<script type="text/javascript" language="javascript">

$(document).ready(function() {
alert("jQuery");
alert($().SPServices.SPGetCurrentSite());
});
</script>

While this approach works, it requires to put the actual script on the page, only to be removed later.

There is a better way to check it instead of actually writing script on the page. Thanks to the IE Developer Toolbar. Here are the quick steps:

1. Reference your jQuery (and SPServices) in your page

2. Open your page in Internet Explorer on which IE Developer toolbar is installed. Note that you have IE 8 and above, it comes with it by default

3. Hit F12 to activate it the IE Developer Toolbar

4. Click on Script Tab, the beauty of IE Developer toolbar is that it allows you to run Ad-hoc JavaScript on your page as shown below:

SNAGHTMLaadd08

To make sure jQuery is referenced properly and it is functional on your page is to type a simple jQuery function like alert($(‘title’).html()). If it displays browser title like shown below, you are all set with the wonder of jQuery!

image

Many developers use SPServices library for some wonderful enhancements to the Forms (and for everything else it provides, of course). To test integration of SPServices (and jQuery as well because SPServices depend on jQuery), you may use similar approach but a different function to test and here it is:

alert($().SPServices.SPGetCurrentSite());

Hope this helps!

Wednesday, September 7, 2011

The Extensible and Reusable FEATURE Framework

As we all know there are two methods to develop/deploy various artifacts using SharePoint FEATURES : Declaratively using Feature.xml and element files and Programmatically.

Most examples that use programming approach either directly writes code in Feature Activated, Feature Deactivating etc. or use helper methods to promote the reusability of the code. While the second approach ensures reusability, it is not extensible, meaning, you must change code to change the actions that you want to perform in the Feature Receiver class. In my recent SharePoint implementation, I came across the need for extensibility and ended up writing a mini framework for the same.

Following are the elements of the framework and how they are connected to each other:

1. IFeatureAction class which has one method with following signature:

void Execute(object target, XmlElement actionData)

2. A Class named FeatureActionReceiver which is derived from SPFeatureReceiver. Overrides FeatureActivated and FeatureDeactivating methods.

3. The overriden FeatureActivated method looks for FeatureActivated.xml file under {Feature Root}\Actions directory and processes the file (more on processing later). Similarly, FeatureDeactivating looks for FeatureDeactivating.xml file under {Feature Root}\Actions directory

4. The Actions file (FeatureActivated.xml or FeatureDeactivating.xml) contains one of more Actions in following format:

<Actions>

<Action ActionName=”” Class=”” Assembly=””>

<AnyValidXML>

</Action>

<Action ActionName=”” Class=”” Assembly=””>

</Action>

</Actions>

4. Action Processing: the framework class reads the action one after another and for each action, it loads the specified Assembly and Specified class which must implement IFeatureAction class. After creating instance of the Action, it gives an opportunity of providing the target object (on which action needs to be performed) to the parent FeatureReceiver class by calling a virtual method “ProvideTargetObject”. The framework passes action name so that the feature’s receiver class can write a switch statement and provide appropriate target object to the framework. If no object is provided by Feature’s Receiver class, by default, framework will pass Feature’s Parent (depending on scope of feature) to the Execute method of the Action.

5. Now your Feature’s Receiver class must derive from FeatureActionReceiver  instead of SPFeatureReceiver and override FeatureActivated and FeatureDeactivating methods. Here, the Feature’s receiver must call Base.FeatureActivated and Base.FeatureDeactivating methods so that framework can process those actions. Apart from that, as usual other code can be written in the Feature Receiver.

Now, everything that I want to do in Feature Receiver is externalized in a set of Action Classes and those Action classes must get the parameters from XML file (the parameters varies from one project to another) while performing the actions. So that’s about reusability. In addition, performing a new action means writing a new action (if existing action does not satisfy the need, of course) and updating those XML files so that the action will be executed in FeatureActivated or FeatureDeactivating. That’s extensible!!

To give you a few examples, I developed following actions:

1. Create List – It would create list instance, add content types, set other properties, set the views and add fields to the view

2. Create Terms – Accepts a set of Term Groups, Term Sets and a hierarchical Terms data. This will ensure that all terms and term sets required by your application are provisioned using Features

3. Set Master Page – Sets the specified master page for the specified web

4. Create Web – creates the webs using specified Title and Web template

5. Add Feature – Activates a Feature. In my case, it was required to activate some features after creating the web so that my content authors do not need to perform the action

.. the list goes on and on..

I hope all dots are connected. Unfortunately, I cannot share the code here but let me know if you need more information about it.

Wednesday, July 13, 2011

Know CAML Queries executed by SharePoint server–CAML Profiling

Did you ever want to know what CAML queries are executed by SharePoint Server?

Well, for troubleshooting and learning purpose, it is not a bad idea. After all, SQL Profiler has been helping us troubleshoot a lot of issues.

There may be products out there but I figured out a way to do it without spending extra bucks! And here it is…

Following facts helped me think about the solution:

1) SharePoint’s code does a very good job at logging everything it does and everything it encounters (if you are lucky enough to figure it out). The challenge is to figure out under what area and category CAML queries are logged (if at all being logged). Once this is figured out, watching it real time is easy, see next step

2) The ULS Viewer is the tool which allows you to filter the events that you want to watch and watch them real time as they are logged to ULS logs by SharePoint code.

Follow the steps below to see it in action:

1. Go to Central Administration-> Monitoring –> Configure Diagnostic Logging.

Expand the SharePoint Foundation Area and check Monitoring and select Verbose level (selecting anything else would not work)

SNAGHTML924829

image

2. I did IIS Reset after changing the setting. Not sure if that is required though.

3. Download and run ULS Viewer. Select ULS Logs as your data source as shown below:

SNAGHTML98d1a3

4. Now modify the Filter (Access it from Edit –> Modify Filter) to filter messages that contains the CAML word as shown below:

image

And that should be it! Go to any Lists and select a view, since views use CAML behind the scene, you should see and entry and watch the CAML that was executed.

Tuesday, July 12, 2011

Locate SharePoint FEATURE from Title

Many times, I wanted to know elements of a particular FEATURE (any scope) before I hit the Activate button so that I can inspect the elements manifests and understand what the FEATURE would do to the site (or site collection, farm or web application).

SNAGHTML116d43

And I figured out that is no straightforward way to locate the FEATURE on the SharePoint Root. I wish Microsoft had provided a link “Open FEATURE folder” on click of which, it opens the FEATURE’s root directory for me…. At least for the Site Collection Administrators?

Anyways, I needed it desperately, so I had to help myself. I also posted a question on SharePoint Overflow and see if someone has already done that before. I wanted to do it with minimum efforts and finally, came up with following simple PowerShell Script;

Explorer (Get-SPFeature | Where-Object {$_.GetTitle([System.Globalization.CultureInfo]::CurrentCulture) -eq "Content Organizer"}).RootDirectory
What it does is retrieves the FEATURE’s list and then applies filter to select FEATURE which matches my Title. Getting Title was tricky but not as much as you can see above. And finally, I wanted to save few more clicks and keyboard strokes, so I used “Explorer” command and opened the directory in  Windows Explorer!
Hope this helps….

Wednesday, May 25, 2011

Small Business Server 2011–I implemented

Recently I got an opportunity to provide a server solution to a local small business in NJ. They are into trucking business and the business owner wanted to replace his old Dell PowerEdge server. When I visited the office, I instantly thought that the server is being replaced because it was making so much noise Smile

Anyways, I grabbed the opportunity with both hands and started looking towards small business server because the name itself suggests that they are made for small businesses like that (just like the Windows Home Server made for my home and no doubt I love it… I have not migrated to Windows Home Server 2011 yet). All they wanted was a domain controller and a file share. But when I started looking into Small Business Server’s feature set, I thought of making a list of things they would get and I did exactly so. And you know what.. it was not that difficult to sell it. I proposed the cost of hardware, SBS 2011 and the client licenses and it was accepted as-is. After all, who does not like 15 features when only 2 were expected. The small business was excited about emails for each individual and the remote access and not to mention access to emails on their smart phones….

I tried to order SBS 2011 online but realized that HP, DELL etc are still a few weeks or months away from shipping it. So I thought of building it and I ended up putting a huge hardware order at my favorite site: newegg.com. I also thought of making a RAID 5 array. I ordered 3 hard drives and configured RAID 5. It did not take much to realize that RAID 5 was very slow.. so finally I ordered the 4th drive and configured a RAID 10 solution on it. This was the first time I configured RAID feature. I did not order a dedicated controller because the on-board controller was giving acceptable performance. I am still surprised at the price of the dedicated controllers though.. they are too pricey!

I spent at least 2 weeks (part time, of course) to build the server and install SBS 2011 on it. The installation of OS was – as usual – very smooth and post installation was a bridge too. Well done Microsoft! I could not test the email functionality at my home though because it needed a dedicated domain registered with godaddy.

The small business had already registered a domain with godaddy, so connecting SBS to internet and configuring domain was not that difficult after I moved the server to the small business location. In the mean time, I suggested them to get a static IP from their ISP and that was already done!

So the end result is that the deployment was successful, I had all their client computers connected to the SBS 2011, their file shares moved to SBS 2011, each employee now had their own email address and guess what, they were able to send and receive both internal and external emails… Emails was most satisfying (apart from the payment from the small business) to me though.

I wish a few more small businesses contact me soon Smile….

My next project is to upgrade to Home Server 2011. I am little reluctant at this moment because I know I will not have the drive extender functionality anymore. But then now that I know RAID, I want to check if my motherboard supports RAID configuration so that the decision to move to Home Server 2011 is easy!