1

The function below returns me the number of the comments on a url. However, if the page saved with a friendly url, then the result of the function is always "0"

Do you have any idea, how can I get the number of comments on friendy urls?

    private static string GetCommentsCount(SPListItem item)
    {
        var commentsCount = "0";
        if (item[Constants.PublishingPageAbsoluteUrlFieldName] != null)
        {
            var pageUrl = item[Constants.PublishingPageAbsoluteUrlFieldName].ToString();

            var serviceContext = SPServiceContext.Current;
            var socialCommentManager = new SocialCommentManager(serviceContext);
            var pageUri = new Uri(pageUrl);

            commentsCount = socialCommentManager.GetCount(pageUri).ToString(CultureInfo.InvariantCulture);
        }

        return commentsCount;
1

I found the solution using the TaxonomyNavigation.TryParseFriendlyUrl method in order to build the url.

    private static string GetCommentsCount(SPListItem item, string url)
    {
        var commentsCount = "0";

        if (item[Constants.PublishingPageAbsoluteUrlFieldName] != null)
        {
            // aspx page
            var pageUrl = item[Constants.PublishingPageAbsoluteUrlFieldName].ToString();

            // Friendly url
            if (!url.EndsWith(".aspx"))
            {
                NavigationTerm term;
                string[] catalogUrlSegments;

                TaxonomyNavigation.TryParseFriendlyUrl(SPContext.Current.Site, url, out term, out catalogUrlSegments);
                if (term != null)
                {
                    var termStoreId = term.TermSet.TermStoreId.ToString();
                    var termSetId = term.TermSet.Id.ToString();
                    var termId = term.Id.ToString();

                    pageUrl = string.Format("{0}?TermStoreId={1}&TermSetId={2}&TermId={3}", pageUrl, termStoreId, termSetId, termId);    
                }
            }

            var serviceContext = SPServiceContext.Current;
            var socialCommentManager = new SocialCommentManager(serviceContext);

            var pageUri = new Uri(pageUrl);

            commentsCount = socialCommentManager.GetCount(pageUri).ToString(CultureInfo.InvariantCulture);
        }

        return commentsCount;
    }
0

You shouldn't really save PublishingPageAbsoluteUrl, instead have a lookup to Pages Library.. So you can access all the fields of a Page..

Once you have the lookup, you get the ListItem for Page Library, and than you can easily get the Url..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.