Enable ratings in SharePoint with PowerShell and CSOM

In document libraries it is possible to enable ratings, both likes and average score (1-5). In this article I will cover how to enable “Likes” on the Pages library in a publishing site.

When configuring libraries manually, this is enabled from the “Rating settings” in the library:

enableratings

But when we follow the remote provisioning pattern to create new sites by using PowerShell and CSOM, there is currently no available function in the API for this. This pattern is the recommenced approach when working with SharePoint Online or SharePoint 2013/2016 without access to server side PowerShell or code.

To understand what is required to programmatic enable ratings I had to, as always with SharePoint, inspect the code behind with a reflection tool like DotPeek.

How to enable likes ratings

This show the procedure to enable ratings with Likes on a Pages library. To enable “Star rating” (1-5 average) this would be similar, but not covered in this post.

  1. Add the required fields to the library
    FieldName Guid
    AverageRating 5a14d1ab-1513-48c7-97b3-657a5ba6c742
    RatingCount b1996002-9167-45e5-a4df-b2c41c6723c7
    RatedBy 4D64B067-08C3-43DC-A87B-8B8E01673313
    Ratings 434F51FB-FFD2-4A0E-A03B-CA3131AC67BA
    LikesCount 6E4D832B-F610-41a8-B3E0-239608EFDA41
    LikedBy 2CDCD5EB-846D-4f4d-9AAF-73E8E73C7312
  2. Add “LikesCount” to default view
  3.  Add “Ratings_VotingExperience” to RootFolder’s property bag

Use PowerShell to enable ratings

Note: This function requires the Office Dev PnP PowerShell library to be installed and loaded in the current PowerShell session. I recommend using this library for working with PowerShell and SharePoint (both Online and On-Premises).

Example: How to use this function with a site

Using this with subsites within the Site Collection would require some extension to the function, as the Fields always are loaded form the RootWeb, but the library reside in the subsite.

When we have run the “Enable-CustomLikesRatingsOnLibrary” function on the desired library, we can see that Likes now are available:

ratings-library

Summary

Using this function it is now possible to provision new sites with PowerShell and CSOM and enable likes rating on libraries.

Add AD security group as Site Collection administrator with PowerShell in SharePoint

In provisioning solutions it can be a good practice to add a security group from Active Directory as an additional site collection owner. Unfortunately a security group is not accepted as either primary or secondary owner. So to set a group as site collection administrator, the easiest approach I could find was to set the “IsSiteAdmin” property on the User object.

Enabling scheduling on Publishing Pages in SharePoint Online (Office 365) using CSOM and PowerShell

Scheduling is easily enabled through the web interface when configuring the “Pages” library, but when deploying solutions using PowerShell, this must be automated as part of the configuration. Unfortunately, as many other sources also state, this is not directly supported in the Client Side API (CSOM).

The solution is to manually set up the Pages library the same way Microsoft does by adding two event receivers, changing some columns from hidden to visible and adding them to the default view.

Note: This example uses commands from the Office Dev PnP PowerShell library, ex. “Get-SPOContext”. I recommend using this library for working this PowerShell and SharePoint (both Online and On-Premises).

Function to enable Scheduling on a library on a given web site:

On line 11 an additional function is needed to create the parameters for the “Load” method:

How to enable scheduling:

Exporting XsltListViewWebPart in SharePoint 2013

By default the XsltListViewWebPart does not support export from the web user interface. I often build solutions by configuring the web parts in the browers, exporting and adjusting the defintions and then deploying them with PowerShell. With a few lines of PowerShell this capability can also be enabled for the Xslt List View Web Part.

Before enabling the Export Mode on the webpart:

XsltListViewWebPart-before

After running the PowerShell script and enabling the Export mode:

XsltListViewWebPart-after

PowerShell script

Unable to search for pages in SharePoint Online when using Managed Navigation

This is a summary of an issue we have had with publishing pages not being indexed, and then unable to build the search-driven pages we wanted using for example Content Search Web Part. This occured in SharePoint Online (Office 365) and we have only had this issue with a couple of tenants. So my guess is that most tenants are working fine, but if you should come across this strange behavior, I hope this post can help you save some time.

The issue

  • Created a Publishing Site
  • Added a Content Type and a Page Layout, created a few test articles
  • Added a Content Search Web Part and set up a query to return articles for the Content Type created above
  • Waited a long time (normal)
  • No results (oops)
  • Tried to reindex the library (curious)
  • No results (worried)
  • Retried all the steps (scared)
  • No results (desperate)
  • Got coffee (calm)
  • No results (called support)

