1

I have a SocialCommentControl in my PageLayout.

In my pageload there is a method to get the counts.

 protected void Page_Load(object sender, EventArgs e)
    {
        GetSocialCommentCount();
    }

In the method,

public void GetSocialCommentCount()
        {
            SPWeb web = SPContext.Current.Web;
            int id = Convert.ToInt32(this.ID.ItemFieldValue);
            SPList list = web.Lists.TryGetList("ListList");
            SPListItem item = list.GetItemById(id);
            string webUrl = web.Url;
            string dispUrl = item.ContentType.DisplayFormUrl;
            if (dispUrl == "")
                dispUrl = list.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url;
            bool isLayouts = dispUrl.StartsWith("_layouts/", StringComparison.CurrentCultureIgnoreCase);
            dispUrl = String.Format("{0}/{1}?ID={2}", webUrl, dispUrl, item.ID);
            if (isLayouts)
                dispUrl = String.Format("{0}&List={1}", dispUrl, SPEncode.UrlEncode(list.ID + ""));
            SocialCommentManager scm = new SocialCommentManager();
            Uri currUri = new Uri(dispUrl);
            var count = scm.GetCount(currUri);
            lblNumComments.Text = count + " comments";
        }

However the counts do not reflect correctly for the specific item. I have checked and the Uri correctly points to the selected view item. Does anybody have a clue?

1 Answer 1

0

Well I finally found the issue.

What the above code gives as URI links correctly to the display form for the relevant item in a list. However as i did not set my custom display form and actually just added the custom content type to the List of a created List in Portal.

The location where sharepoint stores the comments are tagged to the default DisplayForm aspx rather than custom(my custom is not set as default)

Hence if your list was named ListList where comments for a particular Item is tagged as : http://portal.conium.com/sites/test/Lists/ListList/DispForm.aspx?Id=4 while my code gives a url of http://portal.conium.com/sites/test/_layouts/15/testProjectName/DisplayForm.aspx?......(and so on and so forth with the crazy url)

So the problem was the formed Uri having a problem...

So modifying specific codes to get your correct Url. Heres my code for the url part:

string webUrl = currentWeb.Url;
//Pattern for regex to remove extra stuffs like /sites/yourSite
string pattern = "([.]com)";
String[] webUrlSplit = Regex.Split(webUrl, pattern);
//Web url will now be portal.UrDomain.com or portal.com accordingly to your domain
webUrl = webUrlSplit[0] + ".com";
//Get Your default Display Form Url its returns /sites/urSite/List/YourListName/DisplayFormName.aspx
string defaultDisplayFormName = item.ParentList.DefaultDisplayFormUrl;
//Substring it to remove extra / before sites I was lazy to change my above pattern but the pattern could just be ([.]com/)
// then you wouldn't need to substring this. Lazy me...
defaultDisplayFormName = defaultDisplayFormName.Substring(1);
string dispUrl;
dispUrl = String.Format("{0}/{1}?Id={2}", webUrl, defaultDisplayFormName, this.ID.ItemId);
HttpContext.Current = null; //Clear current context
var socialContextManager = new SocialCommentManager(spServiceContext);
var comments = socialContextManager.GetCount(new Uri(dispUrl));

Hope this helps anybody with the same issue. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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