Fluent API and VB.NET
My client is a 100% VB shop and will not be switching to C#. I am trying to accomplish a relatively simple task via the fluent API. I have created a document library within Sitefinity that I would like to upload files to via the Telerik Rad Upload control. However I am having issues with the fluent API. It looks like ALL of the examples are in C# and the code conversion tool does not always give the proper result. I have done this very same process with previous versions of Sitefinity that lacked the fluent API. Up to this point the fluent API and VB have resulted in nothing but headaches. Anybody have any sample code for this seemingly simple task?
Thanks,
TC
http://www.sitefinity.com/40/help/developers-guide/sitefinity-essentials-modules-media-modules-documents-and-files-managing-document-libraries.html
Hello Tony,
Here's a sample that I just tested and it works fine:
Imports
Telerik.Sitefinity
Public
Class
WebForm1
Inherits
System.Web.UI.Page
Protected
Sub
Page_Load(
ByVal
sender
As
Object
,
ByVal
e
As
System.EventArgs)
Handles
Me
.Load
App.WorkWith().DocumentLibrary().CreateNew().
Do
(
Sub
(dL)
dL.Title =
"testLibrary"
dL.Description =
"ds"
dL.UrlName =
"testLibrary"
End
Sub
).SaveChanges()
End
Sub
End
Class
Svetoslav,
At first glance the code you send me looks like it creates a document library, I need to create a library content item that will be uploaded via a custom user control. I am running the code below. When the line that actually uploads the file is executed I am prresented with the following exception. "You are trying to access item that no longer exists. The most probable reason is that it has been deleted by another user."
Dim libraryManager As Modules.Libraries.LibrariesManager = New Modules.Libraries.LibrariesManager()
libraryManager = LibrariesManager.GetManager()
libraryManager.Provider.SuppressSecurityChecks = False
Dim libTitle As String = "TESOL Resource Center"
Dim parentLibrary As DocumentLibrary = App.WorkWith().DocumentLibraries().Where(Function(dL) dL.Title.Equals(libTitle)).[Get]().FirstOrDefault()
If RadAsyncUpload1.UploadedFiles.Count > 0 Then
For Each file As UploadedFile In RadAsyncUpload1.UploadedFiles
' Upload file
App.WorkWith().DocumentLibrary(parentLibrary.Id).CreateDocument().Do(Sub(d)
d.Title = file.FileName
End Sub).CheckOut().UploadContent(file.InputStream, file.GetExtension()).CheckInAndPublish().SaveChanges()
' Get files GUID in the library and store it in the DB
Next
End If
Hello Tony,
I ran exactly the same code that you provided with a RadAsyncUpload control on an .aspx page in my VB Sitefinity project and it worked just fine - the file got uploaded in the selected library. The error that you are getting most probably indicates that there is something wrong with the library that you selected. Can you double-check if the name of the library is correct and if it is correct, try uploading in another library?
Kind regards,The name is correct because the following returns the correct library
Dim libTitle As String = "TESOL Resource Center"
Dim parentLibrary As DocumentLibrary = App.WorkWith().DocumentLibraries().Where(Function(dL) dL.Title.Equals(libTitle)).[Get]().FirstOrDefault()
I tried to create another library and use that new library and was presented with the same error. The only difference between what you used to test and my code is that my code is in a custom user control that has been placed on a Sitefinity content page. But That should not effect the way the code works.
My project is approaching deadline and this is the only portion that does not work as intended.
Could you send me the code pages that you used?
Thanks,
TC
I ran the code below in an empty Sitefinity site and still got an error. Sitefinity ver 4.2.1733.0
I have included a screen shot of the error as well.
Private Sub UploadFiles2()
Dim libraryManager As Modules.Libraries.LibrariesManager = New Modules.Libraries.LibrariesManager()
libraryManager = LibrariesManager.GetManager()
libraryManager.Provider.SuppressSecurityChecks = False
Dim libTitle As String = "TEST"
Dim parentLibrary As DocumentLibrary = App.WorkWith().DocumentLibraries().Where(Function(dL) dL.Title.Equals(libTitle)).[Get]().FirstOrDefault()
If RadAsyncUpload1.UploadedFiles.Count > 0 Then
For Each file As UploadedFile In RadAsyncUpload1.UploadedFiles
' Upload file
App.WorkWith().DocumentLibrary(parentLibrary.Id).CreateDocument().Do(Sub(d)
d.Title = file.FileName
End Sub).CheckOut().UploadContent(file.InputStream, file.GetExtension()).CheckInAndPublish().SaveChanges()
Next
End If
End Sub
Hi Tony,
Ok, I tried the same code in 4.2 1733 and indeed the error is thrown. I guess this is a bug that got fixed in our latest release, because trying it in 4.3 1885 causes no problems, as I've said in my previous answer. Is there a chance that you could upgrade?
Regards,We upgraded the site and ran the code again. The following line is resulting in an error. I have attached a screen shot. This line worked before the upgrade occurred.
Dim parentLibrary As DocumentLibrary = App.WorkWith().DocumentLibraries().Where(Function(dL) dL.Title.Equals(libTitle)).Get().FirstOrDefault()
Hello Tony,
Probably something broke during the upgrade. Can you again try the code on a blank 4.3.1885 project to make sure that it works (it works here)? If it doesn't work - please send me the exact code you are running, so I can reproduce it. If it works on a blank project - then the problem is indeed in the upgrade. What I can suggest is try deleting the library or create a new one and try uploading the files there. Also, please double-check the dlls in your bin folder. Probably some of the references still point to previous versions and this may be causing such an issue. Another thing - can you open the same library from the backend or the issue appears just when you try selecting it by code?
Best wishes,Svetoslav,
We had to roll back the upgrade anyway because there was another issue on the site. So we are back on version 4.2.1650.0. We really need to be able to upload and insert files into a document library. Can you please supply an example in VB that DOES NOT use the fluent API? We need a reliable way to progammatically add files to a library and publish them.
Thanks,
Tony
Hello Tony,
Here's an example with LibrariesManager:
Dim
libraryManager
As
Modules.Libraries.LibrariesManager =
New
Modules.Libraries.LibrariesManager()
libraryManager = LibrariesManager.GetManager()
libraryManager.Provider.SuppressSecurityChecks =
False
Dim
parent
As
DocumentLibrary = libraryManager.GetDocumentLibraries().Where(
Function
(dL) dL.Title.Equals(
"Test"
)).FirstOrDefault()
If
RadAsyncUpload1.UploadedFiles.Count > 0
Then
For
Each
file
As
UploadedFile
In
RadAsyncUpload1.UploadedFiles
' Upload file
Dim
document1
As
Telerik.Sitefinity.Libraries.Model.Document = libraryManager.CreateDocument()
document1.Parent = parent
document1.Title = file.FileName
document1.UrlName = file.FileName
document1.Description = file.FileName
document1.ApprovalWorkflowState =
"Published"
libraryManager.RecompileAndValidateUrls(document1)
libraryManager.Upload(document1, file.InputStream, file.GetExtension())
libraryManager.SaveChanges()
' Get files GUID in the library and store it in the DB
Next
End
If
Svetoslav,
The code you sent worked on an empty project but on the site I need it operational on there was the same error. I tried the code on version 4.2.1650.0 as well as 4.2.1733.0 both resulted in the same error.
How can we get this resolved?
Thanks,
Tony
Hi Tony,
You say that this works on an empty project, then I suppose there is a problem with your libraries. Can you recall any customizations you made that may cause this? Could you try deleting all of the libraries and then again try uploading the file in an empty library? I cannot think of anything that could cause such a problem, so the only solution here would be for you to open a support ticket and send me your project.
Greetings,