I guess you get it. Something was very wrong and I was stuck on not getting any results back. On-Premises this would probable been one of those situations a full crawl, or at worst a index reset, had fixed it. But running in Office 365 we don’t have these abilities.

The workaround

After some trial and error I understood what was causing the issue, and got this workaround:

  • Go to “Site Settings”
  • Select “Navigation” under “Look and feel”
  • Uncheck these options:
    • Add new pages to navigation automatically
    • Create friendly URLs for new pages automatically

search-mn-error-1

All pages created and published after making this change was indexed and showed up as expected. Unfortunately all the pages created before doing this change was a lost cause. Believe me, I tried a lot but unable to get them indexed.

The fix

As being on Office 365 this issue was addressed with Microsoft Support, and after a while they released a fix.

Unfortunately I suspect this error can still occur in some scenarios, and to make sure we don’t run into this trouble again, a recommendation is to avoid using the Managed Navigation set up as shown above just to be safe.

Using BCS from PowerShell with SharePoint 2013

Running data migrations with SharePoint 2013 often involves using BCS and PowerShell if you don’t have and third party tools at hand. This can be a bit tricky to set up, but once you are familiar the the BCS configuration, the PowerShell scripts are mostly straight forward.

From my experience there are a few differences when running against BCS lists vs. regular lists. I have now been able to find a good guide for this, so from my own trial and error I wanted to share a few tips.

Get the context straight: Accessing the list gives a weird error

Running this:


$Web = Get-SPWeb "http://mybcsweb/"

$Web.GetList("/Lists/MyBcsDataList")

Gives this error:

format-default : The shim execution failed unexpectedly – Proxy creation failed. Default context not found..    + CategoryInfo          : NotSpecified: (:) [format-default], BdcException   + FullyQualifiedErrorId : Microsoft.BusinessData.Infrastructure.BdcException,Microsoft.PowerShell.Commands.FormatDefaultComman

bcs-powershell-sp2013-1

The fix:


$Site = Get-SPSite "http://mybcsweb"

$ServiceContext = Get-SPServiceContext -Site $Site # Required when working with BCS
$ServiceScope = New-Object Microsoft.SharePoint.SPServiceContextScope $ServiceContext # Required when working with BCS

$Web = $Site.RootWeb
$Web.GetList("/Lists/MyBcsDataList")

A bit more wiring is required to get the context straight for BCS to work.

Make sure you are running with administrator privileges

I use PowerShell ISE when creating and running these scripts. From time to time I accidently forget to start this with Administrator privileges. This can fool you into weird errors like this one. Short minded one can try to start adding the missing assembly references, but the real reason is the missing privileges.

Running this:

$Web = Get-SPWeb "http://mybcsweb/"
$Web.GetList("/Lists/MyBcsDataList")

Gives this error:

format-default : The shim execution failed unexpectedly – Could not load file or assembly ‘System.Data.OracleClient, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’ or one of its dependencies. Either a required impersonation level was not provided, or the provided impersonation level is invalid. (Exception from HRESULT: 0x80070542).    + CategoryInfo          : NotSpecified: (:) [format-default], BdcException  + FullyQualifiedErrorId : Microsoft.BusinessData.Infrastructure.BdcException,Microsoft.PowerShell.Commands.FormatDefaultCommand

bcs-powershell-sp2013-2

The fix:

Search for “PowerShell ISE”, right-click it and choose “Run as Administrator”.

bcs-powershell-sp2013-3

Include ViewFields in you CAML queries

Always remember to include the fields you are trying to query on in the “<Where>” statement within “<ViewFields>”. This isn’t required on regular lists, but to get any results in return, I experienced I was required to include them querying BCS lists.

Example of CAML query in PowerShell:


$Query= New-Object Microsoft.SharePoint.SPQuery 

$Query.ViewXml =
 "<View>
 <Method Name='Read List'/>
 <Query>
 <Where>
 <And>
 <Eq>
 <FieldRef Name='ProjectID' />
 <Value Type='Text'>100</Value>
 </Eq>
 <Eq>
 <FieldRef Name='Role' />
 <Value Type='Text'>Project Manager</Value>
 </Eq>
 </And>
 </Where>
 </Query>
 <ViewFields>
 <FieldRef Name='ProjectID'/>
 <FieldRef Name='Role'/>
 </ViewFields>
 </View>" 

 $Items = $List.GetItems($Query)

Summary

