2

I need to tag a lot of pages with the tag I like it.

Is there any way of doing it programmatically? (i.e. PowerShell)

1

Reputation class from Microsoft.Office.Server.ReputationModel namespace (CSOM assembly Microsoft.SharePoint.Client.UserProfiles.dll) contains the following method:

public static ClientResult<int> SetLike(ClientRuntimeContext context, string listID, int itemID, bool like)

The following example demonstrates how to set Like for a List Item using SharePoint 2013/Online CSOM in PowerShell:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.UserProfiles")


Function Get-SPOCredentials([string]$UserName,[string]$Password)
{
    $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
    return New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
}


$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$ctx.Credentials = Get-SPOCredentials -UserName $UserName -Password $Password
$result = [Microsoft.Office.Server.ReputationModel.Reputation]::SetLike($ctx,$listId,$ItemId,$true)
$ctx.ExecuteQuery()
$ctx.Dispose()
1
  • 2
    First answer I've come across to clearly indicate that the "like" equivalent for managed CSOM resides in the Microsoft.SharePoint.Client.UserProfiles.dll assembly and not a "CSOM version" of Microsoft.Office.Server.ReputationModel. This isn't as obvious as it should be in the documentation. – nsturdivant Jan 2 '16 at 6:50
0

See the solution implemented in C# here.

You should either use the Reputation class:

Reputation.SetLike(listID, itemID, true);

or via updating the item’s LikesCount and LikedBy fields:

SPFieldUserValueCollection likedBy = new SPFieldUserValueCollection(web, item["LikedBy"].ToString());
SPUser user = web.EnsureUser("YOURDOMAIN\\USERNAME");
SPFieldUserValue newUser = new SPFieldUserValue(web, user.ID, user.Name);
likedBy.Add(newUser);
int likes = likedBy.Distinct().Count();
item["LikesCount"] = likes;
item["LikedBy"] = likedBy;item.SystemUpdate(false);

You can find another discussions of the same issue here and here.

The PowerShell equivalent of the first option should be:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.UserProfiles")
[Microsoft.Office.Server.ReputationModel.Reputation]::SetLike($listId, $itemId, $True)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.