Add Images to the Library programatically

Posted by Community Admin on 03-Aug-2018 10:58

Add Images to the Library programatically

All Replies

Posted by Community Admin on 27-Oct-2010 00:00

Greetings,


I have a RadUpload control on a Custom Control and I need to upload that image into the Image Library Manager, to a specific album, creating the album if it doesn't exist.

Is there a way to achieve this functionality using the Facades? If so, can you provide me with an example? 

Thanks in advance,
Daniel 

Posted by Community Admin on 27-Oct-2010 00:00

Hello Daniel,

Below is a sample code that illustrates desired behavior.

var a = App.WorkWith().Albums().Where(al => al.Title == "testAlbum").Get().SingleOrDefault();
           if (a == null)
           
               Guid albumId = Guid.Empty;
               App.WorkWith().Album().CreateNew().Do(alb =>
                   
                          albumId = alb.Id;
                          alb.Title = "testAlbum";
                          alb.Visible = true;
                   )
                   .SaveChanges();
           
 
           var c = App.WorkWith().Albums().Where(al => al.Title == "testAlbum").Get().SingleOrDefault();
 
           // uploading
 
           List<ImageItem> list = new List<ImageItem>();
           var manager = new LibrariesManager();
           foreach (UploadedFile file in filesUpload.UploadedFiles)
           
               Telerik.Sitefinity.Libraries.Model.Image image = manager.CreateImage();
               image.Parent = c;
               image.Title = file.GetNameWithoutExtension();
               image.UrlName = file.GetNameWithoutExtension();
               image.Extension = file.GetExtension();
               manager.RecompileItemUrls<Telerik.Sitefinity.Libraries.Model.Image>(image);
               manager.Upload(image, file.InputStream, file.GetExtension());
 
               list.Add(
                   new ImageItem()
                   
                       Title = image.Title,
                       Url = manager.GetItemUrl(image)
                   );
           


Best wishes,
Ivan Dimitrov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 27-Oct-2010 00:00

Thank you for the reply Ivan,

I can't, however, use the Linq expression 'Where' while working with the Albums Facade. It does work with the Pages Facade but not with the Albums. Is there anything I'm probably missing?

Thank in advance,
Daniel

Posted by Community Admin on 27-Oct-2010 00:00

Hello Daniel,

The reason for the missing where method is because in the Beta 2 build there is a problem with the Albums facade. This issue has been resolved and you will be able to use it in the Release Candidate which we are going to release soon. The other approach that you can use is the native API to get albums:

LibrariesManager libManager = LibrariesManager.GetManager();
var album = libManager.GetAlbums().FirstOrDefault();


Greetings,
Radoslav Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 10-Nov-2010 00:00

Just a quick note for those who are also trying to achieve this functionality.


I was getting strange errors while attempting to use this method. After creating the Album and uploading the images and going into the Image Listing page nothing would appear and no errors would be thrown. Trying to access the albums with /Images/albums opened the albums correctly, though.

After digging a bit around I noticed I had to also call the manager.RecompileItemUrls(album) after creating the album and not only for the uploaded images. Doing so made it start working correctly.

Regards,
Daniel

Posted by Community Admin on 15-Dec-2010 00:00

Ivan,

I am trying to get all the documents in a given library.

LibrariesManager libManager = LibrariesManager.GetManager();
var docs = libManager.GetDocumentLibrary().Documents();
But .getDocumentLibarary() requires a parameter 'Guid id'. I know the name of my library 'Documents'. How do i get its Guid programatically?

Many thanks,
Andrei

Posted by Community Admin on 15-Dec-2010 00:00

Hi Andrei,

You can first get all document libraries using GetDocumentLibraries() and filter them using Where clause and a lambda expression and then get the first item in the sequence. Or you can use the Fluent API alternative:

var files = fluent.DocumentLibraries().Where(dL => dL.Title == "LibraryName").First().Documents().Get();


Best wishes,
Radoslav Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 15-Dec-2010 00:00

Radoslav,

Not heard about the Fluent API.
I am trying to use the code you wrote:

