Show Create view link and Edit view for SharePoint Surveys
Attaching this script e. g. to master page, we add by javascript CSOM links to create view and edit views for SharePoint surveys to Survey Settings page.
var MyJSNamespace = {
showSurveyCreateViewLink: function () {
var currentUrl = RubicusFrontendIns.location.href;
if (currentUrl.indexOf("survedit.aspx") != -1) {
var createViewLink = currentUrl.replace("survedit.aspx", "ViewType.aspx");
var lastLinkText = "Zmeniť poradie otázok"; //slovak localization text first
var lastLinkTextEn = 'Change the order of the questions';
var lastTr = $('a').filter(function (index) { return $(this).text() === lastLinkText; }).closest('tr');
if (lastTr.length == 0) { //if slovak text not found, try english
lastLinkText = lastLinkTextEn;
lastTr = $('a').filter(function (index) { return $(this).text() === lastLinkText; }).closest('tr');
}
if (lastTr.length != 0) {
var surveySettingTable = $('a').filter(function (index) { return $(this).text() === lastLinkText; }).closest('table');
var clone = $(lastTr).clone();
clone.find('a').attr('href', createViewLink);
clone.find('a').text('Create view');
surveySettingTable.append(clone);
}
}
},
showSurveyViewLinks: function () {
var currentUrl = RubicusFrontendIns.location.href;
if (currentUrl.indexOf("survedit.aspx") != -1) {
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
var listId = MyJSNamespace.getUrlVars(currentUrl)["List"];
var listIdClear = decodeURI(listId).slice(0, -1).substring(1);
var clientContext = SP.ClientContext.get_current();
oList = clientContext.get_web().get_lists().getById(listIdClear);
oWeb = clientContext.get_web();
clientContext.load(oList, "Id");
clientContext.load(oWeb, "Url");
this.collListView = oList.get_views();
clientContext.load(collListView);
clientContext.executeQueryAsync(Function.createDelegate(this, function () { MyJSNamespace.onQuerySucceeded(); }), Function.createDelegate(this, MyJSNamespace.onQueryFailed));
});
}
},
onQuerySucceeded: function () {
var listData = collListView.get_data();
var listId = oList.get_id().toString();
var webUrl = oWeb.get_url();
var listIdEncoded = MyJSNamespace.encodeUrl(listId);
var lastLinkText = "Zmeniť poradie otázok";//location of link adition
var lastLinkTextEn = 'Change the order of the questions';
var lastTr = $('a').filter(function (index) { return $(this).text() === lastLinkText; }).closest('tr');
if (lastTr.length == 0) {
lastLinkText = lastLinkTextEn;
lastTr = $('a').filter(function (index) { return $(this).text() === lastLinkText; }).closest('tr');
}
if (lastTr.length != 0) {
var views = [];
if (listData.length > 0) {
var surveySettingTable = $('a').filter(function (index) { return $(this).text() === lastLinkText; }).closest('table');
for (var i = 0; listData.length > i; i++) {
var viewId = listData[i].get_id().toString().toUpperCase();
var viewTitle = listData[i].get_title();
var viewIdEncoded = MyJSNamespace.encodeUrl(viewId);
var viewEditLink = webUrl + "/_layouts/15/ViewEdit.aspx?List=" + listIdEncoded + "&View=%7B" + viewIdEncoded + "%7D"
var clone = $(lastTr).clone();
clone.find('a').attr('href', viewEditLink);
clone.find('a').text(('Edit view - ' + viewTitle));
surveySettingTable.append(clone);
}
}
}
},
onQueryFailed: function (sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
},
getUrlVars: function (url) {
var vars = [], hash;
var hashes = url.slice(url.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
},
encodeUrl: function (urlPart) {
var urlPartEncoded = encodeURI(urlPart);
urlPartEncoded = urlPartEncoded.replace(/-/g, "%2D");
return urlPartEncoded;
}
}
MyJSNamespace.showSurveyCreateViewLink();
MyJSNamespace.showSurveyViewLinks();