none
SocialRatingManager causing custom WCF server to send back Bad Request

    Question

  • I'm working on a custom WCF for SharePoint 2010 that I call through Javascript that grabs the ratings of all the users who rated a particular item. Since there is no way for a standard user to retrieve all ratings (outside of giving them Manage Social Data rights), I'm trying to do this part via elevated privledges. Of course, I can't use that in Javascript so that's why I have my custom web service. In any case, I've been messing around adding some code piece by piece because before when I put all my code in I couldn't figure out why I was getting a Bad Request sent back as the error thrown.

    When I finally added code to get the SocialRatingManager to make a call (GetRating()) I got the bad request sent back to me. I can't for the life of me figure out why!

    Below are code snippets, in case they help. Thanks for any help in advance. Let me know if I should provide anymore information.

    WCF C#:

    Interface:
    
     [ServiceContract]
        public interface ITestService
        {
            [OperationContract]
            [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
            int SetVotes(string url, string listName);
        }
    
    Class:
    
     public int SetVotes(string url, string listName)
            {
                int test = 60;
    
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    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);
                            var test2 = ratingManager.GetRating(uri);
                        }
                    }
                });
    
                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);
            }

    jQuery:

    function CallService2(opts, $obj, serviceData, callBack) {
            
            $.ajax({
                type: "GET",
                url: opts.SetVotesUrl,
                data: { url: serviceData.url, listName: serviceData.listName },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    callBack(msg, opts, $obj);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(errorThrown);
                }
            });
        };
    

    Tuesday, March 13, 2012 9:52 PM