Sitefinity CMS

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

Glossary Item Box

Most of the methods available in the ContentManager class could be used to manage libraries. For more information on how to work with the ContentManager methods, see Generic Content API Overview of Generic Content API.

 

This topic will provide examples to demonstrate how to use the methods provided by the LibraryManager class. Also, there are examples to show how to manage libraries while at the same time applying ContentManager methods.

  

All Generic Content based modules work with data of type IContent. However, there is an important difference between the Images and Documents module and all other Generic Content based modules - the Images and Document module's content items (images and documents) have binary value while the content items of the other modules have text values.

 

The following example demonstrates how to create an image and add it to a library. It uses the RadUpload control to upload the image file.

RadUpload is one of Telerik's RadControls for ASP.Net AJAX. For more information, see RadControls for ASP.Net AJAX
C# Copy Code
protected void Button1_Click(object sender, EventArgs e)
{
// upload files using RadUpload
foreach (Telerik.Web.UI.UploadedFile file in RadUpload1.UploadedFiles)
{
   file.SaveAs(
"c:\\uploaded files\\" + file.GetName(), true);
}
// create new instance of LibraryManager
Telerik.Libraries.LibraryManager libraryManager = new Telerik.Libraries.LibraryManager();
// get library by specified Name
Telerik.Libraries.ILibrary parentLibrary = libraryManager.GetLibrary("Summer Photoes");
// create an image content item
Telerik.Cms.Engine.IContent testContent = libraryManager.CreateContent("image/jpeg");
// specify parent library the image will belong to
testContent.ParentID = parentLibrary.ID;
//get the first uploaded file
Telerik.Web.UI.UploadedFile firstFile = RadUpload1.UploadedFiles[0];
byte[] buffer = new byte[firstFile.InputStream.Length];
long position = 0;
int currentByte = firstFile.InputStream.ReadByte();
while (currentByte != -1)
{
   buffer[position++] = (
byte)currentByte;
   currentByte = firstFile.InputStream.ReadByte();
}
Telerik.Cms.Engine.IContent content =
null;
// upload the file
try
{
   
content = libraryManager.UploadFile(buffer,
   firstFile.GetNameWithoutExtension(),
   firstFile.GetExtension(),
   firstFile.ContentType,
   firstFile.ContentLength,
   parentLibrary);
}
catch (ArgumentException ex)
{
   Response.Write(
"There is an exception");
}
// save the image as a content of the file
testContent.Content = buffer;
Response.Write(testContent.GetMetaData(
"Name") + "<br />");
}

 

ASPX Copy Code
<telerik:radupload id="RadUpload1" runat="server" initialfileinputscount="3" allowedfileextensions=".jpg, .jpeg, .png, .gif" />
<
asp:button id="buttonSubmit" runat="server" cssclass="RadUploadButton" onclick="Button1_Click" text="Submit" style="MARGIN-TOP: 6px" />

 

Although LibraryManager uses ContentManager class methods, which includes GetMetaData and SetMetaData, the Libraries module has specific meta keys which differ from the Generic Content ones. This means that when using the two methods for meta keys, you should use the Libraries meta keys. Here they are:

  • Name
  • Width
  • Height
  • Size
  • Extension
  • AlternateText
  • Author
  • Description

 

 

See Also