BCS has been around for a while and isn’t something very existing. But as mention in the introduction, using it for data migrations is very powerful, specially when you don’t have any 3. parts tools around. Doing this from PowerShell is pretty straight forward, but these three issues I experienced can be a headache if you get stuck with them. Hopefully this post can help others get quickly passed them!

Setting up a development environment for Office 365 and SharePoint Online

As we more and more evolve from working exclusively with SharePoint on-premises with our complex local SharePoint farm rigged with several virtual machines eating up our highly spec’d laptop, the new way of working with SharePoint in-cloud offers a more attractive setup for us developers.

My background before starting with SharePoint, as for most others, was as a general programmer used to pretty simple and lightweight development environments. I must say I almost had forgotten what a lightweight and clean development environment feels like. It feel like coming home after a long and tough journey😉

Setting up a development environment for doing Office 365 and SharePoint Online development is really easy, I that’s the reason I chose to create this post it just to keep a note to myself what the essentials are.

o365-dev-env-1

Where to run it

I recommend to set up a dedicated virtual machine for this setup. I prefer using Hyper-V as it is included in Windows 8, and for many other reasons.

Specification:

  • 4 cores (minimum 2)
  • 8 GB RAM or more (dynamic)
  • 80 GB disk (expanding)

Getting the necessary software

Operative system

  • Windows 8.1 x64 with Update

Software

  • Visual Studio 2013 Update 3 x86
  • Office 2013 SP1 x86
  • SharePoint Designer 2013 x86
  • Google Chrome (set as default)

SDKs

These SDKs are pre-requirements to getting started with development against Office 365. It is important to install them in the listed order to avoid errors.

Title

Description

URL

Office Developer Tools for Visual Studio 2013 – July 2014 update

This can also be installed from Web Platfrom installer.

http://msdn.microsoft.com/en-us/office/aa905340.aspx

Microsoft Online Services Sign-In Assistant for IT Professionals BETA

The Microsoft Online Services Sign-In Assistant provides end user sign-in capabilities to Microsoft Online Services, such as Office 365.

http://www.microsoft.com/en-my/download/details.aspx?id=39267

Azure Active Directory Module for Windows PowerShell

Use Windows PowerShell cmdlets to manage your Windows Azure tenant.

http://msdn.microsoft.com/en-us/library/azure/jj151815.aspx#bkmk_installmodule

SharePoint Online Management Shell

The SharePoint Online Management Shell is a tool that contains a Windows PowerShell Module to manage your SharePoint Online subscription in the Office 365. http://www.microsoft.com/en-us/download/details.aspx?id=35588
Azure SDK for .NET (OPTIONAL) Install this if you plan to develop provider hosted ASP.NET apps in Azure. http://azure.microsoft.com/en-us/downloads/ (VS 2013 install)

Development tools

 Going from on-premises development to cloud, first of all reduces the available tools. Luckily a few of those contributing with their excellent tools have also made them compatible with SharePoint Online. These tools I recommend to increase your productivity working with developing solutions for SharePoint Online. Spend some time to learn what you can gain from using them.

Title

Description

URL

CAML Designer 2013

http://www.camldesigner.com/

Fiddler

http://www.telerik.com/fiddler

SharePoint Client Browser

https://spcb.codeplex.com/

SharePoint Search Query Tool

https://sp2013searchtool.codeplex.com/

Utilities

There as some basic utilities I always find handy to have available on every computer I work on. So this is only a recommendation, and not necessary to get the environment working.

Setting it up

This is pretty a straight forward top-down installation. Only be careful to click away some of the ad-ware stuff on the utilities to avoid getting crazy toolbars in your not-default browser IE.

One important factor is to install the SDKs in the right order as listed. I had some trouble getting this to work, and it turned out installing SharePoint Online Management shell too early gave some bad consequences.

Troubleshooting

When I tried to install Azure Active Directory Module for Windows PowerShell it got an error:

“In order to install Windows Azure Active Directory Module for PowerShell, you must have Microsoft Online Services Sign-In Assistant version 7.0 or greater installed on this computer.”.

For the time being I had version 7.250.4303.0 installed, I this error message made no sense. It turns out there is a newer BETA version (7.250.4551.0) that is required. I have linked to the correct version above, so just make sure you are using it.

Summary

Getting started with developing for SharePoint Online in Office 365 is a much more pleasant journey than the traditional on-premises story. We are now able to be productive using a very lightweight machine with a small set of tool available to do our work.

I wrote this guide primarily for my own use I as tend to forget all those small details between each time I set up a new environment, but I also hope it can help others getting started as quick and hassle-free as possible with developing for SharePoint Online.

NOTE: This topic is always subject to change as new versions roll out all the time.