SPListItem.CopyTo Method (Microsoft.SharePoint)
Copies the item to the specified destination.

Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Syntax

Visual Basic (Declaration)
Public Sub CopyTo ( _
    destinationUrl As String _
)
Visual Basic (Usage)
Dim instance As SPListItem
Dim destinationUrl As String

instance.CopyTo(destinationUrl)
C#
public void CopyTo (
    string destinationUrl
)

Parameters

destinationUrl

A string that contains the URL of the destination to which to copy the item.

NoteNote:

The destination URL must reside on the same server on which the code is running. This copy operation does not copy to URLs on remote servers.

See Also

Tags :


Community Content

Shah Mehul
Possible Exceptions

Here is a possible exception you may recieve:

Microsoft.SharePoint.SPException: Source item cannot be found. Verify that the item exist and that you have permission to read it.
at Microsoft.SharePoint.SPCopy.CopyIntoNewItem(SPListItem src, SPFolder targetFolder, String targetUrl)
at Microsoft.SharePoint.SPCopy.CopyIntoItem(SPListItem src, String targetUrl)
at Microsoft.SharePoint.SPListItem.CopyTo(String destinationUrl)
at CopyMoveToParentFolder.Program.Main(String[] args)

Note: In MOSS 2007 it is not possible to copy/move an item from List/Folder/Folder/Item to List/Folder/Item in sitemanager (UI).

what is the possible resolution ?

Tags :

Geronimo007
System.UriFormatException

Another possible Exception, because of malformed destinationUrl

Void CreateThis(System.String, Boolean, System.UriKind)
System.UriFormatException: Invalid URI: The format of the URI could not be determined.
at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
at System.Uri..ctor(String uriString)
at Microsoft.SharePoint.SPCopy.EnsureValidUrl(String url)
at Microsoft.SharePoint.SPCopy.GetItemForUrl(String url)
at Microsoft.SharePoint.SPCopy.CopyIntoItem(SPListItem src, String targetUrl)
at Microsoft.SharePoint.SPListItem.CopyTo(String destinationUrl)

Tags :

Peter Kirchner
Additional explanation to parameter destinationUrl

destinationUrl has to be an absolute URL including the destination file name.

E.g. http://host/site/web/list/filename.aspx

Tags :

galiciarocio
Cannot copy list items to anoter list using Splistitem.copyto Need more documentation.

Tim Stewart
Other possible exceptions

InvalidOperationException: Collection was modified; enumeration operation may not execute.

System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext()

Microsoft.SharePoint.SPCopy.CopyIntoItem(string srcUrl, SPListItem target, Hashtable props, byte[] stream, bool saveStream)

Microsoft.SharePoint.SPCopy.CopyIntoItem(SPListItem src, SPListItem target)
Microsoft.SharePoint.SPCopy.CopyIntoItem(SPListItem src, string targetUrl)
Microsoft.SharePoint.SPListItem.CopyTo(string destinationUrl)

OR

Microsoft.SharePoint.SPException occurred
Message="Cannot lock file \"BPF WCD-F-0007.xml\". Either the file is in use or the Web server is temporarily busy."
Source="Microsoft.SharePoint"
ErrorCode=-2130247155
StackTrace:
at Microsoft.SharePoint.Library.SPRequest.MoveUrl(String bstrUrl, String bstrWebRelOldUrl, String bstrWebRelNewUrl, Int32 grf)
at Microsoft.SharePoint.SPFile.MoveCopyInternal(String strNewUrl, Int32 grf)
at Microsoft.SharePoint.SPFile.CopyTo(String strNewUrl)
at ChangeOrderProposal.CustomActivities.CopyDocumentsAndAttachmentsActivity.CopyDocument(SPFile document, SPList destinationList)


Sakari H
An example for using SPListItem.CopyTo
Hi

In case someone needs a bit light in using this method, here is example:

SPListItem itemToCopy; //original item to copy

string strDestinationPath= targetSite.Url + "/" + targetWeb.Name + targetLibrary.Name + itemToCopy.File.Name;
//this will look like http://localhost/mySite/myLibrary/myItem.doc , needs to be full name including extension if present

itemToCopy.CopyTo(strDestinationPath);

This should be all that is needed to copy items using this method.



Rakesh Narayan Lal
One More Possible Exception in Case when you copy file cross server
Microsoft.SharePoint.SPException was unhandled
Message="Cannot create an item at the requested destination. Verify that the folder exists and that you have permission to edit in it."
Source="Microsoft.SharePoint"
ErrorCode=-2130575221
StackTrace:
at Microsoft.SharePoint.SPCopy.CopyIntoNewItem(SPListItem src, SPFolder targetFolder, String targetUrl)
at Microsoft.SharePoint.SPCopy.CopyIntoItem(SPListItem src, String targetUrl)
at Microsoft.SharePoint.SPListItem.CopyTo(String destinationUrl)
at Tenplates.Program.Main(String[] args) in C:\Documents and Settings\dev1admin\Desktop\Tenplates\Tenplates\Program.cs:line 142
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Tags :

sbrickey
Re: verify that you have permission
regarding the SPException "Cannot create an item [...] and that you have permission to edit in it" simply means that the user which the code is running as (remember that by default code executes in the context of the user currently logged in) doesn't have full permission in the destination.

either change permission in the target, or use execute with elevated privileges.
Tags :

Page view tracker