Error adding a duplicate item to a list with folders

Apr 2, 2012 at 5:56 PM
Edited Apr 2, 2012 at 5:58 PM

I am using the following code to add a list item:

 

addListItem: function (itemsArray, listName) { 
         $().SPServices({
                    operation: "UpdateListItems",
                    async: false,
                    batchCmd: "New",
                    listName: listName,
                    valuepairs: itemsArray,
                    completefunc: function (xData, Status) {
                       // do some stuff
                    }
            });
    
        }
itemsArray contains valuepairs of items to be added.  I have to include 'BaseName' for one of the value pairs because we are using folders so I want to specify the folder path to add it to.  It all works great with one exception - I notice that when I include the BaseName value pair, the code errors off if I try to add an item with the same title (in the same path). However, if I don't include BaseName (i.e. the folder path does not matter and it just adds the item to the top level of the list), it allows duplicate entries.  Is this the intended behavior or am I doing something wrong?  (I need to be able to add items with duplicate titles at various path levels.) We are using SP2010 although I tested this in SP2007 as well.
Coordinator
Apr 2, 2012 at 9:28 PM

jindihar:

Are you sure that the folder exists before you try to create new items in it? The settings on your list will probably have an impact on this as well.

M.

Apr 3, 2012 at 2:25 PM
Edited Apr 3, 2012 at 6:58 PM

Yes, the folder exists. I can add an item to a folder and it works fine and I can add additional items to the folder and it works fine unless I add one with the same title. In fact, we never knew this error existed until we removed the validation we had which checked for duplicate names in the folder. It was removed due to a requirement change. Can you give me an idea of what list settings might impact this? It occurs in both SP2007 and SP2010. I tried to manually add a duplicate item (through the Sharepoint interface) within a folder in the list and it allowed me to add it.

 


Apr 3, 2012 at 2:53 PM
Edited Apr 3, 2012 at 7:00 PM

The value pairs that I am passing for BaseName and title look like this:

items.push(["BaseName", _path + "/" + projectName]);

items.push(["Title", projectName]);

 

The soap response I get back when I use this and try to add a duplicate title (and include the BaseName value pair) is:

<?xml version="1.0"?>

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><UpdateListItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/"><UpdateListItemsResult><Results><Result ID="1,New"><ErrorCode>0x80131904</ErrorCode><ErrorText>The operation failed because an unexpected error occurred. (Result Code: 0x80131904)</ErrorText></Result></Results></UpdateListItemsResult></UpdateListItemsResponse></soap:Body></soap:Envelope>

 

 

Apr 3, 2012 at 3:07 PM
Looks like you have the ability to capture the XML message... can you post the full Request and associated response?
Also, please use the code formatter when you post the message... all this will make it quicker for us to try and help you.

_________
Paul T



Apr 3, 2012 at 6:01 PM

Hey guys,

Look over this thread... I ran into the same barrier a while ago with this as well.

 

Cheers,

Matt

Apr 3, 2012 at 6:02 PM
Edited Apr 3, 2012 at 6:03 PM

Not exactly sure what you mean by the 'full Request' - not sure how to capture the actual request going out, but maybe this will help.  Here is the code that makes the call to SPServices:

        addListItem: function (itemsArray, listName, source) {
            var thisId = -1;
            try {
                $().SPServices({
                    operation: "UpdateListItems",
                    async: false,
                    batchCmd: "New",
                    listName: listName,
                    valuepairs: itemsArray,
                    completefunc: function (xData, Status) {
                        var soapResponse = soapResponseData.getErrorResponse(xData, Status);
                        if (soapResponse.responseData.IsError) {
                            errorLogData.addError("addListItem in spsUtilities.js Failed", source, soapResponse.responseData.Details);
                            thisId = -1;
                        } else {
                            $(xData.responseXML).find("[nodeName='z:row']").each(function () {
                                thisId = $(this).attr("ows_ID");
                            });
                        }
                    }
                });
            } catch (e) {
                errorLogData.addError("addListItem in spsUtilities.js Failed", source, "Exception raised. " + e.name + ": " + e.message);
                thisId = -1;
            }
            if (thisId === undefined) {
                thisId = -1;
            }
            return thisId;
        }
