How to debug 'Bad Request' from WCF called in JavaScript?

Mar 14, 2012 at 7:49 PM

I've been doing some EXTENSIVE research into the Ratings Service to try to implement a real-time Up/Down voting system. I found a good article that started me off (I'll post later if anyone wants), but I ran into some issues considering that the ratings are updated through a timer job. In order to combat that, we figured the best way was to grab all the ratings on the URL (GetRatingsOnUrl) which doesn't rely on the service and counting all ratings of 5 (which would be cast from our up arrow) and post the total into a column of the item the user is rating.

My first big hurdle came when I figured out that some of the SocialDataService calls require elevated privelges that we aren't willing to give all users. To combat this, I thought the best solution would be to create a seperate WCF service that would Elevate the user's right and do all the work, then leave. I wasn't able to successfully create a custom SharePoint WCF (that's called through Javascript) without using the CKS method. However, I'm encountering the strangest error:

When I call certain methods from the SocialRatingsManager, I get a "Bad Request" sent back from the Javascript. Other times when I use a method, it returns the correct value. Let me show you my WCF code (beware, it's very "Hello World"-y):

SVC File:

<%@ ServiceHost Language="C#" Debug="true"
    Service="TestWCF.TestService, $SharePoint.Project.AssemblyFullName$"  
    Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>


using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Web;

namespace TestWCF
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "ITestService" in both code and config file together.
    public interface ITestService
        [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        int SetVotes(string url, string listName);


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Services;
using System.Web;
using Microsoft.SharePoint.Client.Services;
using System.ServiceModel.Activation;
using Microsoft.SharePoint;
using Microsoft.Office.Server.SocialData;
using Microsoft.Office.Server.UserProfiles;

namespace TestWCF
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class TestService : ITestService
        // To test this service, use the Visual Studio WCF Test client
        // set the endpoint to http://<Your server name>/_vti_bin/TestWCF/TestService.svc/m
        public int SetVotes(string url, string listName)
            int test = 60;

                using (SPSite site = new SPSite(url))
                    using (SPWeb ElevatedWeb = site.OpenWeb())
                        var item = ElevatedWeb.Lists[listName].Items.GetItemById(GetItemID(url));
                        test = Convert.ToInt32(item["Votes"]);

                        var serviceContext = SPServiceContext.GetContext(site);
                        var uri = new Uri(url);
                        var ratingManager = new SocialRatingManager(serviceContext);
                        ElevatedWeb.AllowUnsafeUpdates = true;
                        test = ratingManager.GetCount(uri);
                        ElevatedWeb.AllowUnsafeUpdates = false;

            return test;

        private int GetItemID(string url) 
            int index = url.IndexOf("?");
            if (index > 0)
                url = url.Substring(index).Remove(0, 1);

            var node = HttpUtility.ParseQueryString(url).Get("ID");

            return Convert.ToInt32(node);





I'm not looking for a straight answer for this issue that I provided extremely limited information on, I'd just like to know how I'd go about debugging this situation! If anyone could provide some insight into that for me I'd appreciate it a lot.