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

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

'Usage

Dim instance As SPListItem
Dim destinationUrl As String

instance.CopyTo(destinationUrl)

'Declaration

Public Sub CopyTo ( _
    destinationUrl As String _
)

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

Community Content

Possible Exceptions
Added by:Shah Mehul

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 ?

System.UriFormatException
Added by:Geronimo007

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)

Additional explanation to parameter destinationUrl
Added by:Peter Kirchner

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

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

Cannot copy list items to anoter list using Splistitem.copyto Need more documentation.
Added by:galiciarocio
Other possible exceptions
Added by:Tim Stewart

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)

An example for using SPListItem.CopyTo
Added by:Sakari H
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.


One More Possible Exception in Case when you copy file cross server
Added by:Rakesh Narayan Lal
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()
Re: verify that you have permission
Added by:sbrickey
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.
© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View