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

Based on this question I wrote the following in my class for emptying the recycle bin:

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);
    }
}

Unfortunately this throws a URL error, specifically:

System.UriFormatException: Invalid URI: The URI scheme is not valid.
at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind) at Microsoft.SharePoint.SPSite..ctor(String requestUrl)
at Testing.RE.SharePoint.SharePointRecycleBin.EmptyRecycleBinItem(String siteCollection, String originalLocation, String filename) in C:\Dev\Testing.RE\Testing.RE.SharePoint\Testing.RE.SharePoint\SharePointRecycleBin.cs:line 57 --- End of inner exception stack trace --- at Testing.RE.SharePoint.SharePointRecycleBin.EmptyRecycleBinItem(String siteCollection, String originalLocation, String filename) in C:\Dev\Testing.RE\Testing.RE.SharePoint\Testing.RE.SharePoint\SharePointRecycleBin.cs:line 78 at Testing.RE.SharePoint.ListsAndItems.MoveListItems(String siteURL, String sourceList, String destinationList, Boolean retainMeta) in C:\Dev\Testing.RE\Testing.RE.SharePoint\Testing.RE.SharePoint\ListsAndItems.cs:line 80 * Exception Details *

I've checked, and I'm passing strings into the method. I even simplified this down to the following, but still received the exception:

using (var site = new SPSite(siteCollection))
{
    var idsToDelete = new List<Guid>(); 

    foreach (SPRecycleBinItem item in site.RecycleBin)
    {
        if(item.LeafName.Equals(filename, StringComparison.OrdinalIgnoreCase))
        {
            idsToDelete.Add(item.ID);
        }
    }

    site.RecycleBin.Delete(idsToDelete.ToArray());
}

I also tried changing:

if(item.LeafName.Equals(filename, StringComparison.OrdinalIgnoreCase))

To be:

if (item.Title.Equals(filename, StringComparison.OrdinalIgnoreCase))

But had no luck.

The full class that calls this method (so you can see that I am, in fact, passing strings) is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Diagnostics;

// todo: Lists
// DeleteListItem
// WriteFileMetadata
// AddListEntry

// Throttling on copy methods

namespace Testing.RE.SharePoint
{
    /// <summary>
    /// Class to encapsulate methods that interact with SharePoint Lists and Libraries
    /// </summary>
    public class ListsAndItems
    {
        /// <summary>
        /// Move items from a list to another list in the same site
        /// </summary>
        /// <param name="siteURL">URL to the host site collection</param>
        /// <param name="sourceList">URL to the source list</param>
        /// <param name="destinationList">URL to the destination list</param>
        /// <param name="retainMeta">Option to retain meta data (created/modified dates) or create new ones</param>
        /// <returns>Boolean value, true for successful copy, false for a failed copy</returns>
        public bool MoveListItems(string siteURL, string sourceList, string destinationList, bool retainMeta)
        {
            OnSharePointOpeningSite(siteURL);

            using (var site = new SPSite(siteURL))
            {
                OnSharePointOpenedSite(siteURL);

                using (var web = site.OpenWeb())
                {
                    OnSharePointGetRelativeURL(web.ServerRelativeUrl);

                    try
                    {
                        // Get your source and destination libraries
                        var source = web.GetList(web.ServerRelativeUrl + sourceList);
                        var destination = web.GetList(web.ServerRelativeUrl + destinationList);

                        OnSharePointSourceSet(source);
                        OnSharePointDestinationSet(destination);

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

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

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

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

                            OnSharePointProcessFile(folder.Url + "/" + file.Name);

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

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

                            file.Delete();
                            SharePointRecycleBin.EmptyRecycleBinItem(site.ToString(), source.ToString(), file.Name.ToString());

                            fileCount++;
                        }

                        OnSharePointProcessList(fileCount, 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\nSite URL: " 
                                    + siteURL + " \nSource List: " + sourceList + " \nDestination List: " + destinationList + "\n", false, fex);

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

                        return false;
                    }

                    return true;
                }
            }
        }

