Page Out Of List Scope?

Jun 23, 2010 at 5:34 PM

I was trying to use SPRequireUnique but it was not working.  I kept getting a JS error at the browser.  I went into debug and found that the SPGetDesiplayFromStatic was not working for me.  I think it is b/c I somehoe got my page out of scope from the list.  I was tippped off to this because of hte good comments on the source (thanks Marc!)  The page I am using has been moved to a doc library away fromt he list it works with which may be the problem.

Anyway, my solution was to modify the options and logic on the SPRequireUnique routine to allow me to add the list name manually as part of the call top the funtion.  Now it works.

Could this be an included in a future release?

Or....was there a better way to fix this.  I assume that I broke something in the list or page but it is really easier for me to modify the JavaScript and JQuery than to figure thigns out in Sharepoint sometimes.



Jun 26, 2010 at 2:27 AM
Edited Jun 27, 2010 at 1:29 AM



Yes, the function assumes that the page is in the list’s context.  I’m not sure why you’d move the page into another Document Library’s context, frankly.


In any case, when you ask “Could this be an included in a future release?”, what specifically are you looking for?




Jun 26, 2010 at 11:28 PM


I separate forms from the lists into another doc library because it offers some separation (that sounds redundant!).  Explanation - Sometimes the form or view is working with more than 1 list, so it doesn't really belong to any 1 list.  Also, I have had forms where there are 2 versions - 1 that could edit any column for managers and 1 that could only edit certain columns for a regualr user.  With the 2 versions in a doc library I set permissions for who can see and use each form.

Anyways, what I modified the SPServices .js file to do is accept the list name as an option so that if it is out of scope it still works.  It can still assume the the list if the page is in scope, but now I can ovverride it with a list name.

Does that make sense or am I smokin'  dope here?



Jun 27, 2010 at 1:31 AM


I think what you're doing makes good sense now that you've described it.  I'd be interested to see your mods to SPServices, as it sounds like this might be something others could take advantage of.


Jun 28, 2010 at 5:10 PM


My changes are commented.  I offer this warning - I am not particularly good with JS or JQ - I have only been into them since late last year - about when I started working with Sharepoint.  Sharepoint actually drove me to learn JS and JQ.  However, I think this modification is pretyt basic.

Also, thanks again for you contributions to the SP community!  Hopefully I can catch your presentaition at SP Tech Con this Fall.  I may have alreay spent my conference budget for the year.

// Function which checks to see if the value for a column on the form is unique in the list.
$.fn.SPServices.SPRequireUnique = function (options) {

var opt = $.extend({}, {
columnStaticName: "Title", // Name of the column
duplicateAction: 0, // 0 = warn, 1 = prevent
ignoreCase: "false", // If set to true, the function ignores case, if false it looks for an exact match
initMsg: "This value must be unique.", // Initial message to display after setup
initMsgCSSClass: "ms-vb", // CSS class for initial message
errMsg: "This value is not unique.", // Error message to display if not unique
errMsgCSSClass: "ms-formvalidation", // CSS class for error message
completefunc: null, // Function to call on completion of rendering the change.
listName: null //Required if page is out of scope fo the list
//Above mod becase I had a page out of scope.  SPD 06/12/2010
}, options);
var msg = "<span id='SPRequireUnique" + opt.columnStaticName + "' class='{0}'>{1}<br/></span>";
var initMsg = msg.replace(/\{0\}/g, opt.initMsgCSSClass).replace(/\{1\}/g, opt.initMsg);
var errMsg =  msg.replace(/\{0\}/g, opt.errMsgCSSClass).replace(/\{1\}/g, opt.errMsg);
var thisList

//The rest of the mod  SPD  06/12/2010

 if (opt.listName == null){
thisList = listNameFromUrl();
thisList = opt.listName;
//End of the mod  SPD  06/12/2010
  var columnDisplayName = $().SPServices.SPGetDisplayFromStatic({
listName: thisList,
columnStaticName: opt.columnStaticName
var columnObj = $("input:[Title='" + columnDisplayName + "']");

$(columnObj).change(function () {
var columnValueCount = 0;
// Get the columnDisplayName's value
var columnValue = $(this).attr("value");

// Call the Lists Web Service (GetListItems) to see if the value already exists
operation: "GetListItems",
async: false,
listName: listNameFromUrl(),
// Make sure we get all the items, ignoring any filters on the default view.
CAMLQuery: "<Query><Where><IsNotNull><FieldRef Name='" + opt.columnStaticName + "'/></IsNotNull></Where></Query>",
// Filter based on columnDisplayName's value
CAMLViewFields: "<ViewFields><FieldRef Name='" + opt.columnStaticName + "' /></ViewFields>",
// Override the default view rowlimit and get all appropriate rows
CAMLRowLimit: 0,
completefunc: function(xData, Status) {
var testValue = opt.ignoreCase ? columnValue.toUpperCase() : columnValue;
$(xData.responseXML).find("[nodeName=z:row]").each(function() {
var thisValue = opt.ignoreCase ? $(this).attr("ows_" + opt.columnStaticName).toUpperCase() : $(this).attr("ows_" + opt.columnStaticName);
if(testValue == thisValue) columnValueCount++;

var newMsg = initMsg;
$("input:[value='OK']").attr("disabled", "");
if(columnValueCount > 0) {
newMsg = errMsg;
if(opt.duplicateAction == 1) {
$("input:[Title='" + opt.columnDisplayName + "']").focus();
$("input:[value='OK']").attr("disabled", "disabled");
$("span#SPRequireUnique" + opt.columnStaticName).html(newMsg);

// If present, call completefunc when all else is done
if(opt.completefunc != null) opt.completefunc();