Add Images to the Library programatically
Greetings,
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)
);
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();
Just a quick note for those who are also trying to achieve this functionality.
Ivan,
I am trying to get all the documents in a given library.
LibrariesManager libManager = LibrariesManager.GetManager();
var docs = libManager.GetDocumentLibrary().Documents();
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();
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();
this.Label1.Text = lib.Documents.Count().ToString();
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
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();
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();
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.
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);
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);
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
And it does.
Many thanks,
Andrei
Hi Ivan, I tried to use the sample you provide in this thread but get error at 'image.Parent = album;' LibrariesManager libManager = LibrariesManager.GetManager(); var manager = new LibrariesManager();
Telerik.Sitefinity.Libraries.Model. Image image = manager.CreateImage();
image.Parent = album ; //this line failed
am I missing anything?
var album = App.WorkWith().Albums().Where(al => al.Title == "album01").Get().SingleOrDefault();
var album2 = libManager.GetAlbums().Where(al=>al.Title=="album01").FirstOrDefault();
image.Parent = album2; //this line worked
thanks,
JH
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
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
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;
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
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);
_documents.OrderBy(Document => Document.Title);
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);
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
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
Victor,
Any more ideas on this one?
Cheers,
Andrei
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
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)
How do you set the Images Thumbnail?
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
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: