Sort Templates

Jul 27, 2009 at 5:12 PM

hello again !

is ist possible to sort the template-lists alphabetically ? (like windows-menüs) would be a nice feature if you have much templates in your template-folders !

Coordinator
Jul 27, 2009 at 6:48 PM

I suppose basing the file listings on views would open up some interesting possibilities. I'm afraid I don't have any eta on any new features, though - I'm pretty full up at the moment :-) Of course, if anyone else would like to pitch in, feel free to.

Jul 29, 2009 at 1:09 PM

basing the listing on views was too heavy for me - i´m not an experienced programmer. i went an other way : sorting the lists ! here is the code:

at the top in your ListViewTemplateConnector.cs:

using System.Collections.Generic;

then:

        private void AddFolderContentsToMenu(SubMenuTemplate menu, SPFolder folder, string targetFolderUrl)
        {
            List<SPFolder> folderList = new List<SPFolder>();
            foreach (SPFolder subFolder in folder.SubFolders)
            {
                // Only parse real item folders
                if (subFolder.Item == null)
                {
                    continue;
                }
                folderList.Add(subFolder);
            }
            folderList.Sort(delegate(SPFolder p1, SPFolder p2) { return p1.Name.CompareTo(p2.Name); });
            folderList.ForEach(delegate(SPFolder subFolder)
            {
                // Add a sub menu level and recurse into that
                SubMenuTemplate subMenu = menu.AddSubMenu(GetMenuItemUniqueId(), subFolder.Item.Name,
                                                          "/_layouts/images/FLDRNEW.GIF", "");
                AddFolderContentsToMenu(subMenu, subFolder, targetFolderUrl);
            });

            // Add all files from the folder
            List<SPFile> fileList = new List<SPFile>();
            foreach (SPFile file in folder.Files)
            {
                SPListItem fileItem = file.Item;
                // Ignore non-items
                if (fileItem == null)
                {
                    continue;
                }
                fileList.Add(file);
            }
            fileList.Sort(delegate(SPFile p1, SPFile p2) { return p1.Name.CompareTo(p2.Name); });
            fileList.ForEach(delegate(SPFile file)
            {
                SPListItem fileItem = file.Item;
                // Add file menu item
                menu.AddMenuItem(GetMenuItemUniqueId(),
                                 fileItem.GetValueOrDefault("Title", fileItem.File.Name),
                                 fileItem.File.IconUrl.StartsWith("ic")
                                     ? "lg_" + fileItem.File.IconUrl
                                     : fileItem.File.IconUrl,
                                 fileItem.GetValueOrDefault(
                                     _settings.DescriptionFieldName, ""),
                                 "",
                                 @"javascript:createNewDocumentWithProgID('" +
                                 fileItem.ParentList.ParentWeb.Url + "/" +
                                 fileItem.File.Url +
                                 "', '" + targetFolderUrl +
                                 "', 'SharePoint.OpenDocuments', false)");
            });
        }

 feel free to use it in your next release or anybody who wants alphabetical template-lists.

Coordinator
Aug 3, 2009 at 6:49 AM

Cool - thanks!

I'll look into this before releasing the next issue.

If anyone else wants to contribute to the subject matter, please do so and comment in this thread.

Aug 3, 2009 at 11:16 AM

well, just an idea - this is the pattern I have used to implement a test version of this feature.

You could add a parameter in the setting page and save the name of a view. Then, in the class ListViewTemplateConnector you could do something along the lines of

...<cut>

            // Add all files from the folder
            // the list of items is based on the view chosen in the setting pages
            foreach (SPListItem fileItem in 
                templateLibrary.GetItemsInFolder(templateLibrary.Views[_settings.ListViewName], folder))
            {
                SPFile file = fileItem.File;

                // Ignore non-items
                if (fileItem == null)
                {
                    continue;
                }

                // Add file menu item
                menu.AddMenuItem(GetMenuItemUniqueId(),
                                 fileItem.GetValueOrDefault("Title", fileItem.File.Name),
                                 fileItem.File.IconUrl.StartsWith("ic")
                                     ? "lg_" + fileItem.File.IconUrl
                                     : fileItem.File.IconUrl,
                                 fileItem.GetValueOrDefault(
                                     _settings.DescriptionFieldName, ""),
                                 "",
                                 @"javascript:createNewDocumentWithProgID('" +
                                 fileItem.ParentList.ParentWeb.Url + "/" +
                                 fileItem.File.Url +
                                 "', '" + targetFolderUrl +
                                 "', 'SharePoint.OpenDocuments', false)");
            }

...<cut>

 

Hope I didn't forgot anything, I have done the test when I first saw this topic and then forgot to post my discovery .. ^_^'.

Well, I will check if I added anything else when I get some time.... but I think that all you needed is that instruction "GetItemsInFolder".

Feb 16, 2010 at 12:08 PM

Hi, This is a beautiful feature and it saved my project :). I'm not a coder at all so I cannot edit the source to get the sorting working. Could it be possible to put that on the next release?

Br Juske