Sitefinity CMS

Managing Files with LibraryManager Send comments on this topic.
See Also
Developing with Sitefinity > Modules > Modules API > Generic Content Based Modules > Images and Documents > Files > Managing Files with LibraryManager

Glossary Item Box

 These methods are part of the LibraryManager class and are used to manage images and documents. 

 

Images

Get the number of image files in a given library:

ImagesCount(Guid libraryId) Copy Code
// create new instance of LibraryManager
Telerik.Libraries.LibraryManager libraryManager = new Telerik.Libraries.LibraryManager();
// get all libraries
IList listOfAllLibraries = libraryManager.GetAllLibraries();
if (listOfAllLibraries.Count > 0)
{
   
// get first library
   
Telerik.Libraries.ILibrary firstLibrary = (Telerik.Libraries.ILibrary)listOfAllLibraries[1];
   
// check if type of library instance could have image files
   
if (firstLibrary.TypeName == "Image" || firstLibrary.TypeName == "Custom")
   {
       
// get number of images
       
int imagesNumber = libraryManager.ImagesCount(firstLibrary.ID);
       Response.Write(firstLibrary.Name +
" has the following number of images: " + imagesNumber.ToString() + "<br />");
   }
   
else
       
Response.Write("This type of library could not contain image files");
}

 

Get the number of image files in a given library:

The GetImages(int from, int max, string sortExp, Guid[] parentIDs) method returns all content items that have the following images extensions: .jpg, .jpeg, .png, .gif.

The GetImages method does not check the allowed extensions for images from the web.config file but rather checks against the pre-defined ones:  .jpg, .jpeg, .png, .gif.  

At the same time, the GetDocuments(int from, int max, string sortExp, Guid[] parentIDs) method gets all files with extensions different from the image ones. If the parentIDs array has a library ID, both methods will return the files for the specified library only. If there is no value, all files with the above-mentioned extensions will be returned. 

GetImages(int from, int max, string sortExp, Guid[] parentIDs) Copy Code
// create new instance of LibraryManager
Telerik.Libraries.LibraryManager libraryManager = new Telerik.Libraries.LibraryManager();
// get all libraries
IList listOfAllLibraries = libraryManager.GetAllLibraries();
// create an array of library IDs and populate it
Guid[] parentIDArray = new Guid[5];
parentIDArray
[0] = ((Telerik.Libraries.ILibrary)listOfAllLibraries[0]).ID;
parentIDArray
[1] = ((Telerik.Libraries.ILibrary)listOfAllLibraries[1]).ID;
// get images in ascending order for Name meta key,
// get all items ('0,0'),
// where the parent IDs are passed in an array
IList listOfImages = libraryManager.GetImages(0, 0, "Name ASC", parentIDArray);
if (listOfImages.Count > 0)
{
   
foreach (Telerik.Cms.Engine.IContent contentItem in listOfImages)
       Response.Write(
"Image is named " + contentItem.GetMetaData("Name") + "<br />");
}  

 

Documents

Get the number of document files in a given library:

DocumentsCount(Guid libraryId) Copy Code
// create new instance of LibraryManager
Telerik.Libraries.LibraryManager libraryManager = new Telerik.Libraries.LibraryManager();
// get all libraries
IList listOfAllLibraries = libraryManager.GetAllLibraries();
if (listOfAllLibraries.Count > 0)
{
   
// get first library
   
Telerik.Libraries.ILibrary firstLibrary = (Telerik.Libraries.ILibrary)listOfAllLibraries[0];
   
// check if type of library instance could have document files
   
if (firstLibrary.TypeName == "Document" || firstLibrary.TypeName == "Custom")
   {
       
// get number of documents
       
int docsNumber = libraryManager.DocumentsCount(firstLibrary.ID);
       Response.Write(firstLibrary.Name +
" has the following number of documents: " + docsNumber.ToString() + "<br />");
   }
   
else
       
Response.Write("This type of library could not contain document files");
}

 

Get the number of image files in a given library:

The GetDocuments(int from, int max, string sortExp, Guid[] parentIDs) method gets all files with extensions different from the image ones (.jpg, .jpeg, .png, .gif). If the parentIDs array has a library ID, the method will return the files for the specified library only. If there is no value, all files that do not have these extensions - .jpg, .jpeg, .png, .gif - will be returned. 

GetDocuments(int from, int max, string sortExp, Guid[] parentIDs) Copy Code
// create new instance of LibraryManager
Telerik.Libraries.LibraryManager libraryManager = new Telerik.Libraries.LibraryManager();
// get all libraries
IList listOfAllLibraries = libraryManager.GetAllLibraries();
// create an array of library IDs and populate it
Guid[] parentIDArray = new Guid[5];
parentIDArray
[0] = ((Telerik.Libraries.ILibrary)listOfAllLibraries[0]).ID;
parentIDArray
[1] = ((Telerik.Libraries.ILibrary)listOfAllLibraries[1]).ID;
// get documents in ascending order for Name meta key,
// get all items ('0,0'),
// where the parent IDs are passed in an array
IList listOfDocuments = libraryManager.GetDocuments(0, 0, "Name ASC", parentIDArray);
if (listOfDocuments.Count > 0)
{
   
foreach (Telerik.Cms.Engine.IContent contentItem in listOfDocuments)
       Response.Write(
"Document is named " + contentItem.GetMetaData("Name") + "<br />");
}  

 

Uploading Files 

There are two overloads of the method UploadFile in LibraryManager:

  • UploadFile(byte[] buffer, string fileName, string fileExtension, string mimeType, long streamLength, ILibrary library) - uploads the file in binary (buffer) and some properties
  • UploadFile(byte[] buffer, string fileName, string fileExtension, string mimeType, long streamLength, ILibrary library, bool overwrite) - uploads the file in binary (buffer) and some properties. The only difference between the two overloads is that this method contains the boolean parameter overwrite. When true, this method saves the new file and overwrites an existing file with the same properties. If false, the method does not remove the existing file but instead adds a bracket with a number to the name of the new file. Example: Custom Image (existing file) and Custom Image (1) (new file).

 

An example of applying the first method is provided in Using ContentManager with Libraries where an image file is uploaded and saved.

 

See Also