Hi,
We had a requirement as below
1/ Add the new item to the target list
2/ After adding the item in the list, the value should be auto incremented under the Chrono Column
3/ Upload the document in the target document library
4/ After uploading the document, the value should be auto incremented under the Chrono Column
5/ In the Chrono Config list of the root site, the updated value should be visible under Chrono Increment Value column w.r.t target list/ library GUID
6/The Chrono field should be hidden in the target list new form and Editform.aspx
7/The Chrono field should be hidden in the target library Editform.aspx
For this the below java script has been developed
var oList, oListItem, oConfigList, oConfigListcount, count = 0, autoincrementcount = 0;
var MC2ChronoConfigList = 'MC2ChronoConfigList';
var ChronoIncrementValue = 'ChronoIncrementValue';
var ChronoFieldColumnName = 'ChronoFieldColumnName';
$(document).ready(function () {
try {
//Get the Chrono Field Auto Increment column details to hide
getChronoColumnandHide(_ spPageContextInfo. siteAbsoluteUrl, "{" + WPQ2FormCtx.ListAttributes.Id + "}");
}
catch (ex) {
console.log(ex);
}
});
function getChronoColumnandHide(webUrl, listGUID) {
try {
var fullUrl = webUrl + "/_api/web/lists/getByTitle('" + MC2ChronoConfigList + "')/items?$select=" + ChronoFieldColumnName + "&$filter= Title eq '" + listGUID + "'";
$.ajax({
url: fullUrl,
type: "GET",
headers: {
"accept": "application/json;odata= verbose",
"content-type": "application/json;odata= verbose",
},
success: onQueryConfigChronoSucceeded,
error: onQueryConfigChronoFailed
});
}
catch (ex) {
console.log(ex);
}
}
function onQueryConfigChronoSucceeded( data) {
if (data.d.results.length != 0) {
SPUtility.GetSPField(data.d. results[0]. ChronoFieldColumnName).Hide();
}
}
function onQueryConfigChronoFailed() {
alert("An error has occurred.");
}
// PreSaveAction Operation
// success: The function to execute if the call is sucessfull
// failure: The function to execute if the call fails
function PreSaveAction() {
try {
//WPQ2FormCtx.FormControlMode = 2 - Denotes current form is Edit Form
//WPQ2FormCtx.FormControlMode = 3 - Denotes current form is New Form
//WPQ2FormCtx.ListAttributes. ListTemplateType == 100 - Denotes current list is of type Document Library
//WPQ2FormCtx.ListAttributes. ListTemplateType == 101 - Denotes current list is of type Generic List
if (WPQ2FormCtx.ListAttributes. ListTemplateType == 101) {
GetMC2ChronoConfigListData()
}
else if (WPQ2FormCtx.ListAttributes. ListTemplateType == 100) {
if (WPQ2FormCtx.FormControlMode == 3) {
GetMC2ChronoConfigListData()
}
else {
return true;
}
}
}
catch (ex) {
console.log(ex);
}
}
function GetMC2ChronoConfigListData() {
//Get Client Context and Web object.
clientContext = new SP.ClientContext.get_current() ;
var oWeb = clientContext.get_web();
oConfigList = clientContext.get_site().get_ rootWeb().get_lists(). getByTitle( MC2ChronoConfigList);
//CamlQuery to fetch records w.r.t List Guid
var camlQuery = new SP.CamlQuery();
var listGUID = "{" + WPQ2FormCtx.ListAttributes.Id + "}";
camlQuery.set_viewXml('<View>< Query><Where><Eq><FieldRef Name=\'Title\'/>' + '<Value Type=\'Text\'>' + listGUID + '</Value></Eq></Where></Query> <RowLimit>10</RowLimit></View> ');
oConfigListcollListItemColl = oConfigList.getItems( camlQuery);
clientContext.load( oConfigListcollListItemColl);
clientContext.load( oConfigList);
clientContext. executeQueryAsync( onQueryReadSucceeded, onQueryReadFailed);
}
// Auto Increment Operation
// success: The function to execute if the call is sucessfull
// failure: The function to execute if the call fails
//Method to Compare List Guid from Config List and update Auto Increment Value
function onQueryReadSucceeded(sender, args) {
if (oConfigListcollListItemColl. get_count() != 0) {
var oConfigListItem = oConfigListcollListItemColl. get_item(0);
var strTitle = oConfigListItem.get_item(' Title');
var strChronoIncrementValue = oConfigListItem.get_item( ChronoIncrementValue);
var strChronoFieldColumnName = oConfigListItem.get_item( ChronoFieldColumnName);
var currValue = SPUtility.GetSPField( strChronoFieldColumnName). GetValue();
if (!currValue) {
SPUtility.GetSPField( strChronoFieldColumnName). SetValue( strChronoIncrementValue + 1)
oConfigListItem.set_item( ChronoIncrementValue, strChronoIncrementValue + 1);
oConfigListItem.update();
clientContext.load( oConfigList);
clientContext. executeQueryAsync(OnSuccess, OnFailure)
}
else
{
form.SubmitClientForm();
}
}
else {
alert("An error has occurred.");
}
}
function onQueryReadFailed(sender, args) {
alert('An error has occurred. ' + args.get_message() + '\n' + args.get_stackTrace());
}
function OnSuccess(sender, args) {
form.SubmitClientForm();
}
function OnFailure(sender, args) {
console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
Output
Chrono Configuration List
Target Document Library