вторник, 13 октября 2015 г.

Display SharePoint list item or folder permissions in a list view

It is a common scenario that users need to create a document library with folders, so that each folder would have unique permissions. To do so, the site owner breaks permission inheritance on the folders and assigns the permissions as needed. In the end, the permission model gets complicated, and the challenge challenge is to quickly find what are the actual permissions on a specific folder or a specific list item at a glance.

The Out of the Box tools include a column called "Shared With", which allows to quickly access the list of users who have access to the item, but when there are more than 2-3 groups it just shows shared with "Multiple people". To overcome this limitation and show all groups and users who have access to the item, I created a list column column with Client Side Rendering technique and some JavaScript. The column displays a list of all users and groups who have access to the folder or document, and renders a link that points directly to the "Manage Permissions" page for this item. When you have to set up permissions for tens of folders, this saves some grey hair.

The end result is on the picture below.

Permissions column shows the actual permissions on each folder and list item








The main challenge in implementing this column was to get CSR (Client Side Rendering) to work together with Minimal Download Strategy enabled or disabled. To do so, there is some additional startup code in the JavaScript file:

    Ivan.PermissionsField.Functions.RegisterField = function () {
        SPClientTemplates.TemplateManager.RegisterTemplateOverrides(Ivan.PermissionsField)
    }

    Ivan.PermissionsField.Functions.MdsRegisterField = function () {
        var thisUrl = _spPageContextInfo.siteServerRelativeUrl
            + '/SiteAssets/JSLink.js';
        Ivan.PermissionsField.Functions.RegisterField();
        RegisterModuleInit(thisUrl, Ivan.PermissionsField.Functions.RegisterField)
    }

    if (typeof _spPageContextInfo != "undefined" && _spPageContextInfo != null) {
        Ivan.PermissionsField.Functions.MdsRegisterField()
    } else {
        Ivan.PermissionsField.Functions.RegisterField()
    }

Once the script file is deployed to the site's "Site Assets" folder, the only step that remains is to create a basic column called Permissions and assign a ScriptLink to it. In my case it is done with a C# console app that uses the SharePoint Client API to perform the actions:


var fieldName = "Permissions";
var ctx = GetClientContext(url);
ctx.Load(ctx.Web.Fields, fc=>fc.Include(f=>f.Title));
ctx.ExecuteQuery();
if(ctx.Web.Fields.ToList().FindIndex(f=>f.Title == fieldName) == -1)
{
    // Create a field called Permissions
    var newField = "<Field Type=\"Text\" DisplayName=\"Permissions\" Name=\"Permissions\"/>";
    ctx.Web.Fields.AddFieldAsXml(newField, false, AddFieldOptions.DefaultValue);
    ctx.ExecuteQuery();
}
var field = ctx.Web.Fields.GetByTitle(fieldName);
ctx.Load(field);
ctx.ExecuteQuery();
field.JSLink = "clienttemplates.js|~site/SiteAssets/PermissionField.js";
field.Update();
ctx.ExecuteQuery();


The entire JS file can be downloaded from here. Please, feel free to improve the solution.
If you use it, please mention my blog as the origin.



Комментариев нет:

Отправить комментарий