UGC post content
Asked Answered
P

2

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

Paviour answered 29/1, 2013 at 10:43 Comment(4)
Why do you want to use a web service to post comments? in which API is the class WebServiceClient?Blackhead
Nice question, could we interest you in committing to the Area 51 Tridion specific proposal. Sign up with the same SO account if you have a moment.Linzer
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.Paviour
Hi Bart, already signed up. :)Paviour
O
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.

Olivia answered 29/1, 2013 at 11:15 Comment(1)
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.Paviour
V
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);
Viscose answered 29/1, 2013 at 12:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.