Thursday, December 30, 2010

SharePoint 2010 how to get Social Comments Count using jQuery and webservice

Hi Everyone.This is my first blog post and I wanted to share some of my tough scenarios where I spent more time to find solutions. One of issue I spent more time was finding the number of comments for a page without customization (i.e., without doing managed code). Then I found some scripts which will fetch SharePoint 2010 social comments using webservice and jquery script. I hope it might help for some developers.

<script src="/sites/dev001Portal/styles/JS/jquery.SPServices-0.5.8P1.js" type="text/javascript"></script>
<script src="/sites/dev001Portal/styles/JS/jquery.min.js" type="text/javascript"></script>                                                                                            
 <script type='text/javascript'>
var strUrl = location.href;

CallCountMethod(strUrl);

function CallCountMethod(strUrl) {
    var CountMethod;
    var Counter;
   
        CountMethod = '/_vti_bin/SocialDataService.asmx?op=CountCommentsOnUrl';
            var Counter = "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' \
                    xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> \
                          <soap:Body>    \
                               <CountCommentsOnUrl xmlns='http://microsoft.com/webservices/SharePointPortalServer/SocialDataService'> \
                                <url>" + strUrl + "</url> \
                            </CountCommentsOnUrl> \
                        </soap:Body> \
                    </soap:Envelope>";
   

    $.ajax({
        type: "POST",
        async: true,
        url: CountMethod,
        data: Counter,
        contentType: "text/xml; charset=utf-8",
        dataType: "xml",
        success: function(content, txtFunc, XMLHttpRequest) {
                    GetCommentsCount(content, txtFunc, XMLHttpRequest, strUrl);
        }
    });
}

function GetCommentsCount(content, txtFunc, XMLHttpRequest, strUrl) {
     var count = 0;
     count = $('CountCommentsOnUrlResponse', data).find('CountCommentsOnUrlResult').text();
     document.getElementById('showCount').innerHTML = comb;
}
</script>

// Executing the above script will show the count into the below div tag.
<div id="showCount"></div>


=//\\Enjoy Programming//\\=

No comments:

Post a Comment