With the release of SharePoint 2013 the SP team has chosen to promote ”Share” in line with the other social features. Share as in share list items. Share as in share it with other users within SharePoint. So- what does Share mean for the twirled mind of a SharePoint specialist?

Even though Share is a new terminology there’s not really that much new behind the curtain. Share is the new way of saying ”manage permissions”. However, I support the effort in trying to make it more user friendly and easy to use for the average user.

Assigning unique permissions to an item, or Sharing, has always had a bit of a sourish flavor to it. It’s an easy way to share the content with other users but there’s also a couple of challenges in its surge.

One of the biggest challenges rest in governance. It won’t be long before you start to ponder which items that are shared with whom. You will have to check each item or file in order to get an overview making it almost impossible to keep track of it all.

Wouldn’t it be nice if we could see what files that have unique permissions in a list view instead of having to check each file?

One of the new features in SharePoint 2013 is a concept called JSLink. JSLink is a way of customizing the rendering of a number of different objects using JavaScript. We can use JSLink and a rendering template, a script, to customize a view, an edit form and a lot of other things throughout SharePoint.

With a bit of magic we can use JSLink and a custom rendering template to present information about which list items that have unique permissions, in other words are Shared.

The idea consists of two parts.

  • A rendering template. The rendering template is a JavaScript file that we can store in the physical layouts folder or within a library in SharePoint, like the style or asset library.
  • A field, column, to override. The rendering template need a field to override. We could use one of the existing once, like the title field, but in this example I will use a unique field.

Rendering template

The rendering template is not that complicated. The challenge is that we need to look at a specific property called HasUniqueRoleAssignments. This property, that we need, isn’t presented in the default set of properties provided to a list view. The property is calculated by SharePoint, making it ”performance expensive”. For that reason it makes sense for the SharePoint team not to include it in the default set.

Fortunately there are other ways of collecting the information we need. Using JavaScript and REST or the SharePoint client API (CSOM). I choose to use REST in this case but you could do the same with CSOM if you prefer.

// Declare a unique namespace.
var sitesSharing = sitesSharing || {};
 
// Customize the field rendering of the Sharing field.
sitesSharing.FieldRenderSetup = function () {
 
    var overrideField = {};
    overrideField.Templates = {};
    overrideField.Templates.Fields =
    {
        'Sharing': { 'View': sitesSharing.SharingFieldOverride }
    };
 
    // Register the template.
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideField);
};
 
// Override the item rendering.
sitesSharing.SharingFieldOverride = function (ctx) {
 
    // Put together the REST endpoint using the url, list id and the id of the current item.
    var url = _spPageContextInfo.webServerRelativeUrl + '_api/web/lists(\'' + _spPageContextInfo.pageListId + '\')/items(' + ctx.CurrentItem['ID'] + ')/hasuniqueroleassignments';
 
    var placeholderId = ctx.CurrentItem.ID;
 
    // Make the AJAX call.
    $.ajax({
        url: url,
        type: 'GET',
        headers: { 'Accept': 'application/json;odata=verbose' },
        success: function (result) {
            if (result.d.HasUniqueRoleAssignments) {
                // Render the icon in the DIV placeholder if the item has unique role assignments.
		$('#pSitesSharing' + placeholderId).html('<img title="Shared" class="js-sharingHint-sharedDefault" src="/_layouts/15/images/spcommon.png?rev=23">');
            }          
        }
    });
 
    // Return the placeholder for the async AJAX call.
    return '<div id="pSitesSharing' + placeholderId + '" class="js-sharingHint-iconContainer"/>';
}
 
// Fire away! 
$(document).ready(sitesSharing.FieldRenderSetup());

Save the file as a JavaScript file (.js) and upload it to the Style Library or store it in the 15-hive on the SharePoint server (s).

Please note that I use JQuery and Ajax in the template, more on that later.

Field to override

The second thing we need is a field, a site column, to override, to display the information to the end user. We could use one of the existing fields, like the icon or maybe title and append the permission information. However, for the simplicity of this demo I will use a new and unique field, called ”Sharing”.

I use JQuery and Ajax in my rendering template. I therefore need to reference two additional JS files, beside my template. You could do this in a delegate control or similar but for the sake of this demo I will use the JSLink file to reference these two files as well.

As you can see I use the field’s JSLink property in order to reference the three JavaScript files I need. I have also chosen to hide the field in any edit, new or display forms.

Add-PSSnapin Microsoft.SharePoint.PowerShell
 
Write-Host 'This script will create a field called Sharing.'
 
$WebUrl = $(Read-Host -prompt 'Please enter the url of the site collection')
 
# Get the web.
$Web = Get-SPWeb $WebUrl
 
# Declare the field and its properties using xml.
$FieldXml = '<Field 
Type ="Text" 
DisplayName="Sharing" 
ShowInDisplayForm="FALSE"
ShowInEditForm="FALSE"
ShowInListSettings="TRUE"
ShowInNewForm="FALSE"
JSLink="~sitecollection/Style Library/MicrosoftAjax.js|~sitecollection/Style Library/jquery-1.7.2.min.js|~sitecollection/Style Library/Sharing_field.js" 
/>'
 
# Add the field.
$Field = $Web.Fields.AddFieldAsXml($FieldXml)
 
$Web.Update()
 
# Clean up.
$Web.Dispose()
 
Write-Host 'Done!' -ForegroundColor Green

The result

Putting it all together will give you a site column that you can use and re-use in your lists. Add the Sharing column to any list and include the field in a view to see the result.

Sharing, SharePoint, REST, JSLink

You may recognize the column label and icon from personal sites, where the SharePoint team has decide to provide this information out of box.

I have gathered the rendering template and two small PowerShell scripts in a small package that may help you on your way. You can download it here.

Good luck!

Please note! Adding the rendering template above may affect the performance of your SharePoint farm in a negative way. Use it with common sense and the performance aspect in mind.