//Reference-https://social.technet.microsoft.com/Forums/office/en-US/0c77954e-596a-4e66-aa6b-2f066449f338/presaveaction-is-not-firing?forum=sharepointcustomizationprevious
//Reference-http://blogs.microsoft.co.il/justguy/2014/06/22/creating-a-publishing-page-in-sharepoint-using-javascript/
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.publishing.js"></script>
<script type="text/javascript">
function PreSaveAction() {
var txtStatus = $(":input[title='Title']").val();
var fieldName = "Description";
var txtDesc;
//var txtDesc = $(":input[title='Description']").val();
$('td.ms-formbody').each(function (i, item) {
item = $(item);
if (item.html().indexOf('FieldName="' + fieldName + '"') > -1) {
txtDesc = item.find("div[contenteditable='true']").html();
}
});
//var txtDesc = getTagFromIdentifierAndTitle("textarea", "TextField", "Description");
var txtStatustrim = txtStatus.replace(/\s+/g, '');
createPublishingPage("" + txtStatustrim + ".aspx", "BlankWebPartPage.aspx", txtStatus, txtDesc);
return false;
}
function loadPageLayout(pageLayoutName, callback) {
var pageFromDocLayout, pageLayoutItem;
SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function () {
var context = SP.ClientContext.get_current();
var site = context.get_site();
context.executeQueryAsync(function () {
var rootWeb = site.get_rootWeb();
context.load(rootWeb, 'ServerRelativeUrl');
context.executeQueryAsync(function () {
var rootUrl = rootWeb.get_serverRelativeUrl();
pageFromDocLayout = rootWeb.getFileByServerRelativeUrl(rootUrl + "_catalogs/masterpage/" + pageLayoutName);
context.executeQueryAsync(function () {
pageLayoutItem = pageFromDocLayout.get_listItemAllFields();
context.executeQueryAsync(function () {
if (typeof callback == "function") {
callback(pageLayoutItem);
}
});
});
});
});
});
};
function getTagFromIdentifierAndTitle(tagName, identifier, title) {
var len = identifier.length;
var tags = document.getElementsByTagName(tagName);
for (var i = 0; i < tags.length; i++) {
var tempString = tags[i].id;
if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
return tags[i];
}
}
return null;
}
function createPublishingPage(filename, pageLayoutName, pageTitle, pageDescription) {
SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function () {
SP.SOD.executeFunc('SP.Publishing.js', 'SP.Publishing.PublishingWeb', function () {
var web, pubWeb, pageInfo, newPage, listItem;
var context = SP.ClientContext.get_current();
web = context.get_web();
context.load(web);
context.executeQueryAsync(function () {
pubWeb = SP.Publishing.PublishingWeb.getPublishingWeb(context, web);
context.load(web);
context.load(pubWeb);
context.executeQueryAsync(function () {
// load page layout and create the new page
loadPageLayout(pageLayoutName, function (pageLayoutItem) {
pageInfo = new SP.Publishing.PublishingPageInformation();
pageInfo.set_pageLayoutListItem(pageLayoutItem);
pageInfo.set_name(filename);
newPage = pubWeb.addPublishingPage(pageInfo);
context.load(newPage);
context.executeQueryAsync(function () {
// Success callback after adding a new Publishing Page.
// We want to get the actual list item that is represented by the Publishing Page.
listItem = newPage.get_listItem();
context.load(listItem);
context.executeQueryAsync(
// Success callback after getting the actual list item that is
// represented by the Publishing Page.
// We can now get its FieldValues, one of which is its FileLeafRef value.
// We can then use that value to build the Url to the new page
// and set the href or our link to that Url.
function () {
listItem.set_item("Title", pageTitle);
listItem.set_item("PublishingPageContent", pageDescription);
listItem.update();
context.load(listItem);
listItem.get_file().checkIn();
listItem.get_file().publish("Publishing");
context.executeQueryAsync(
function () {
//if (listItem.file.get_checkOutType() == SP.CheckOutType.online) {
// listItem.file.checkIn();
//}
},
function (sender, args) {
alert('Failed to update page item metadata: ' + args.get_message());
}
);
//if (typeof callback == "function") {
// callback(listItem);
//}
},
// Failure callback after getting the actual list item that is
// represented by the Publishing Page.
function (sender, args) {
alert('Failed to get new page: ' + args.get_message());
}
);
},
// Failure callback after trying to add a new Publishing Page.
function (sender, args) {
alert('Failed to Add Page: ' + args.get_message());
}
);
});
},
// Failure callback after trying to get the host Web as a PublishingWeb.
function (sender, args) {
alert('Failed to get the PublishingWeb: ' + args.get_message());
});
});
});
});
}
</script>
//Reference-http://blogs.microsoft.co.il/justguy/2014/06/22/creating-a-publishing-page-in-sharepoint-using-javascript/
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.publishing.js"></script>
<script type="text/javascript">
function PreSaveAction() {
var txtStatus = $(":input[title='Title']").val();
var fieldName = "Description";
var txtDesc;
//var txtDesc = $(":input[title='Description']").val();
$('td.ms-formbody').each(function (i, item) {
item = $(item);
if (item.html().indexOf('FieldName="' + fieldName + '"') > -1) {
txtDesc = item.find("div[contenteditable='true']").html();
}
});
//var txtDesc = getTagFromIdentifierAndTitle("textarea", "TextField", "Description");
var txtStatustrim = txtStatus.replace(/\s+/g, '');
createPublishingPage("" + txtStatustrim + ".aspx", "BlankWebPartPage.aspx", txtStatus, txtDesc);
return false;
}
function loadPageLayout(pageLayoutName, callback) {
var pageFromDocLayout, pageLayoutItem;
SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function () {
var context = SP.ClientContext.get_current();
var site = context.get_site();
context.executeQueryAsync(function () {
var rootWeb = site.get_rootWeb();
context.load(rootWeb, 'ServerRelativeUrl');
context.executeQueryAsync(function () {
var rootUrl = rootWeb.get_serverRelativeUrl();
pageFromDocLayout = rootWeb.getFileByServerRelativeUrl(rootUrl + "_catalogs/masterpage/" + pageLayoutName);
context.executeQueryAsync(function () {
pageLayoutItem = pageFromDocLayout.get_listItemAllFields();
context.executeQueryAsync(function () {
if (typeof callback == "function") {
callback(pageLayoutItem);
}
});
});
});
});
});
};
function getTagFromIdentifierAndTitle(tagName, identifier, title) {
var len = identifier.length;
var tags = document.getElementsByTagName(tagName);
for (var i = 0; i < tags.length; i++) {
var tempString = tags[i].id;
if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
return tags[i];
}
}
return null;
}
function createPublishingPage(filename, pageLayoutName, pageTitle, pageDescription) {
SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function () {
SP.SOD.executeFunc('SP.Publishing.js', 'SP.Publishing.PublishingWeb', function () {
var web, pubWeb, pageInfo, newPage, listItem;
var context = SP.ClientContext.get_current();
web = context.get_web();
context.load(web);
context.executeQueryAsync(function () {
pubWeb = SP.Publishing.PublishingWeb.getPublishingWeb(context, web);
context.load(web);
context.load(pubWeb);
context.executeQueryAsync(function () {
// load page layout and create the new page
loadPageLayout(pageLayoutName, function (pageLayoutItem) {
pageInfo = new SP.Publishing.PublishingPageInformation();
pageInfo.set_pageLayoutListItem(pageLayoutItem);
pageInfo.set_name(filename);
newPage = pubWeb.addPublishingPage(pageInfo);
context.load(newPage);
context.executeQueryAsync(function () {
// Success callback after adding a new Publishing Page.
// We want to get the actual list item that is represented by the Publishing Page.
listItem = newPage.get_listItem();
context.load(listItem);
context.executeQueryAsync(
// Success callback after getting the actual list item that is
// represented by the Publishing Page.
// We can now get its FieldValues, one of which is its FileLeafRef value.
// We can then use that value to build the Url to the new page
// and set the href or our link to that Url.
function () {
listItem.set_item("Title", pageTitle);
listItem.set_item("PublishingPageContent", pageDescription);
listItem.update();
context.load(listItem);
listItem.get_file().checkIn();
listItem.get_file().publish("Publishing");
context.executeQueryAsync(
function () {
//if (listItem.file.get_checkOutType() == SP.CheckOutType.online) {
// listItem.file.checkIn();
//}
},
function (sender, args) {
alert('Failed to update page item metadata: ' + args.get_message());
}
);
//if (typeof callback == "function") {
// callback(listItem);
//}
},
// Failure callback after getting the actual list item that is
// represented by the Publishing Page.
function (sender, args) {
alert('Failed to get new page: ' + args.get_message());
}
);
},
// Failure callback after trying to add a new Publishing Page.
function (sender, args) {
alert('Failed to Add Page: ' + args.get_message());
}
);
});
},
// Failure callback after trying to get the host Web as a PublishingWeb.
function (sender, args) {
alert('Failed to get the PublishingWeb: ' + args.get_message());
});
});
});
});
}
</script>
No comments:
Post a Comment