Question

I'm trying to use the Reputation.js setLike method in a sharepoint hosted app/add-in which in theory is possible with the following snippet:

var ctx = new SP.ClientContext(webAppUrl);
Microsoft.Office.Server.ReputationModel.Reputation.setLike(ctx, "73eaae6a-9839-43bd-b509-ddc64989536a", "40", true);
ctx.executeQueryAsync(
    //Success
    Function.createDelegate(this, function () {
        alert("Everything OK!");
    }),
    //Failed to execute, log this in console.
    Function.createDelegate(this, function (e, args) {
        alert("Failed! " + args.get_message());
    })
);

As a result I get an alert with "The assembly Microsoft.SharePoint.Portal.Proxy, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c does not support app authentication."

Is it not possible to Like/Unlike through JSOM? Any pointers or other ways to do this in an app?

Was it helpful?

Solution

At last I managed to solve my problem by creating and use a custom WCF service to call the Reputation.SetLike method. Not really the way I would have preferred but in lack of alternatives it was the fastest way forward.

IProxyService.cs

[ServiceContract]
interface IProxyService
{
    [OperationContract]
    [WebInvoke(Method="POST",
        UriTemplate = "LikeArticle?url={url}&listid={listid}&listitemid={listitemid}&like={like}",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Bare)]
    String LikeArticle(string url, string listid, int listitemid, bool like);
}

ProxyService.svc.cs

public String LikeArticle(string url, string listid, int listitemid, bool like)
    {
        int likes = -1;
        using (var site = new SPSite(url)) {
            using (var web = site.OpenWeb())
            {
                using (new SPContextFaker(HttpContext.Current, SPContext.GetContext(web)))
                {
                    likes = Reputation.SetLike(listid, listitemid, like);
                }
            }
        }

        return ""+likes;
    }

SPContextFaker.cs

public class SPContextFaker : IDisposable
{
    private SPContext _oldSPContext;
    private HttpContext httpContext;
    public SPContextFaker(HttpContext context, SPContext newSPContext)
    {
        _oldSPContext = (SPContext)context.Items["DefaultSPContext"];
        context.Items["DefaultSPContext"] = newSPContext;
        httpContext = context;
    }
    public void Dispose()
    {
        httpContext.Items["DefaultSPContext"] = _oldSPContext;
    }
}

I merged the following two resources in order to create the solution above:

Programmatically like a blog post with Reputation.SetLike Sharepoint 2013

SharePoint 2013: Create a Custom WCF REST Service Hosted in SharePoint and Deployed in a WSP

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top