Pages

Monday, October 22, 2012

Retrieve document rating with Powershell

Hi everyone,

I will show you another piece of code that I use during development.

In this case I need to get the rating of a document. In C# the code looks like this:

using (SPSite site = new SPSite("SharePoint site URL"))
{
SPServiceContext context = SPServiceContext.GetContext(site);
SocialRatingManager mySocialRatingManager = new SocialRatingManager(context);
SocialRating aRating = mySocialRatingManager.GetRating(myUri);
Console.WriteLine(aRating.Url + ": " + aRating.Rating);
}


(http://msdn.microsoft.com/en-us/library/ff407954.aspx)


To do the same with powershell we must do some changes. I show to you:


$siteUrl = Read-Host "Site url"
$site = Get-SPSite $siteUrl
$uri = Read-Host "Uri document"

$serviceCtx = Get-SPServiceContext -Site $site
$socialRatingMgr = New-Object Microsoft.Office.Server.SocialData.SocialRatingManager($serviceCtx)
$rating = $socialRatingMgr.GetRating($uri)

$rating | Format-Table -AutoSize -Wrap

$site.Dispose()


I want remember that I’m not a powershell guy but I like it to use for my general purpose Winking smile

No comments:

Post a Comment