Copy the Sharepoint list item (splistitem) to another list

Source: Internet
Author: User

Theoretically, there is an incredible solution: splistitem provides a copyto (destinationurl) method (refer to msdn ). Unfortunately, this method does not seem to work. At least for me (a custom list with attachments. It always tells me that I cannot find the source list item, I have no read permission, or the list is not published. I found many posts on the Internet, and other people encountered the same problem. The best solution is to implement it by yourself.

First, the parameters and return value types of the design method are as follows:

public static SPListItem CopyItem(SPListItem sourceItem,string destinationListName)

The first part of the content is to create a target list item. Copy the field of the source list item to the target item.

// Copy sourceitem to destinationlistsplist destinationlist = sourceitem. web. lists (destinationlistname); splistitem targetitem = destinationlist. items. add (); foreach (spfield F in sourceitem. fields) {If (! F. readonlyfield & F. internalname! = "Attachments") {targentitem [F. internalname] = sourceitem [F. internalname];}

The read-only fields and attachments are skipped in the code. We will handle the attachment as follows:

// Copy the attachment foreach (string filename in sourceitem. attachments) {spfile file = sourceitem. parentlist. parentweb. getFile (sourceitem. attachments. urlprefix + filename); byte [] imagedata = file. openbinary (); targetitem. attachments. add (filename, imagedata );}

Next, you only need to submit the target item to the database and return it.

// Save targetitemtargetitem. Update (); Return targetitem;

The calling method for this method is roughly as follows:

Splistitem approveditem = commonfunctions. copyitem (item, "target list name ");

To facilitate copy, the complete code is listed below:

Public static splistitem copyitem (splistitem sourceitem, string destinationlistname) {// copy sourceitem to destinationlist splist destinationlist = sourceitem. web. lists [destinationlistname]; splistitem targetitem = destinationlist. items. add (); foreach (spfield F in sourceitem. fields) {If (! F. readonlyfield & F. internalname! = "Attachments") {targetitem [F. internalname] = sourceitem [F. internalname] ;}}// copy the attachment foreach (string filename in sourceitem. attachments) {spfile file = sourceitem. parentlist. parentweb. getFile (sourceitem. attachments. urlprefix + filename); byte [] imagedata = file. openbinary (); targetitem. attachments. add (filename, imagedata);} targetitem. update (); Return targetitem ;}

For further tracking of target items. By default, splistitem in SharePoint has a _ copysource field and a corresponding copysource attribute. It should be used in combination with the copyto method. Unfortunately, they are all read-only. It cannot be used by me. For this reason, muhimbi creates a new field -- _ m_copysource in the target list to implement similar functions:

Splist sourcelist = myworkflow. list; splist destinationlist = myworkflow. web. lists [myworkflow. parameter1 as string]; splistitem sourceitem = myworkflow. item; // first check whether the custom source field exists in the target list if (destinationlist. fields. containsfield ("_ m_copysource") = false) {spfield Newfield = destinationlist. fields. createnewfield ("text", "_ m_copysource"); Newfield. hidden = true; destinationlist. fields. add (Newfield);} // check Whether the list item to be updated exists: String camlquery = "<where>" + "<EQ> <fieldref name = '_ m_copysource'/> <value type = 'text'> {0} </value> </EQ> "+" </where> "; camlquery = string. format (camlquery, sourceitem ["fileref"]); spquery query = new spquery (); query. query = camlquery; query. rowlimit = 1; // query the list splistitemcollection items = destinationlist. getitems (query); splistitem newitem = NULL; If (items. count = 0) newitem = destinatio NLIST. items. add (); else newitem = items [0]; // copy the field foreach (spfield field in sourceitem. fields) {If (newitem. fields. containsfield (field. internalname) = true & field. readonlyfield = false & field. internalname! = "Attachments") {newitem [field. internalname] = sourceitem [field. internalname] ;}}// Delete the existing attachment for (INT I = newitem. attachments. count; I> 0; I --) {newitem. attachments. delete (newitem. attachments [I-1]);} // copy all the attachments foreach (string filename in sourceitem. attachments) {spfile file = sourceitem. parentlist. parentweb. getFile (sourceitem. attachments. urlprefix + filename); byte [] imagedata = file. openbinary (); newitem. attachments. add (filename, imagedata);} // write down the source of the copy on the target item to update newitem ["_ m_copysource"] = sourceitem ["fileref"] in the future; newitem. update ();

This code is used in custom workflow activities. Implement one-way synchronous update of list items. If we add our own conditions to the query, we can update the query to meet specific conditions. It is really useful.

 

References

SharePoint listenelement (splistitem) in eine andere liste kopieren

Synchronise SharePoint lists (calendar/tasks) using the workflow power pack

How to copy splistitem from one splist to another splist

 

Theoretically, there is an incredible solution: splistitem provides a copyto (destinationurl) method (refer to msdn ). Unfortunately, this method does not seem to work. At least for me (a custom list with attachments. It always tells me that I cannot find the source list item, I have no read permission, or the list is not published. I found many posts on the Internet, and other people encountered the same problem. The best solution is to implement it by yourself.

First, the parameters and return value types of the design method are as follows:

public static SPListItem CopyItem(SPListItem sourceItem,string destinationListName)

The first part of the content is to create a target list item. Copy the field of the source list item to the target item.

// Copy sourceitem to destinationlistsplist destinationlist = sourceitem. web. lists (destinationlistname); splistitem targetitem = destinationlist. items. add (); foreach (spfield F in sourceitem. fields) {If (! F. readonlyfield & F. internalname! = "Attachments") {targentitem [F. internalname] = sourceitem [F. internalname];}

The read-only fields and attachments are skipped in the code. We will handle the attachment as follows:

// Copy the attachment foreach (string filename in sourceitem. attachments) {spfile file = sourceitem. parentlist. parentweb. getFile (sourceitem. attachments. urlprefix + filename); byte [] imagedata = file. openbinary (); targetitem. attachments. add (filename, imagedata );}

Next, you only need to submit the target item to the database and return it.

// Save targetitemtargetitem. Update (); Return targetitem;

The calling method for this method is roughly as follows:

Splistitem approveditem = commonfunctions. copyitem (item, "target list name ");

To facilitate copy, the complete code is listed below:

Public static splistitem copyitem (splistitem sourceitem, string destinationlistname) {// copy sourceitem to destinationlist splist destinationlist = sourceitem. web. lists [destinationlistname]; splistitem targetitem = destinationlist. items. add (); foreach (spfield F in sourceitem. fields) {If (! F. readonlyfield & F. internalname! = "Attachments") {targetitem [F. internalname] = sourceitem [F. internalname] ;}}// copy the attachment foreach (string filename in sourceitem. attachments) {spfile file = sourceitem. parentlist. parentweb. getFile (sourceitem. attachments. urlprefix + filename); byte [] imagedata = file. openbinary (); targetitem. attachments. add (filename, imagedata);} targetitem. update (); Return targetitem ;}

For further tracking of target items. By default, splistitem in SharePoint has a _ copysource field and a corresponding copysource attribute. It should be used in combination with the copyto method. Unfortunately, they are all read-only. It cannot be used by me. For this reason, muhimbi creates a new field -- _ m_copysource in the target list to implement similar functions:

Splist sourcelist = myworkflow. list; splist destinationlist = myworkflow. web. lists [myworkflow. parameter1 as string]; splistitem sourceitem = myworkflow. item; // first check whether the custom source field exists in the target list if (destinationlist. fields. containsfield ("_ m_copysource") = false) {spfield Newfield = destinationlist. fields. createnewfield ("text", "_ m_copysource"); Newfield. hidden = true; destinationlist. fields. add (Newfield);} // check Whether the list item to be updated exists: String camlquery = "<where>" + "<EQ> <fieldref name = '_ m_copysource'/> <value type = 'text'> {0} </value> </EQ> "+" </where> "; camlquery = string. format (camlquery, sourceitem ["fileref"]); spquery query = new spquery (); query. query = camlquery; query. rowlimit = 1; // query the list splistitemcollection items = destinationlist. getitems (query); splistitem newitem = NULL; If (items. count = 0) newitem = destinatio NLIST. items. add (); else newitem = items [0]; // copy the field foreach (spfield field in sourceitem. fields) {If (newitem. fields. containsfield (field. internalname) = true & field. readonlyfield = false & field. internalname! = "Attachments") {newitem [field. internalname] = sourceitem [field. internalname] ;}}// Delete the existing attachment for (INT I = newitem. attachments. count; I> 0; I --) {newitem. attachments. delete (newitem. attachments [I-1]);} // copy all the attachments foreach (string filename in sourceitem. attachments) {spfile file = sourceitem. parentlist. parentweb. getFile (sourceitem. attachments. urlprefix + filename); byte [] imagedata = file. openbinary (); newitem. attachments. add (filename, imagedata);} // write down the source of the copy on the target item to update newitem ["_ m_copysource"] = sourceitem ["fileref"] in the future; newitem. update ();

This code is used in custom workflow activities. Implement one-way synchronous update of list items. If we add our own conditions to the query, we can update the query to meet specific conditions. It is really useful.

 

References

SharePoint listenelement (splistitem) in eine andere liste kopieren

Synchronise SharePoint lists (calendar/tasks) using the workflow power pack

How to copy splistitem from one splist to another splist

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.