SPServices.SPRequireUnique on list column with empty cells?

Sep 28, 2010 at 1:04 PM

Hi folks,

I'm trying to implement SPRequireUnique and it doesnt seem to be working. Here is the code snippet I've pasted in below the PlaceHolderMain:

<script language="javascript" type="text/javascript" src="../../jQuery%20Library/jquery-1.4.2.js"> </script>
<script language="javascript" type="text/javascript" src="../../jQuery%20Library/jquery.SPServices-0.5.6.js"> </script>
<script language="javascript" type="text/javascript">
$().SPServices.defaults.listName = "DMAC All";  // Name of the list for list operations
 $(document).ready(function() {
  $().SPServices.SPRequireUnique({ 
   columnStaticName: "Serial",
   duplicateAction: 1,
   ignoreCase: "true",
   initMsg: "This value must be unique.",
   initMsgCSSClass: "ms-vb",
   errMsg: "This value is not unique.",
   errMsgCSSClass: "ms-formvalidation"
  });
 });
</script>

The Column "Serial" is not a required column and does have existing emtpy rows which would make them not unique. How do I work around this so that the function functions? Could I implement a counter so that if Serial=Null then +1 to satisfy the unique requirement?

 

Thanks,

Mike

Coordinator
Sep 28, 2010 at 2:22 PM

Mike:

The purpose of SPRequireUnique is to provide true uniqueness, primarily to ensure good "keys" in referencial lists, like a list of States or Countries. What you're trying to do sounds like "non-blank uniqueness". That'll require some adaptation of the function, as you guessed. Am I hearing you right? You want unique non-blank values, but blank is fine to repeat? It might make sense to add this as an option to SPRequireUnique. Hmm.

M.

Sep 28, 2010 at 3:15 PM

Thats exactly what I'm looking for. I have, for now, resolved the empty cells with unique values and am still not having any success. I onyly NewFormCustom.aspx modified with the above code. Do I need to call it from anywhere else?

Thanks,

Mike

Coordinator
Sep 28, 2010 at 4:26 PM

Depends on what you want to happen! You probably want to call the function in both the NewForm and the EditForm, though.

M.

Sep 29, 2010 at 2:25 PM

I'm calling it in both and even though I've updated the column to contain only unique id's it still is not working. I created a document library and pasted in the relevent js libraries that is site accessible. Any thoughts?

 

Thanks,

Mike

Coordinator
Sep 30, 2010 at 4:05 AM
Mike: When you say it's not working now, what's the issue? If you've resolved the existing duplicates issue, then it must be something else. Are you getting an error? M.
Oct 1, 2010 at 12:46 AM

M. No error message is generated. Even though I verified that the specified column (Serial) is a single line text, has no duplicate values, when I click new and add a new row and purposely insert a value that exists it allows the save without generating any sort of error message or warning. I'm stumped...

Coordinator
Oct 1, 2010 at 1:40 AM
Sounds like the function may not even be running. Does the initMsg show in the form? Check the reference to the two .js files, too. If you put an alert inside the $(document).ready, that'll tell you if the jQuery library reference is correct. M.
Oct 1, 2010 at 3:16 AM

The initmsg shows right next to the OK and Cancel buttons. The reference to the two libraries is correct and my alert pops up. Is my load order correct?

<script language="javascript" type="text/javascript" src="../../jQuery%20Library/jquery-1.4.2.min.js"> </script>
<script language="javascript" type="text/javascript" src="../../jQuery%20Library/jquery.SPServices-0.5.6.js"> </script>
<script language="javascript" type="text/javascript">
	$(document).ready(function() {
	alert("NewForm Loaded.");
		$().SPServices.SPRequireUnique({	
			columnStaticName: "Serial",
			duplicateAction: 1,
			ignoreCase: "true",
			initMsg: "This value must be unique.",
			initMsgCSSClass: "ms-vb",
			errMsg: "This value is not unique.",
			errMsgCSSClass: "ms-formvalidation"
		});
	});
</script>

Coordinator
Oct 1, 2010 at 3:27 AM

This one's vexing. It ought to be working. This is WSS 3.0 or MOSS? (i.e., not SharePoint 2010?)

Check the images in the docs here:
http://spservices.codeplex.com/wikipage?title=%24%28%29.SPServices.SPRequireUnique
Is that where you're seeing the initMsg? You said next to the OK and Cancel buttons, which doesn't sound right.

M.

Oct 1, 2010 at 6:01 PM

MOSS 2007, Nope the initmsg doesnt show as a column description it shows up right next to the button. Here's the full page of code perhaps I screwed something up:

 

<%@ Page language="C#" MasterPageFile="~masterurl/default.master"    Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage,Microsoft.SharePoint,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" meta:progid="SharePoint.WebPartPage.Document" meta:webpartpageexpansion="full" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Import Namespace="Microsoft.SharePoint" %> <%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<asp:Content ContentPlaceHolderId="PlaceHolderPageTitle" runat="server">
	<SharePoint:ListFormPageTitle runat="server"/>
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderPageTitleInTitleArea" runat="server">
	<SharePoint:ListProperty Property="LinkTitle" runat="server" id="ID_LinkTitle"/>
	: 
	<SharePoint:ListItemProperty id="ID_ItemProperty" MaxLength=40 runat="server"/>
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderPageImage" runat="server">
	<IMG SRC="/_layouts/images/blank.gif" width=1 height=1 alt="">
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderLeftNavBar" runat="server"/>
<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">


