Second Life of a Hungarian SharePoint Geek

January 14, 2010

Using the SocialRatingManager class from a console application

Filed under: Social computing, SP 2010 — Tags: , — Peter Holpar @ 12:04

Today I worked with the SocialRatingManager class of SP 2010 to play with the new rating feature of the product. I’ve found a useful post about that on Yaroslav Pentsarskyy’s blog:

Programaticaly enable rating on SharePoint 2010 lists

When Yaroslav checks the existence of the fields on the list he uses the StaticName property of the SPField class, like fieldByField.StaticName. Based on my experience it should be the Title property, like fieldByField.Title. SPFieldCollection.ContainsField method expects either the display name or the internal name of the field (see the documentation), but I found that using the InternalName does not work as expected.

Yaroslav’s example is intended to be used as a feature receiver, but it is easy to modify to a console application.

In that case you should alter

SPList list = ((SPWeb)properties.Feature.Parent).Lists[listName];

to

SPList list = web.Lists[listName];

where you get the web reference through the usual using clauses:

using (SPSite site = new SPSite(url))
{
    using (SPWeb web = site.OpenWeb())
    {
       // include the main code block here
    }
}

The second minor modification is in the Repropagate method:

Since our code does not run in server context, instead of

SocialRatingManager ratingManager = new SocialRatingManager(SPServiceContext.Current);

you should use

SocialRatingManager ratingManager = new SocialRatingManager(list.ParentWeb.Site);

Thanks to Yaroslav for this post and the other ones on his blog! Keep sharing your experience!

Advertisements

2 Comments »

  1. Hi,

    SocialRatingManager expects SPServiceContext, so your code will not work.

    In your/Yaruslav’s sample, it is done correctly, once you have opened a connection to SharePoint in your using statement, the code block has context on that URL.

    Cheers
    Piet

    Comment by Piet — November 2, 2010 @ 08:45

    • Hi Piet,

      Thanks for your comment, you are absolutely right. Should be SocialRatingManager ratingManager = new SocialRatingManager(SPServiceContext.GetContext(list.ParentWeb.Site));

      Peter

      Comment by Peter Holpar — December 2, 2010 @ 22:22


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: