7

I'm just trying to run a little prototype that posts UGC comments using the UGC web service.

The code example is below. I'm getting a 403 response from the web service which indicates I'm not authorised to use the service so I presume I need to create an authentication header? Does anybody have any examples of how to post comments using the UGC web service?

string ugcData = "{d\":{\"Content\":\"FROM WEB SERVICE\",\"Status\":2,\"ItemPublicationId\":\"68\",\"ItemId\":\"17805\",\"ItemType\":\"16\",\"Id\":0,\"ModeratedDate\":\"\",\"LastModifiedDate\":\"\",\"CreationDate\":\"\",\"Score\":0,\"Moderator\":\"\",\"User\":{\"Id\":\"DOMAIN%5Cbsmith\",\"Name\":\"Bill Smith\"}\"}";

WebServiceClient ugcCall = new WebServiceClient();

ugcCall.UploadString("/PostData", "POST", ugcData);

MTIA.

John

Daniel Neagu
  • 1,711
  • 11
  • 13
John
  • 271
  • 1
  • 5
  • Why do you want to use a web service to post comments? in which API is the class WebServiceClient? – Asier Fernández Jan 29 '13 at 11:11
  • Nice question, could we interest you in committing to the [Area 51 Tridion specific proposal](http://area51.stackexchange.com/proposals/38335/tridion?referrer=gPujQMxthNCNn9xqeeO2NA2). Sign up with the same SO account if you have a moment. – Bart Koopman Jan 29 '13 at 11:44
  • Hi Asier, we're using DD4T for this project so can't use the standard web controls. WebServiceClient is part of the UGC web service. – John Jan 29 '13 at 11:49
  • Hi Bart, already signed up. :) – John Jan 29 '13 at 11:50

2 Answers2

9

You should try to post on the Comments collection:

ugcCall.UploadString("/Comments", "POST", ugcData);

Then you will see that you're missing the CreationDate, moment in which you need to add to your entity something like:

\"CreationDate\":\"/Date(1359457694472)\"

(I have not actually checked if you need more quotes in there). For the format of the date in a JSON string check the odata specs.

If you still have problems, try to change DOMAIN%5Cbsmith to another dummy value ('test' for example).

If that is not enough then maybe you can look at the logs generated by the UGC WebService and try to make-out some stack-trace.

One more thing to notice here: the UGC properties need to be defined correctly in the Web.config in order for the post to even happen.

Hope this helps.

Daniel Neagu
  • 1,711
  • 11
  • 13
  • Hi Daniel, thanks for the pointers. Your "Comments" suggestion rather than "PostData" got it working. I left the creation date as blank as it seems to default to the date anyway. Once I changed it to "Comments" the logs led me to a syntax error in the JSON string. This was corrected and it's now posting from comments from the web service. Thanks again. – John Jan 29 '13 at 12:44
7

I've used the same approach as you are following, namely using a generated proxy for the UGC web service. To create the correct json we used the standard .NET JavaScriptSerializer. This makes the code a bit easier to read, I think.

Here is a code snippet, maybe it helps a bit. Of course you need to make sure the variables are set.

 WSR_ContentDelivery.User user = new WSR_ContentDelivery.User
 {
        Id = GetUserId(),
        Name = username,
        EmailAddress = email,
        ExternalId = website
 };

 WSR_ContentDelivery.Comment comment = new WSR_ContentDelivery.Comment
 {
        CreationDate = DateTime.UtcNow,
        LastModifiedDate = DateTime.UtcNow,
        ItemPublicationId = tcmUri.PublicationId,
        ItemId = tcmUri.ItemId,
        ItemType = tcmUri.ItemTypeId,
        Content = content,
        User = user,
        Status = Statuses.SubmittedNeedsModeration,
        Score = 0
 };

 JavaScriptSerializer serializer = new JavaScriptSerializer();
 return WSClient.UploadString("/Comments", "POST", "{d:" + 
        serializer.Serialize(comment) + "}", user.Id);
Quirijn
  • 3,529
  • 12
  • 28