Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I've written a tool top copy/paste/delete files accross site collections and then remove the original version from the recycle bin.

The method to copy the files is:

public bool MoveListItemsSiteToSite(string sourceSiteURL, string sourceList, string destinationSiteURL, string destinationList, bool retainMeta, int daysOlder)
        {
            OnSharePointOpeningSite(sourceSiteURL);

            using (SPSite sourceSite = new SPSite(sourceSiteURL))
            {
                OnSharePointOpenedSite(sourceSiteURL);

                using (SPWeb sourceWeb = sourceSite.OpenWeb())
                {
                    OnSharePointGetRelativeURL(sourceWeb.ServerRelativeUrl);

                    try
                    {
                        // Get your source library
                        var source = sourceWeb.GetList(sourceWeb.ServerRelativeUrl + sourceList);

                        OnSharePointSourceSet(source);

                        // Get the collection of items to move, use source.GetItems(SPQuery) if you want a subset
                        SPListItemCollection items = source.Items;

                        int fileCount = 0;
                        int skippedFiles = 0;

                        OnSharePointOpeningSite(destinationSiteURL);

                        using (var destSite = new SPSite(destinationSiteURL))
                        {
                            OnSharePointOpenedSite(destinationSiteURL);

                            using (var destinationWeb = destSite.OpenWeb())
                            {
                                OnSharePointGetRelativeURL(destinationWeb.ServerRelativeUrl);

                                // get destination library
                                SPList destination = destinationWeb.GetList(destinationWeb.ServerRelativeUrl + destinationList);

                                OnSharePointDestinationSet(destination);

                                // Get the root folder of the destination we'll use this to add the files
                                SPFolder destinationFolder = destinationWeb.GetFolder(destination.RootFolder.Url);

                                OnSharePointProcessItem(items.Count, source.ToString(), destination.ToString());

                                // Now to move the files and the metadata
                                foreach (SPListItem item in items)
                                {
                                    //Get the file associated with the item
                                    SPFile file = item.File;

                                    // check that file is older then the pre-determined days (aging)
                                    if (item.File.TimeCreated < DateTime.Today.AddDays(-daysOlder))
                                    {
                                        // Add event handler
                                        OnSharePointProcessFile(destinationFolder.Url + "/" + file.Name);

                                        // Create a new file in the destination library with the same properties
                                        SPFile newFile = destinationFolder.Files.Add(destinationFolder.Url + "/" + file.Name, file.OpenBinary(),
                                                                                     file.Properties, true);

                                        if (retainMeta)
                                        {
                                            SPListItem newItem = newFile.Item;
                                            WriteFileMetaDataFiletoFile(item, newItem);
                                        }

                                        file.Delete();

                                        SharePointRecycleBin.EmptyRecycleBinItem(sourceSite.Url, source.ToString(), file.Name);

                                        fileCount++;
                                    }
                                    else
                                    {
                                        OnSharePointProcessFile("Skipping file as not older than " + daysOlder + " days:" + destinationFolder.Url + "/" + file.Name);
                                        skippedFiles++;
                                    }

                                }
                                OnSharePointProcessList(fileCount, skippedFiles, source.ToString(), destination.ToString());
                            }
                        }
                    }
                    catch (System.IO.FileNotFoundException fex)
                    {
                        OnError("Unable to set a location. Please check that paths for source and destination libraries are correct and relative to the site collection. \n\nSource Site: " 
                                    + sourceSiteURL + " \nSource List: " + sourceList + " \nDestination Site: " + destinationSiteURL + " \nDestination List: " + destinationList + "\n", false, fex);

                        return false;
                    }
                    catch (Exception ex)
                    {
                        OnError("General Exception: ", true, ex);

                        return false;
                    }

                    return true;
                }
            }
        }

and the method to empty the recycle bin is:

public static void EmptyRecycleBinItem(string siteCollection, string originalLocation, string filename)
        {
            try
            {
                using (var site = new SPSite(siteCollection))
                {
                    site.RecycleBin.Delete((from SPRecycleBinItem item
                                            in site.RecycleBin
                                            where item.DirName.Equals(originalLocation, StringComparison.OrdinalIgnoreCase)
                                                  && item.LeafName.Equals(filename, StringComparison.OrdinalIgnoreCase)
                                            select item.ID).ToArray());
                }
            }
            catch (Exception ex)
            {
                throw new UnableToEmptyRecycleBin(filename, ex);
            }

        }

This moved about 6000 (out of a batch of 30000) successfully until it failed on one particular file with the following error:

2012-10-25 09:25:40.2150 
 *** Log Site *** 
 QIC.RE.SharePoint.Archive.ProcessArchiveQueue.listsAndItems_OnError 
 *** Message *** 

[9:25 AM] General Exception:  Cannot remove file "CentrePO_PPA0142012_1200_20000_2011-09-26T11_01_42.xml". Error Code: 16388.

 Microsoft.SharePoint.SPException: Cannot remove file "CentrePO_PPA0142012_1200_20000_2011-09-26T11_01_42.xml". Error Code: 16388. ---> System.Runtime.InteropServices.COMException (0x81070207): Cannot remove file "CentrePO_PPA0142012_1200_20000_2011-09-26T11_01_42.xml". Error Code: 16388.
   at Microsoft.SharePoint.Library.SPRequestInternalClass.AddOrDeleteUrl(String bstrUrl, String bstrDirName, Boolean bAdd, UInt32 dwDeleteOp, Int32 iUserId, Guid& pgDeleteTransactionId)
   at Microsoft.SharePoint.Library.SPRequest.AddOrDeleteUrl(String bstrUrl, String bstrDirName, Boolean bAdd, UInt32 dwDeleteOp, Int32 iUserId, Guid& pgDeleteTransactionId)
   --- End of inner exception stack trace ---
   at Microsoft.SharePoint.Library.SPRequest.AddOrDeleteUrl(String bstrUrl, String bstrDirName, Boolean bAdd, UInt32 dwDeleteOp, Int32 iUserId, Guid& pgDeleteTransactionId)
   at Microsoft.SharePoint.SPFile.DeleteCore(DeleteOp deleteOp)
   at Microsoft.SharePoint.SPFile.Delete()
   at QIC.RE.SharePoint.ListsAndItems.MoveListItemsSiteToSite(String sourceSiteURL, String sourceList, String destinationSiteURL, String destinationList, Boolean retainMeta, Int32 daysOlder) 
 *** Exception Details *** 

 *** Exception.ToString() *** 

I've googled the error however I can't find a clear explanation as to what it is. Has anybody come accross this before or is familiar with this code? I'm hesitant to start the archiver again until I understand this and I can't reproduce the event in my test/development environments to find out more about it.

share|improve this question
    
Micheal, is the file checked-out or being used? It may be possible that the file is locked out for some reason! –  Arsalan Adam Khatri Oct 25 '12 at 0:58
    
Sorry, should have mentioned that. No, it's not checked out - I was abale to repeat the process that the archiver would be automating manually with no issues. –  ElvisLikeBear Oct 25 '12 at 1:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.