LibrariesManager libManager = LibrariesManager.GetManager();
            var lib = libManager.GetDocumentLibraries().Where(dL => dL.Title == "Documents").First();
And then as a test to see if I am getting the correct library:
this.Label1.Text = lib.Documents.Count().ToString();
I am extracting the count of how many documents there are in the library.
The answer comes back as 12 when i only have 6 documents. What am I missing here.

Many thanks,
Andrei

Posted by Community Admin on 15-Dec-2010 00:00

Hi Andrei,

You can read more about the fluent API in the bellow articles:
http://www.sitefinity.com/40/help/developer-manual/fluent-api.html
http://www.sitefinity.com/40/help/developer-manual/modules-built-in-modules-api-media-modules-documents-managing-documents-finding-documents.html

The reason you are getting more documents is because there are many states of each document. This is covered in the developer's manual and one of the above linked articles.

Best wishes,
Radoslav Georgiev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 15-Dec-2010 00:00

Radoslav,

Many thanks, I got it working now. For those interested, the code is as folows:

protected void Page_Load(object sender, EventArgs e)
    LibrariesManager libManager = LibrariesManager.GetManager();
    DocumentLibrary lib = libManager.GetDocumentLibraries()
                                    .Where(dL => dL.Title == "Documents").Single();
    int count = 0;
    foreach (Document doc in lib.Documents)
    
        if (doc.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live)
         
            // here you do anything you like with the doc.
            count++; 
        
    
    this.Label1.Text = count.ToString();
Many thanks again,
Andrei

Posted by Community Admin on 15-Dec-2010 00:00

Hello Andrei,

Thank you for sharing your code.

The same approach can be applied in the bellow piece of code, which should work faster as the query from the lambda expression will be executed on the SQL and not counting in a foreach:

LibrariesManager libManager = LibrariesManager.GetManager();
int count = libManager.GetDocumentLibraries().Where(dL => dL.Title == "Documents").First().Documents.Where(d=>d.Status==Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live).Count();


Kind regards,
Radoslav Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 15-Dec-2010 00:00

Radoslav,

Good point. SQL Server would do it faster. And since I did not really need the count but all the active documents in the library, I changed the code to implement your idea:

LibrariesManager libMan = LibrariesManager.GetManager();
IEnumerable<Document> docs = libMan.GetDocumentLibraries().Where(dL => dL.Title == "Documents").First().Documents
    .Where(d => d.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live);
foreach (Document doc in docs)
    // do whatever with the doc here.            

Many thanks again,
Andrei

Posted by Community Admin on 16-Dec-2010 00:00

Radoslav,

Guess who's back?
One more question if I may... Why does this work:

LibrariesManager libMan = LibrariesManager.GetManager();
IEnumerable<Document> docs = libMan.GetDocumentLibraries().Where(dL => dL.Title == "Documents").First().Documents
.Where(d => d.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live);
And this does not.
string _library = "Documents";
LibrariesManager libMan = LibrariesManager.GetManager();
IEnumerable<Document> docs = libMan.GetDocumentLibraries().Where(dL => dL.Title == _library).First().Documents
.Where(d => d.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live);
When I hard-code the library name, it works. When I read it from a property it comes up with the attached error.
I see nothing wrong, but because I want the user to choose which library s/he wants to connect to, I must have a property.

Many thanks in advance,
Andrei

Posted by Community Admin on 16-Dec-2010 00:00

Hello Andrei,

The problem is in the OQL parser, please try using local variables. For example if you have a property or a field _library you should instantiate a new variable in the method which is equal to the property/field. This should work.

Greetings,
Radoslav Georgiev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 16-Dec-2010 00:00

And it does.
Many thanks,
Andrei

Posted by Community Admin on 24-Jan-2011 00:00

Hi Ivan,

I tried to use the sample you provide in this thread but get error at 'image.Parent = album;'
am I missing anything?  
var
album = App.WorkWith().Albums().Where(al => al.Title == "album01").Get().SingleOrDefault();

 

 

 

