Tuesday, July 15, 2014

How to move SharePoint list items to a folder and subfolders in the same list using C# programmatically?

It was really simple at the end, but it took my whole day for writing the utility to achieve the desired results.

However, the tricky part of the utility was how i can move the items to a sub folder of the list.

For example there is a folder 2014 and it contains 12 folders of each month. You want to move the item inside a folder.


Keep in mind the hierarchy of SharePoint. Follow below steps.

1. Get SPWeb
2. Get SPList
3. Get List Folders
4. Get Sub Folders of Folders
5. Copy the Item

I have divided the steps into Methods. Code below.

Get Site,Web and list Objects and Passing them to a method InsertItemIntoFolder.

using (SPSite site = new SPSite("Your Site URL"))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPList sourceList = web.Lists.TryGetList("Your List Name");
        List<Guid> IID = new List<Guid>();
        foreach (SPListItem item in sourceList.Items)
        {
        InsertItemIntoFolder(sourceList, item, "2014", "July");
        IID.Add(item.UniqueId);
        }
    }
}

Now inserting item in the folders. 

Note: Make sure your folders have been already created in the desired structure.

public static void InsertItemIntoFolder(SPList sourceList, SPListItem item, string year, string month)
        {
            foreach (SPFolder folder in sourceList.RootFolder.SubFolders)
            {
                if (folder.Name == year)
                {
                    foreach (SPFolder SubFolder in folder.SubFolders)
                    {
                        if (SubFolder.Name == month)
                        {
                            try
                            {
                                SPListItem listItem = sourceList.Items.Add(SubFolder.ServerRelativeUrl, SPFileSystemObjectType.File);
                                listItem["Title"] = item["Title"];
                                listItem["Author"] = item["Author"];
                                listItem["Modified"] = item["Modified"];
                                listItem["Editor"] = item["Editor"];
                                listItem.Update();
                            }
                            catch(Exception)
                            { }
                        }
                    }
                }
            }
        }

 Your item will be copied successfully. Now you can delete the original item.

                for (int i = 0; i < IID.Count; i++)
                        {
                            SPListItem item = sourceList.GetItemByUniqueId(IID[i]);
                            item.Delete();
                        }
                        sourceList.Update();

Any questions, please put in comments. 

Regards,

1 comment:

  1. Thanks so much! Your instructions are very clear and helpful. I've been wondering how to do this for months and thrilled to find out how finally!
    SharePoint 2013 Online Training

    ReplyDelete