<script language="javascript" type="text/javascript" src="../../jQuery%20Library/jquery-1.4.2.min.js"> </script>
<script language="javascript" type="text/javascript" src="../../jQuery%20Library/jquery.SPServices-0.5.6.js"> </script>
<script language="javascript" type="text/javascript">
	$(document).ready(function() {
		$().SPServices.SPRequireUnique({	
			columnStaticName: "Serial",
			duplicateAction: 1,
			ignoreCase: "true",
			initMsg: "This value must be unique.",
			initMsgCSSClass: "ms-vb",
			errMsg: "This value is not unique.",
			errMsgCSSClass: "ms-formvalidation"
		});
	});
</script>




<table cellpadding=0 cellspacing=0 id="onetIDListForm">
 <tr>
  <td>
 <WebPartPages:WebPartZone runat="server" FrameType="None" ID="Main" Title="loc:Main"><ZoneTemplate>
<WebPartPages:ListFormWebPart runat="server" __MarkupType="xmlmarkup" WebPart="true" __WebPartId="{F6A3E266-131E-4B3E-83E0-A4C6486847A5}" >
<WebPart xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/WebPart/v2">
  <Title>Daily Input</Title>
  <FrameType>Default</FrameType>
  <Description />
  <IsIncluded>true</IsIncluded>
  <PartOrder>1</PartOrder>
  <FrameState>Normal</FrameState>
  <Height />
  <Width />
  <AllowRemove>true</AllowRemove>
  <AllowZoneChange>true</AllowZoneChange>
  <AllowMinimize>true</AllowMinimize>
  <AllowConnect>true</AllowConnect>
  <AllowEdit>true</AllowEdit>
  <AllowHide>true</AllowHide>
  <IsVisible>true</IsVisible>
  <DetailLink />
  <HelpLink />
  <HelpMode>Modeless</HelpMode>
  <Dir>Default</Dir>
  <PartImageSmall />
  <MissingAssembly>Cannot import this Web Part.</MissingAssembly>
  <PartImageLarge />
  <IsIncludedFilter />
  <ExportControlledProperties>true</ExportControlledProperties>
  <ConnectionID>00000000-0000-0000-0000-000000000000</ConnectionID>
  <ID>g_f6a3e266_131e_4b3e_83e0_a4c6486847a5</ID>
  <ListName xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">{BD0DE14C-FFBD-4290-97FD-C3C73E3F3095}</ListName>
  <ListItemId xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">0</ListItemId>
  <ControlMode xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">New</ControlMode>
  <TemplateName xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">ListForm</TemplateName>
  <FormType xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">8</FormType>
  <ViewFlag xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">1048576</ViewFlag>
</WebPart>
</WebPartPages:ListFormWebPart>
</ZoneTemplate></WebPartPages:WebPartZone>
 <IMG SRC="/_layouts/images/blank.gif" width=590 height=1 alt="">
  </td>
 </tr>
</table>
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderTitleLeftBorder" runat="server">
<table cellpadding=0 height=100% width=100% cellspacing=0>
 <tr><td class="ms-areaseparatorleft"><IMG SRC="/_layouts/images/blank.gif" width=1 height=1 alt=""></td></tr>
</table>
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderTitleAreaClass" runat="server">
<script id="onetidPageTitleAreaFrameScript">
	document.getElementById("onetidPageTitleAreaFrame").className="ms-areaseparator";
</script>
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderBodyAreaClass" runat="server">
<style type="text/css">
.ms-bodyareaframe {
	padding: 8px;
	border: none;
}
</style>
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderBodyLeftBorder" runat="server">
<div class='ms-areaseparatorleft'><IMG SRC="/_layouts/images/blank.gif" width=8 height=100% alt=""></div>
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderTitleRightMargin" runat="server">
<div class='ms-areaseparatorright'><IMG SRC="/_layouts/images/blank.gif" width=8 height=100% alt=""></div>
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderBodyRightMargin" runat="server">
<div class='ms-areaseparatorright'><IMG SRC="/_layouts/images/blank.gif" width=8 height=100% alt=""></div>
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderTitleAreaSeparator" runat="server"/>

Coordinator
Oct 5, 2010 at 5:24 AM

Mike:

Again, everything looks fine. I'm not sure what to do next to try to sort this out. Hmm. Serial needs to be a Single line of text column (as indicated in the docs). Is it some other type?

M.

Oct 6, 2010 at 1:40 PM

Where in the code does this function reference the list that you are actually checking uniqeness against? Maybe I am missing it or I could be wrong here, but I think that you have to tell this function the list you are working on right?

Oct 13, 2010 at 8:22 PM

Issue resolved... I created the original list as an import from a spreadsheet. I created a new blank custom list and copied in the data from existing list. Now it works fine.

Thanks,

Mike

Coordinator
Oct 13, 2010 at 8:25 PM

Great! That makes some sense, as the list structure may have been different due the the import. It's impossible to tell from here, of course. In any case, glad you got it working.

M.

Jan 10, 2013 at 6:38 AM

how do i implement "SPRequireUnique" validation for combination of fields in list?

Siva

Coordinator
Jan 10, 2013 at 12:32 PM

Siva:

Quick answer: you can't. The function only works for a single column.

M.