LibrariesManager libManager = LibrariesManager.GetManager();
var album2 = libManager.GetAlbums().Where(al=>al.Title=="album01").FirstOrDefault();

 

 

var manager = new LibrariesManager(); 

Telerik.Sitefinity.Libraries.Model.

 

 

Image image = manager.CreateImage();

 

 

image.Parent = album ;   //this line failed
image.Parent = album2;  //this line worked

thanks,
JH

 

 

 

Posted by Community Admin on 24-Jan-2011 00:00

Hi JH,

It would be helpful if you can show us the error message you are getting. Right now we cannot tell exactly why the code is failing.

All the best,
Radoslav Georgiev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 25-Jan-2011 00:00

Hi,

attach is a snapshot where the exception occured. 
it works when reference native api variable album2.

THanks,
JH

Posted by Community Admin on 25-Jan-2011 00:00

Hello,

1. Use only one instance of LibraryManager.
2. You can work with transaction and commit the transaction at the end of your code instead of calling SaveChanges

TransactionManager.CommitTransaction("test");


All the best,
Ivan Dimitrov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 31-Jan-2011 00:00

Hi,

I am trying to upload a document to the specified Library in a similar way returning a string representation of the documentId after it is uploaded however I keep running into errors. At the moment I have this error:

[InvalidOperationException: No items in the sequence.]
   Telerik.Sitefinity.Data.Linq.Oql.OqlQueryProvider`2.ExecuteKnownType(IObjectScope scope, String queryText, Boolean isEnumerable, Int32 skip, Int32 take, IList parameters, ElementOperator op) +1011
   Telerik.Sitefinity.Data.Linq.Oql.OqlQueryProvider`2.Execute(Expression expression) +585
   Telerik.Sitefinity.Data.Linq.QueryProvider`2.System.Linq.IQueryProvider.Execute(Expression expression) +115
   System.Linq.Queryable.Single(IQueryable`1 source) +265
   Promineo.Web.UI.Fields.FormFileUpload.AddFiletoLibrary(UploadedFile file) in C:\Webinsite\Clients\Joinery-Products\Website\Promineo.Web\UI\Fields\FormFileUpload.cs:80
Here is my code:

private string AddFiletoLibrary(UploadedFile file)
        
            string val = String.Empty;
 
            var docLib = new DocumentLibrary();
 
            if (String.IsNullOrEmpty(LibraryName))
                docLib = App.WorkWith().DocumentLibraries().Get().SingleOrDefault();
            else
            
                string libName = LibraryName;
                docLib = App.WorkWith().DocumentLibraries().Where(d1 => d1.Title == libName).Get().Single();
 
                if (docLib == null)
                
                    Guid libId = Guid.Empty;
                    App.WorkWith().DocumentLibrary().CreateNew().Do(lib =>
                                                                  
                                                                      libId = lib.Id;
                                                                      lib.Title = libName;
                                                                      lib.Visible = true;
                                                                  ).SaveChanges();
 
                    docLib = App.WorkWith().DocumentLibraries().Where(d1 => d1.Title == libName).Get().SingleOrDefault();
                
            
 
            LibrariesManager libManager = LibrariesManager.GetManager();
 
            var document = libManager.CreateDocument();
 
            //set the newly created document
            document.Parent = docLib;
            document.Description = "Original Filename: " + file.GetName();
            document.UrlName = file.GetNameWithoutExtension() + DateTime.Now.ToString("_dd-MM-yy_H:mm");
            document.Title = (FileTitlePrefix + " " + file.GetNameWithoutExtension()).Trim();
            document.Extension = file.GetExtension();
            libManager.RecompileItemUrls<Document>(document);
 
            libManager.Upload(document, file.InputStream, file.GetExtension());
 
            libManager.Publish(document);
            libManager.SaveChanges();
 
            val = document.Id.ToString();
 
            return val;
        

Posted by Community Admin on 08-Feb-2011 00:00

Hi Webinsite,

