0

How to move an item from custom list root location to sub folder programmatically (in Event Receiver (ItemAdded))?

3 Answers 3

1

You can identify a custom list's root folder using SPList.Rootfolder property and corresponding subfolders could be accessed using SPList.Rootfolder.SubFolders[index].

While adding the list item, make sure that it is added to the correct folder path.

SPListItem listitem = list.Items.Add(folder.ServerRelativeURL,SpFileSystemObjectType.File)

Sample code might look like this.

     using (SPWeb web = properties.OpenWeb())
       {
           SPList list = web.Lists["Test"];

           if (properties.List.Title == list.Title)
           {
               foreach (SPFolder folder in properties.List.RootFolder.SubFolders)
               {
                   if (folder.Name == "Specific Folder Name")
                   {
                       SPListItem listItem = list.Items.Add(folder.ServerRelativeUrl, SPFileSystemObjectType.File);
                       listItem["FieldName"] = properties.ListItem["FieldName"];
                       listItem.Update();
                   }
               }
           }

       }
0

This solution allow you to move to any folder in same or different list http://think4code.blogspot.com/2012/03/move-list-item-splistitem-to-sub-folder.html

1
0

This is probably coming too late, however, I created a couple of extension methods for this. You can simply put this in your .cs in your project and it will add these methods to your SPListItem and SPList objects.

Don't forget to set Your.Namespace to what suits you.

You can do something like this:

SPList list = null; //replace null with code to get list
list.CreateFolderRecursively("path/to/folder");

or

SPListItem item = null; // replace null with code to get list item.
item.MoveToFolder("path/to/folder");

These are the definitions of these methods.

using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Your.Namespace
{
    public static class SPListExtensions
    {

        /// <summary>
        /// Creates a folder recursively relative inside a list.
        /// </summary>
        /// <param name="list">The list within which to create the folder.</param>
        /// <param name="fullFolderUrl">The url of the folder to create relative to the list.</param>
        /// <returns>An object representing the folder that was created.</returns>
        public static SPFolder CreateFolderRecursively(this SPList list, string fullFolderUrl)
        {
            return list.CreateFolderRecursively(list.RootFolder.ServerRelativeUrl, fullFolderUrl);
        }

        /// <summary>
        /// Creates a folder recursively beginning from the <para>parentFolder</para>.
        /// </summary>
        /// <param name="list">The list to containing the folder.</param>
        /// <param name="parentFolder">The url of the parent folder relative the server.</param>
        /// <param name="fullFolderUrl">The url of the folder to create relative to the document library</param>
        /// <returns>SPFolder</returns>
        public static SPFolder CreateFolderRecursively(this SPList list, string parentFolder, string fullFolderUrl)
        {
            string[] folderUrls = fullFolderUrl.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            string folderUrl = folderUrls.Length > 0 ? folderUrls[0] : string.Empty;
            SPListItem newFolderItem = null;
            SPFolder folder = null;
            bool ret = false;

            SPSecurity.RunWithElevatedPrivileges(delegate ()
            {
                folder = list.ParentWeb.Site.OpenWeb().GetFolder(parentFolder + (!string.IsNullOrEmpty(folderUrl) ? "/" + folderUrl : string.Empty));

                if (!folder.Exists)
                {
                    newFolderItem = list.Items.Add(parentFolder, SPFileSystemObjectType.Folder, folderUrl);
                    newFolderItem["Title"] = folderUrl;
                    newFolderItem.Update();
                } else
                {
                    ret = true;
                }

                if (folderUrls.Length > 1)
                {
                    var subFolderUrl = string.Join("/", folderUrls, 1, folderUrls.Length - 1);
                    folder = list.CreateFolderRecursively((folder ?? newFolderItem.Folder).ServerRelativeUrl, subFolderUrl);
                    ret = true;
                }
            });

            if (ret)
            {
                return folder;
            }


            return newFolderItem.Folder;
        }
    }

    public static class SPListItemExtensions
    {
        /// <summary>
        /// Moves this list item from anywhere within the list to <param>folderPath</param>.
        /// </summary>
        /// <param name="list">The list within which to create the folder.</param>
        /// <param name="fullFolderUrl">The path to move the list item to. This path is relative to the root folder of this list.</param>
        /// <returns>The this list item.</returns>
        public static SPListItem MoveItemToFolder(this SPListItem item, string folderPath)
        {
            SPFolder folder = item.ParentList.CreateFolderRecursively(folderPath);
            string dest = string.Format("{0}/{1}", folder.ServerRelativeUrl, item["ows_LinkFilename"].ToString());


            item.Web.Site.OpenWeb().GetFile(item["ows_ServerUrl"].ToString()).MoveTo(dest, true);


            return item;
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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