        /// <summary>
        /// Move items from one SharePoint list to another in another site
        /// </summary>
        /// <param name="sourceSiteURL">The URL to the source site collection</param>
        /// <param name="sourceList">The URL to the source list</param>
        /// <param name="destinationSiteURL">The URL to the destination site collection</param>
        /// <param name="destinationList">The URL to the destination list</param>
        /// <param name="retainMeta">Option to retain meta data (created/modified dates) or create new ones</param>
        /// <returns>Boolean value, true for successful copy, false for a failed copy</returns>
        public bool MoveListItemsSiteToSite(string sourceSiteURL, string sourceList, string destinationSiteURL, string destinationList, bool retainMeta)
        {
            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;

                        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;

                                    // 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.ToString(), source.ToString(), file.Name);

                                    fileCount++;
                                }
                                OnSharePointProcessList(fileCount, 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;
                }
            }
        }

        /// <summary>
        /// overwrites a list items meta data with meta data from another file
        /// </summary>
        /// <param name="sourceItem">The source item to take meta data from</param>
        /// <param name="destinationItem">The destination item to set meta data from the source item to</param>
        public static void WriteFileMetaDataFiletoFile(SPListItem sourceItem, SPListItem destinationItem)
        {
            destinationItem["Editor"] = sourceItem["Editor"];
            destinationItem["Modified"] = sourceItem["Modified"];
            destinationItem["Modified By"] = sourceItem["Modified By"];
            destinationItem["Author"] = sourceItem["Author"];
            destinationItem["Created"] = sourceItem["Created"];
            destinationItem["Created By"] = sourceItem["Created By"];

            destinationItem.UpdateOverwriteVersion();
        }


        #region Events

        internal void OnSharePointProcessFile(string itemPath)
        {           
            if (_sharePointProcessedFile == null) return;

            var e = new SharePointProcessFileEventArgs(itemPath);
            _sharePointProcessedFile(this, e);
        }

        internal void OnSharePointProcessItem(int itemCount, string source, string destination)
        {
            if (_sharePointProcessItem == null) return;

            var e = new SharePointProcessItemEventArgs(itemCount, source, destination);

            _sharePointProcessItem(this, e);
        }


        internal void OnSharePointProcessList(int itemCount, string source, string destination)
        {
            if (_sharePointProcessList == null) return;

            var e = new SharePointProcessListEventArgs(itemCount, source, destination);

            _sharePointProcessList(this, e);
        }


        internal void OnSharePointOpeningSite(string siteName)
        {
            if (_sharePointOpeningSite == null) return;

            var e = new SharePointOpeningSiteEventArgs(siteName);

            _sharePointOpeningSite(this, e);
        }

        internal void OnSharePointOpenedSite(string siteName)
        {
            if (_sharePointOpenedSite == null) return;

            var e = new SharePointOpenedSiteEventArgs(siteName);

            _sharePointOpenedSite(this, e);
        }

        internal void OnSharePointGetRelativeURL(string siteCollection)
        {
            if (_sharePointRelativeURL == null) return;

            var e = new SharePointWebRelativeURLEventArgs(siteCollection);

            _sharePointRelativeURL(this, e);
        }

        internal void OnSharePointDestinationSet(SPList destination)
        {
            if (_sharepointDestination == null) return;

            var e = new SharePointDestinationSetEventArgs(destination);

            _sharepointDestination(this, e);
        }

        internal void OnSharePointSourceSet(SPList source)
        {
            if (_sharepointSource == null) return;

            var e = new SharePointSourceSetEventArgs(source);

            _sharepointSource(this, e);
        }

        internal void OnError(string message, bool showException, Exception exception)
        {
            if (_sharePointOnError == null) return;

            var e = new SharePointOnErrorEventsArgs(message, showException, exception);

            _sharePointOnError(this, e);
        }

        private EventHandler<SharePointProcessFileEventArgs> _sharePointProcessedFile;
        private EventHandler<SharePointProcessListEventArgs> _sharePointProcessList;
        private EventHandler<SharePointOpeningSiteEventArgs> _sharePointOpeningSite;
        private EventHandler<SharePointOpenedSiteEventArgs> _sharePointOpenedSite;
        private EventHandler<SharePointWebRelativeURLEventArgs> _sharePointRelativeURL;
        private EventHandler<SharePointDestinationSetEventArgs> _sharepointDestination;
        private EventHandler<SharePointSourceSetEventArgs> _sharepointSource;
        private EventHandler<SharePointProcessItemEventArgs> _sharePointProcessItem;
        private EventHandler<SharePointOnErrorEventsArgs> _sharePointOnError;

        /// <summary>
        /// Event for handling exceptions
        /// </summary>
        public event EventHandler<SharePointOnErrorEventsArgs> SharePointOnError
        {
            add { _sharePointOnError += value; }
            remove { _sharePointOnError += value; }
        }

        /// <summary>
        /// Event for when a file is being processed
        /// </summary>
        public event EventHandler<SharePointProcessFileEventArgs> SharePointProcessFile
        {
            add { _sharePointProcessedFile += value; }
            remove { _sharePointProcessedFile += value; }
        }

        /// <summary>
        /// Event for when a site is attempting to open
        /// </summary>
        public event EventHandler<SharePointOpeningSiteEventArgs> SharePointOpeningSite
        {
            add { _sharePointOpeningSite += value; }
            remove { _sharePointOpeningSite -= value; }
        }

        /// <summary>
        /// Event for when a site has been successfully opened
        /// </summary>
        public event EventHandler<SharePointOpenedSiteEventArgs> SharePointOpenedSite
        {
            add { _sharePointOpenedSite += value; }
            remove { _sharePointOpenedSite -= value; }
        }

        /// <summary>
        /// Event for when source/destination and filecount are established and a copy is about to initiate
        /// </summary>
        public event EventHandler<SharePointProcessItemEventArgs> SharePointProcessItem
        {
            add { _sharePointProcessItem += value; }
            remove { _sharePointProcessItem -= value; }
        }

        /// <summary>
        /// Event for when a list has started processing
        /// </summary>
        public event EventHandler<SharePointProcessListEventArgs> SharePointProcessList
        {
            add { _sharePointProcessList += value; }
            remove { _sharePointProcessList -= value; }
        }

        /// <summary>
        /// Event for when a web relative URL has been retreived from the site collection name
        /// </summary>
        public event EventHandler<SharePointWebRelativeURLEventArgs> SharePointWebRelativeURL
        {
            add { _sharePointRelativeURL += value; }
            remove { _sharePointRelativeURL -= value; }
        }

        /// <summary>
        /// Event for when a destination location has been assigned
        /// </summary>
        public event EventHandler<SharePointDestinationSetEventArgs> SharePointDestinationSet
        {
            add { _sharepointDestination += value; }
            remove { _sharepointDestination -= value; }
        }

        /// <summary>
        /// Event for when a source location has been assigned
        /// </summary>
        public event EventHandler<SharePointSourceSetEventArgs> SharePointSourceSet
        {
            add { _sharepointSource += value; }
            remove { _sharepointSource -= value; }
        }

    #endregion 
    }
}

What is this error telling me and what would be the proposed resolution?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

The problem is not with your recycle bin code - at least not yet. According to the error message, the code is failing when trying to construct the SPSite instance:

System.UriFormatException: Invalid URI: The URI scheme is not valid. at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind) at Microsoft.SharePoint.SPSite..ctor(String requestUrl)

In your class, change:

SharePointRecycleBin.EmptyRecycleBinItem(site.ToString(), source.ToString(), file.Name.ToString());

to:

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

You may also want to consider an override of EmptyRecycleBinItem that accepts an instance of SPSite as the first parameter, rather than a string in the form of a URL. That way you won't have to pass the URL from one SPSite instance just to create another within EmptyRecycleBinItem.

share|improve this answer

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.