1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / General Discussions / How to sync existing files to file system library?

How to sync existing files to file system library?

2 posts, 1 answered
  1. Basem
    Basem avatar
    131 posts
    Registered:
    22 Dec 2010
    11 Aug 2011
    Link to this post
    I have thousands of physical files and folders within my site under ~/Docs. I am trying to use the new file system library in 4.2. So I adjusted the file system provider path in settings and created a new library called "Docs" using the file system provider in the advanced options. I was hoping to see my existing files and folders in albums and files within the Sitefinity admin in real time, but it is empty. So how do I make them show up without having to upload thousands of files within the admin?
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    12 Aug 2011
    Link to this post
    Hello Basem,

    There is no sync option out of the box that comes with 4.2 and we will consider its implementation for 4.3. Currently you can do this programmatically. Below is a sample code

    public static void CreateAlbumAndUploadImages(string rootFolderPath, string blobStorageProvider = null)
          {
              DirectoryInfo rootFolder = new DirectoryInfo(rootFolderPath);
              if (!rootFolder.Exists)
                  throw new ApplicationException(string.Format("A folder with path '{0}' does not exist", rootFolder.FullName));
     
              var manager = LibrariesManager.GetManager();
     
              var albumTitle = rootFolder.Name;
              var albumName = albumTitle.ToLower().Replace(' ', '_');
              if (manager.GetAlbums().Where(i => i.UrlName == albumName).Any())
                  throw new ApplicationException(string.Format("An album with UrlName '{0}' already exists", albumName));
     
              if (blobStorageProvider == null)
                  blobStorageProvider = Config.Get<LibrariesConfig>().BlobStorage.DefaultProvider;
     
              var album = manager.CreateAlbum();
              album.Title = albumTitle;
              album.UrlName = albumName;
     
              album.BlobStorageProvider = blobStorageProvider;
              manager.RecompileItemUrls<Album>(album);
     
              foreach (var file in rootFolder.GetFiles())
              {
                  var image = manager.CreateImage();
     
                  var extension = file.Extension;
                  var title = file.Name;
                  if (extension.Length > 0)
                      title = title.Substring(0, title.Length - extension.Length);
                  image.Parent = album;
                  image.Title = title;
                  image.UrlName = title.ToLower().Replace(' ', '-');
                  manager.RecompileItemUrls<Telerik.Sitefinity.Libraries.Model.Image>(image);
     
                  using (var fileStream = file.OpenRead())
                  {
                      manager.Upload(image, fileStream, file.Extension);
                  }
                  manager.Publish(image);
              }
              manager.SaveChanges();
     
          }

    You need to pass two parameters to the method above

    rootFolderPath - path where you store your images
    blobStorageProvider - FileSystem

    Best wishes,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
    Answered
2 posts, 1 answered