Navigation Bar

Thursday 20 December 2012

Hiding SharePoint 2010 Blog Comments

The OOTB Blog provides commenting feature. One of my client asked me to remove the commenting feature from Blog.

Below is snapshot of OOTB SharePoint 2010 Blog:



If you have similar requirement then this post will help you. To hide comments, you have do following:

1. You have to remove Comments Listing web-part (that is easy!!!)
2. You have to remove Comments Form web-part (that is easy!!!)
3. Hiding comments links (this is one of the requirement from my client) using jQuery/javascript OR blog.xsl

Before writing javascript, I did some research/investigation that how comments are rendered. I found an blog.xsl located at /_layouts/XSL/blog.xsl

I don't want to touch the OOTB blog.xsl as it would apply to every site collection. So I decided to write javascript function to hide comments.


















From above picture, you can see the div for comments link. I need to find that div and find the last SPAN which contains comments link.

I googled and found the javascript to get that elements by class. Once I got that div, I iterated and scan the word 'comments' and made that span hidden.

Below code will grab the div element for class 'ms-PostFooter'
function getElementsByClass(theClass) {
            var allPageTags = new Array();
            allPageTags = document.getElementsByTagName("*");

            var Elements = new Array();
            var n = 0;
            for (i = 0; i < allPageTags.length; i++) {
                if (allPageTags[i].className == theClass) {
                    Elements[n] = allPageTags[i];
                    n++;
                }
            }

            return Elements;
        }


If you pay attention to above html source code, you will find there are two divs for class = 'ms-PostFooter', so we need to skip the first one and work on second div.

function hideComments() {
 var desiredElements = getElementsByClass('ms-PostFooter');            
 if (desiredElements.length > 0) {  
  for (var i = 0; i < desiredElements.length; i++) {
   var desiredElement = desiredElements[i];   
   if (desiredElement.innerHTML.indexOf('Comment(s)', 0) >= 0) {    
    var spans = desiredElement.getElementsByTagName("SPAN");
    for (var j = 0; j < spans.length; j++) {
     if( (spans[j].innerHTML.indexOf('comment(s)', 0) >= 0) ||
       (spans[j].innerHTML.indexOf('Comment(s)', 0) >= 0) ) {
      spans[j].style.visibility = 'hidden';
     }
     
     if( spans[j].innerHTML.indexOf('Number of Comments', 0) >= 0 )  {
      spans[j].style.visibility = 'hidden';
     }
    }
   }
  }
 } 
}  


Now, you have got codes. Copy all javascript code to a file and place it to /_layouts/YourProjectName/Js/BlogCommentsHider.js Now add a content editor web-part on page and write below markup:

<script src="/_layouts/YourProjectName/Js/BlogCommentsHider.js" type="text/javascript"></script><script language="javascript">


_spBodyOnLoadFunctionNames.push("hideComments");</script>


That's it.

Monday 10 December 2012

Approval worfklow showing "System Account" in "Modified By" field


As you may know there is known issue or product feature that when you use OOTB Approval workflow, it publishes the item but leaves Modified by as System Account (http://sharepoint.microsoft.com/blog/Pages/BlogPost.aspx?PageType=4&ListId=%7b72C1C85B-1D2D-4A4A-90DE-CA74A7808184%7d&pID=865 ).

I am writing this blog 1) to save time of my other developer fellows as I have spent lot of time to figure out the issue 2) to help myself for future reference and my other fellows who are still struggling to find out the solution.

You may find below solution when you google it:

1.       Adding another column to track Modified By and displaying in all views

2.       Adding hidden column and call UpdateOverwriteVersion to overwrite when SharePoint Approval workflow updates the item

3.       Update OOTB Approval workflow (tried as well but having workflow publishing issue through SPD)

I have tried option #1 and it works fine, but our client was not happy with that solution. Then we implemented option #2, during testing we observed that after calling UpdateOverwriteVersion it increments by 0.1 version. So this solution is not going to work L

I started analysing “Approval – SharePoint 2010” OOTB workflow and I found that the workflow updates the content approval status using workflow author and that is leaving the “Modified By” field with System Account. Please see below snapshot of OOTB Approval workflow:


[Figure 1]

After analysis, I created a workflow association with below configuration:

 
[Figure 2]




[Figure 3]

When creating association, I did not select options (i.e. Start this workflow to approve publishing a major version of an item” and “Enable Content Approval”) and tested manually and everything worked as expected (i.e. not getting System Account in Modified By field).

So, I wrote an event receiver to publish item through code when the outcome of workflow is approved otherwise rejected.

So I came up with solution #4 that is given below:

1.       Create workflow association as shown in Figure 1 and Figure 2.
2.     An Event Receiver that kicks off the workflow (as I had a requirment to kick approval  process ONLY for some documents). If you don't have this kind of requirement then you don't need this even receiver.
 
3.       Write an event receiver (SPWorkflowEventReceiver) for workflow completed and publish item (a sample of code is posted below as image L)
 
 
 
 
 
 
 
 
 
NOTE: The properties contains information about Workflow History item.

You can download source code from https://approvalworkflowfix.codeplex.com/
Hope this post will help you.