For this run, the itemsArray structure passed in the SPServices calls looked like this:
itemsArray	{...}	Object
[0]	{...}	Object
	[0]	"BaseName"	String
	[1]	"NTD/NTDP3"	String
[1]	{...}	Object
	[0]	"Title"	String
	[1]	"NTDP3"	String
[2]	{...}	Object
	[0]	"Organization"	String
	[1]	"13"	String
[3]	{...}	Object
	[0]	"Categories"	String
	[1]	""	String
[4]	{...}	Object
	[0]	"PM"	String
	[1]	"-1;#DCDJV\indihar"	String
[5]	{...}	Object
	[0]	"PMBackup"	String
	[1]	""	String
[6]	{...}	Object
	[0]	"PMBkupSend"	String
	[1]	"0"	String
length	7	Long

The associated response was:

<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><UpdateListItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/"><UpdateListItemsResult><Results><Result ID="1,New"><ErrorCode>0x80131904</ErrorCode><ErrorText>The operation failed because an unexpected error occurred. (Result Code: 0x80131904)</ErrorText></Result></Results></UpdateListItemsResult></UpdateListItemsResponse></soap:Body></soap:Envelope>
There was already an item in the NTD folder called NTDP3 which is (I believe) why the error occurred since it works if the name is unique in that folder.

 

Jun 4, 2012 at 5:59 PM

Hi jindihar,

I ran into the same problem when trying to create a list item into a folder where there was another record with the same name. I solved my problem by adding a timestamp to the record name. I don't know if your requirements would allow you to do such thing. Here is a fragment of my code, hope it helps you.

function (xData, Status){
			rXML = xData.responseXML;
			rows = rXML.getElementsByTagName("z:row")||rXML.getElementsByTagNameNS("*","row");
			$(rows).each(function(){
				var d = new Date();
				var timestamp = getUTCTimestamp(d);								
				$().SPServices({
						operation: "UpdateListItems",
						async: false,
						batchCmd: "New",
						listName: "ServerCI",
						valuepairs: [["BaseName", FolderName + $(this).attr("ows_Title") + '_' + timestamp],
								["Operation",'Add'],
								["Supported",$(this).attr("ows_Supported") == undefined ? '' : $(this).attr("ows_Supported")],
								["Tier3",$(this).attr("ows_Tier3") == undefined ? '' : $(this).attr("ows_Tier3")],
								["SerialNumber",$(this).attr("ows_SerialNumber") == undefined ? '' : $(this).attr("ows_SerialNumber")],
								["AssetTag",$(this).attr("ows_AssetTag") == undefined ? '' : $(this).attr("ows_AssetTag")],
								["Title",$(this).attr("ows_Title") + '_' + timestamp],
								["ICFID",ICFID]],
								completefunc: function(xData, Status){},//completefunc
								error: function OnFail(result){alert('Could not create the snapshot record, please contact the site adminsitrator');}
						});//SPServices
				});//each
function padZ(n) {
  return (n<10? '0' : '') + n;
}

function getUTCTimestamp(d) {
  return d.getUTCFullYear() + padZ(d.getUTCMonth() + 1) + padZ(d.getUTCDate()) + 'T' + padZ(d.getUTCHours()) + padZ(d.getUTCMinutes()) + padZ(d.getUTCSeconds());
}

 

Jun 4, 2012 at 7:07 PM

When adding items, the BaseName must be unique.  I append the BaseName with a date converted to a number.  This forces a unique value.  In an example where I add items inside of existing folders, my Value Pairs argument would look something like this:

valuepairs: ["BaseName", strMyFolder + "/" + strTitle + "_" + Number(new Date())], ["Title", strTitle],

This ensures the BaseName is unique and the item's Title displays as I want it to.

 

Jun 4, 2012 at 7:08 PM

Whoops!  Let me correct that array with outside brackets....

valuepairs: [["BaseName", strMyFolder + "/" + strTitle + "_" + Number(new Date())], ["Title", strTitle]],