Add callout cu​stom action based on content TYPE

Posted on

I had a strange requirement this week.  We have a document library with 3000 items where there is a workflow that was approved by the users some months ago, the workflow is quite complex.

However after some months using it, the users complain that some documents dont need the long approval and can just go to Completed.

The status field is of course a hidden field, so they cant just modify it, normally the workflow would update this field for the users.

 

Solution

​​Create a callout custom action, for those who dont know the callout its the small pop window that appears when you click on the 3 dots in a document library item.

Callouts are not limited to list items, they can be done everywhere in every element you want, and they are used extensively in search results as display templates.

In  my case I didnt need to put the custom action in search results, just in the document library.

 

​​1. ​add code snippet webpart

​The code below is for a normal document library, as I was working during the weekend at home, I did it on my sharepoint online (Documents Library),  but in your environment you only need to change the list title and the field you want to change.


SP.SOD.executeFunc("callout.js", "Callout", function () {
var itemCtx = {};
itemCtx.Templates = {};
itemCtx.BaseViewID = 'Callout';
// Define the list template type
itemCtx.ListTemplateType = 101;

itemCtx.Templates.Footer = function (itemCtx) {
return CalloutRenderFooterTemplate(itemCtx, AddCustomCompleteAction, true);
};

SPClientTemplates.TemplateManager.RegisterTemplateOverrides(itemCtx);
});

function AddCustomCompleteAction (renderCtx, calloutActionMenu) {
if(renderCtx.CurrentItem.ContentType=='Document') {
calloutActionMenu.addAction (new CalloutAction ({
text: 'COMPLETE',
tooltip: 'This action will set status to Complete, this action cant be rolled back.',
onClickCallback: function() {UpdateBillCycleStatusToCompleted(1);}
}));
}

// Show the default document library actions
CalloutOnPostRenderTemplate(renderCtx, calloutActionMenu);

// Show the follow action
calloutActionMenu.addAction(new CalloutAction({
text: Strings.STS.L_CalloutFollowAction,
tooltip: Strings.STS.L_CalloutFollowAction_Tooltip,
onClickCallback: function (calloutActionClickEvent, calloutAction) {
var callout = GetCalloutFromRenderCtx(renderCtx);
if (!(typeof(callout) === 'undefined' || callout === null))
callout.close();
SP.SOD.executeFunc('followingcommon.js', 'FollowSelectedDocument', function() { FollowSelectedDocument(renderCtx); });
}
}));
}

function UpdateBillCycleStatusToCompleted(itemId) {
var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('Documents');
this.oListItem = oList.getItemById(itemId);
oListItem.set_item('Title', 'Complet');
oListItem.update();
clientContext.executeQueryAsync(Function.createDelegate(this, this.StatusCompletedSucceeded), Function.createDelegate(this, this.StatusCompletedFailed));
}

function StatusCompletedSucceeded() {
alert('Item updated!');
}

function StatusCompletedFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

function IsCurrentUserMemberOfGroup(groupName, callback) {
var currentContext = new SP.ClientContext.get_current();
var currentWeb = currentContext.get_web();

var currentUser = currentContext.get_web().get_currentUser();
currentContext.load(currentUser);

var allGroups = currentWeb.get_siteGroups();
currentContext.load(allGroups);

var group = allGroups.getByName(groupName);
currentContext.load(group);

var groupUsers = group.get_users();
currentContext.load(groupUsers);

currentContext.executeQueryAsync(OnSuccess,OnFailure);

function OnSuccess(sender, args) {
var userInGroup = false;
var groupUserEnumerator = groupUsers.getEnumerator();
while (groupUserEnumerator.moveNext()) {
var groupUser = groupUserEnumerator.get_current();
if (groupUser.get_id() == currentUser.get_id()) {
userInGroup = true;
break;
}
}
callback(userInGroup);
}

function OnFailure(sender, args) {
callback(false);
}
}​

​2. next challenge.

​Now I need to show the option based on the current user group, if the user its in a specific group, then show the custom action, otherwise dont show it, that will be my next blog post.

 

Hope you enjoy it, I had fun times coding this.

Thanks to : @eliostruyf

http://www.eliostruyf.com/adding-a-custom-action-to-a-callout-in-sharepoint-2013/​​​

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s