You will always come across customers with interesting requirements and we had one.
The SharePoint site has a document library in which content was organized within Document sets (i.e documents were created and stored within document sets).
One particular field (say Field 1) was shared between the document set and the document to cascade the values. The requirement was to have unique values in Field 1 within the library. We could not set "Enforce unique values" for Field 1 since it will not allow us to share the value of the field with the documents inside the document set.
JQuery as usual was the savior. We had to leverage the SPServices functions to achieve what was expected. SPService has a function SPRequireUnique which did not work well for us since it was large document library with more than hunderd thousand documents.
SharePoint has a PreSave function which is invoked prior to saving the item in the Database. This is very handy when you need to perform custom validations.
function PreSaveAction()
{
var queryStringVals = $().SPServices.SPGetQueryString();
// Retrieve the ID of the current item from the URL
var thisID = queryStringVals.ID; // Retrieve the ID of the current item from the URL
var thisList = $().SPServices.SPListNameFromUrl();
var columnCheck = 'Field1';
var columnDisplayName = $().SPServices.SPGetDisplayFromStatic({
listName: thisList,
columnStaticName: columnCheck
});
var columnValue = $("input[Title='"+columnDisplayName+"']").attr("value");
var dupExists = 0;
// Call the Lists Web Service (GetListItems) to see if the value already exists
$().SPServices({
operation: "GetListItems",
async: false,
listName: thisList,
// Query to match the items with the same value
CAMLQuery: "<Query><Where><Eq><FieldRef Name='Field1'/><Value Type='Text'>" + columnValue + "</Value></Eq></Where></Query>",
CAMLViewFields: "<ViewFields><FieldRef Name='ID' /><FieldRef Name='Field1' /></ViewFields>",
// Override the default view rowlimit and get all appropriate rows
CAMLRowLimit: 0,
completefunc: function(xData, Status) {
var currentValue = columnValue.toUpperCase();
$(xData.responseXML).SPFilterNode("z:row").each(function() {
var thisValue = $(this).attr("ows_Field1").toUpperCase();
// If this value already exists in columnStaticName and it's not the current item, then save the ID in the array
if((currentValue === thisValue) && ($(this).attr("ows_ID") !== thisID)) {
dupExists = 1;
}
});
}
});
if(dupExists == 0)
{
return true;
}
else
{
alert('The value in Field 1 exists in another item');
return false;
}
}
Hope this helps you save some time.
Cheers.
The SharePoint site has a document library in which content was organized within Document sets (i.e documents were created and stored within document sets).
One particular field (say Field 1) was shared between the document set and the document to cascade the values. The requirement was to have unique values in Field 1 within the library. We could not set "Enforce unique values" for Field 1 since it will not allow us to share the value of the field with the documents inside the document set.
JQuery as usual was the savior. We had to leverage the SPServices functions to achieve what was expected. SPService has a function SPRequireUnique which did not work well for us since it was large document library with more than hunderd thousand documents.
SharePoint has a PreSave function which is invoked prior to saving the item in the Database. This is very handy when you need to perform custom validations.
function PreSaveAction()
{
var queryStringVals = $().SPServices.SPGetQueryString();
// Retrieve the ID of the current item from the URL
var thisID = queryStringVals.ID; // Retrieve the ID of the current item from the URL
var thisList = $().SPServices.SPListNameFromUrl();
var columnCheck = 'Field1';
var columnDisplayName = $().SPServices.SPGetDisplayFromStatic({
listName: thisList,
columnStaticName: columnCheck
});
var columnValue = $("input[Title='"+columnDisplayName+"']").attr("value");
var dupExists = 0;
// Call the Lists Web Service (GetListItems) to see if the value already exists
$().SPServices({
operation: "GetListItems",
async: false,
listName: thisList,
// Query to match the items with the same value
CAMLQuery: "<Query><Where><Eq><FieldRef Name='Field1'/><Value Type='Text'>" + columnValue + "</Value></Eq></Where></Query>",
CAMLViewFields: "<ViewFields><FieldRef Name='ID' /><FieldRef Name='Field1' /></ViewFields>",
// Override the default view rowlimit and get all appropriate rows
CAMLRowLimit: 0,
completefunc: function(xData, Status) {
var currentValue = columnValue.toUpperCase();
$(xData.responseXML).SPFilterNode("z:row").each(function() {
var thisValue = $(this).attr("ows_Field1").toUpperCase();
// If this value already exists in columnStaticName and it's not the current item, then save the ID in the array
if((currentValue === thisValue) && ($(this).attr("ows_ID") !== thisID)) {
dupExists = 1;
}
});
}
});
if(dupExists == 0)
{
return true;
}
else
{
alert('The value in Field 1 exists in another item');
return false;
}
}
Hope this helps you save some time.
Cheers.
Great blog........A value proposition is a promise of value to be delivered and acknowledged and a belief from the customer that value will be delivered and experienced. A value proposition can apply to an entire organization, or parts thereof, or customer accounts, or products or services.
ReplyDeleteorderingchecks.net
Excellent post, thanks a lot for sharing!!! Really useful for me :)
ReplyDelete