UpdateListItems with lookup, date & time, ect. fields

May 3, 2010 at 2:57 PM

I'm trying to add a new item to a list but i'm not able to make it work: the list has lookup, date & time, multiline, ect. columns.

If i try to add an item to a list with only single line column all is ok.

My code is:

$().SPServices({
            operation: "UpdateListItems",
            listName: "Pre-RFQ Archive",
            batchCmd: "New",
            async: false,
            ID: 1,
            valuepairs: [["Title", tables[0][1]], ["Commodity", tables[0][2]], ["Applications_x0020_Model", tables[0][3]], ["Average_x0020_APV_x0020_of_x00200", tables[0][4]], ["Average_x0020_APV_x0020_of_x0020", tables[0][5]], ["Currency", tables[0][6]], ["Buyer_x0020_of_x0020_reference", tables[0][7]], ["Commodity_x0020_Manager_x0020_of", tables[0][8]], ["Components", tables[0][9]], ["Notes1", tables[0][10]], ["Recommended_x0020_Supplier", tables[0][11]], ["Sectors", tables[0][12]], ["Sourcing_x0020_Phase", tables[0][13]], ["Sourcing_x0020_Type", tables[0][14]], ["Date", tables[0][15]], ["Table", tables[0][16]], ["State", tables[0][17]]],
            completefunc: function (xData, Status) {
            }
        });

tables[0][x] is the value to assign.

There's something different to do with columns different to single line?

Coordinator
May 3, 2010 at 3:11 PM

What happens when you try this code? Do you get a useful error?

M.

May 3, 2010 at 3:15 PM

Nothing happens with no errors.

The Pre-RFQ Archive is a 1:1 copy of another list, the one from which i take the values stored in the var tables

Coordinator
May 3, 2010 at 3:21 PM

Have you tried to peel it back to just a few columns?  Is it just the non-Single line of text columns which are problems?

M.

May 3, 2010 at 3:41 PM

I'm trying adding columns one by one: the first problem is with a date & time column, probably it must be converted to date & time field or what?

Coordinator
May 3, 2010 at 3:46 PM

Looking at the SDK documentation for the UpdateListItems method:
http://msdn.microsoft.com/en-us/library/lists.lists.updatelistitems.aspx
it looks like these formats are allowed:

<Batch OnError="Continue" ListVersion="1"
ViewName="270C0508-A54F-4387-8AD0-49686D685EB2">
   <Method ID="1" Cmd="New">
      <Field Name='ID'>New</Field>
      <Field Name="Title">Value</Field>
      <Field Name="Date_Column">2007-3-25</Field>
      <Field Name="Date_Time_Column">2006-1-11T09:15:30Z</Field>
   </Method>
</Batch>

I can't tell from what you've posted what your formats might be, but can you convert to these? Note that my valuePairs option is just an abstraction on top of the underlying XML format for ease of use.

M.

May 3, 2010 at 3:56 PM

Thanks, this works. :)

Second problem is multiline column (a rich text box): it's value is retrivied with all html tags that formatted it but if i assign this value to the valuepair an empy column field is inserted.

Coordinator
May 3, 2010 at 4:07 PM

You're going to need to eacape the HTML to use it.  I got a nice little function from someone for this the other day (which I'd like to adapt into the library sometime soon):

       function escapeHTMLEncode(str) {
              var div = document.createElement('div');
              var text = document.createTextNode(str);
              div.appendChild(text);
              return div.innerHTML;
       }

Basically what this does in convert the "<"s to "&lt;"s and the ">"s to "&gt;"s.

M.

May 3, 2010 at 4:53 PM

Great! All works now.

Last thing (i hope!): each item of the sourcing list contains one or more attachments, how can i copy them to new item just added to the secondary list?

 

Thanks in advance.

Coordinator
May 3, 2010 at 4:56 PM

Attachments can be a problem. I'm not sure if the Copy Web Service will work, but check it here:
http://spservices.codeplex.com/wikipage?title=Copy&referringTitle=%24%28%29.SPServices

The larger question might be what you're trying to do and whether it makes sense, but you must know what you are doing...

M.

May 4, 2010 at 11:13 AM

CopyIntoItemsLocal seems not to work, no errors but no attachments are copied.

I need to copy/move attachments from the source custom list to the destination custom list.

Coordinator
May 4, 2010 at 1:31 PM

I don't think that there's a Web Services option to move the attachments.

If you could explain your business requirements (as opposed to the technical requirements), I might have other ideas for you.

M.

May 4, 2010 at 2:40 PM

I need to backup items from the source list to the destination (the archive) list.

I saw that there's an "AddAttachment" method in the lists.asmx...

Coordinator
May 4, 2010 at 4:58 PM

Yes, AddAttachment looks like a possibility.  I haven't "wrapped" that operation into the library yet, but I can certainly do so if you think it would be useful.  I haven't used it yet, so I'm not positive it will solve your challenge, but it may very well, in concert with the GetAttachmentCollection method.

M.

Coordinator
May 4, 2010 at 4:58 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
May 7, 2010 at 2:30 PM

The files are already stored in the SharePoint environment because they are attachments of the source list that have to be copied to another list.

Coordinator
May 7, 2010 at 4:08 PM

I'm going to see if I can try to figure something out over the weekend.  No guarantees, but I'd like to understand how to do this as well.

M.

Coordinator
May 7, 2010 at 4:12 PM

The copy web service will copy files between document libraries, but there's no really good way to copy attachments from one list to another.

I ran into the exact same thing that you're trying to do and was unable to obtain a really good solution. We actually wanted to attach files to two lists (as part of a bulk loading operation) so that when you attached a file it'd automatically be put on both lists. In the end we went with a document library approach instead of lists, and used a client side Java app to do the bulk load for us (encoding to binary then streaming to the web service).

Obviously you have a solid reason for needing to duplicate data in this way. I never like this type of approach because you end up with the same data in two different places, or even worse, the two versions get out of sync.

I know Marc is thinking on this... I'll ponder it too and see if there's any potential solutions that we hadn't tried.

Coordinator
May 10, 2010 at 12:50 PM

Two words; Mother's Day.  (At least that was yesterday here in the US and in my house.)  I didn't get to this at all over the weekend; sorry.  It's still on my list of things to look at, though.

M.

May 13, 2010 at 7:42 AM

I also tried to do something with attachments and gave up. I took the same document library approach but created a note field in the list that holds the links to the documents. That way, when I copy the list item, I just copy the note field and it carries the links with it.

Jun 19, 2013 at 6:29 AM
Edited Jun 19, 2013 at 6:30 AM
ITs almost 3 Years but it helped me lot especially Rich text editor update issue i.e. creating function escapeHTMLEncode(str)

Thanks Mark for all your work which your shared. Please highlight the above issues for updating in your blog so it will be easily found and will great help to others

Thanks once again