It looks like the query for your parent libraries does not return any results. Can you please break point your code and see if querying parent libraries returns any results. You can try removing the Single() calls just for debugging purposes.

Greetings,
Radoslav Georgiev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 10-Mar-2011 00:00

Hello Ivan or Radoslav,

LibrariesManager libMan = LibrariesManager.GetManager();
_documents = libMan.GetDocumentLibraries().Where(dL => dL.Title == libName).First().Documents
.Where(d => d.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live);

I Wondering if there is a way to have the documents sorted by title at the server side before returning them.

I can do it this way,
_documents.OrderBy(Document => Document.Title);
but won't be as fast.

Many thanks,
Andrei
---------------------

After a quick dig I found this
string libName = _library;
LibrariesManager libMan = LibrariesManager.GetManager();
_documents = libMan.GetDocumentLibraries().Where(dL => dL.Title == libName).First().Documents
.Where(d => d.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live)
.OrderBy(d => d.Title);
at this location: http://www.sitefinity.com/documentation/how-to-create-an-advanced-silverlight-widget/implement-the-book-widget/use-the-fluent-api-to-retrieve-image-urls.aspx

but I am getting some errors.

Posted by Community Admin on 14-Mar-2011 00:00

Hello Andrei,

The last piece of code you provided is ok, because it is executed in the database not in the memory of the webserver. What errors are you getting when using the code? 


Greetings,
Victor Velev
the Telerik team

Posted by Community Admin on 14-Mar-2011 00:00

Victor,

It does not give me an error when the actual retrieval happens, but later on
when I try     
               
if (_documents.Count() > 0)
     //do something.


then I get: "Object must be of type String." and I do nothing to it in-between.
Does the sorting change the type???

Also, I different places I want to sort it by publishing date (Desc and Asce).

Many thanks,
Andrei

Posted by Community Admin on 15-Mar-2011 00:00

Victor,

Any more ideas on this one?

Cheers,
Andrei

Posted by Community Admin on 17-Mar-2011 00:00

Hi Andrei,

You can first try and create a method which checks if documents property is null and before getting them you can use sort to filter them properly.

Greetings,
Victor Velev
the Telerik team

Posted by Community Admin on 17-Mar-2011 00:00

Victor,

I am not entirely sure I follow what you are saying.

I am checking first to see if _documents is null and it is fine,
But when I get to "if (_documents.Count() > 0)" it says "Object must be of type String."
As soon as I change the filtering by publication time, it works fine. Why does it not work 
when filtering by Title? I have no idea. My code is below:

                    // Fetch all the documents in the given library.
                    string libName = _library;
                    LibrariesManager libMan = LibrariesManager.GetManager();
                    _documents = libMan.GetDocumentLibraries().Where(dL => dL.Title == libName).First().Documents
                    .Where(d => d.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live)
                    .OrderBy(d => (string)d.Title);
 
                    // Storage to store the extracted documents in.
                    List<LibraryDocument> allDocs = new List<LibraryDocument> ;
                    LibraryDocument libDoc;
 
                    if (_documents != null)
                    
                        // If the library is empty then say so.
                        if (_documents.Count() > 0)
                        


I hope you can help.
Many thanks,
Andrei

--------------------
18/03/2011@14:40
Victor do not worry. I have fixed the issue. As per the code above, it seems that adding "(string)" has cured the issue.

Many thanks for all your help,
Andrei

Posted by Community Admin on 10-Jun-2011 00:00

How do you set the Images Thumbnail?

Posted by Community Admin on 13-Jun-2011 00:00

Hi Harry,

There is a class Telerik.Sitefinity.Modules.Libraries.Images.ImagesHelper which you can use to generate thumbnails manually by passing the System.Drawing.Image object. Another option is using LibraryDataProvider and its GenerateThumbnails method.

Greetings,
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

Posted by Community Admin on 10-Jul-2014 00:00

FYI - this post is a few years old and the code didn't exactly work for me.  For others out there looking for a solution, check out this article - this code worked for me:

www.sitefinity.com/.../creating-images

This thread is closed