Creating albums: native vs fluent API
Hi all,
I am new to Sitefinity (this is actually my first post) and I have been struggling a bit with creating an album/library via the API. When I try to create an album via the native API as described in the documentation, the album is visible in the back-office but opening the album does not work as expected: the contents of a different album are shown, and the web URL does not contain the album URL name. Below is a sample of the code I'm using (pretty much the same as provided in the docs)
public
Album CreateAlbumIfNotExists(
string
albumTitle)
Album album = _libManager.GetAlbums()
.FirstOrDefault(a => a.Title == albumTitle);
if
(album ==
null
)
album = _libManager.CreateAlbum();
album.Title = albumTitle;
album.DateCreated = DateTime.UtcNow;
album.LastModified = DateTime.UtcNow;
album.UrlName = Regex.Replace(albumTitle.ToLower(), _friendlyUrlRegex,
"-"
);
_libManager.SaveChanges();
return
album;
I would suggest if you can debug the code using Visual Studio to see when it is creating the Album if the UrlName property is being set properly. Check also you are intending to use Album.Title or Album.Name and the case of albumTitle string matches the Title or Name being returned.
Hi Jonathan,
Thanks for your reply.
While debugging I can see the UrlName being set properly, and in a second call the album is retrieved correctly by its title, and the UrlName property is still correct.
Still not sure what I'm missing. I might have to resort to the fluent API but for this I will need to update all of the interfaces using GUID's instead of Album/Image/IFolder objects.
Telerik support helped me with this issue, and it seems the documentation is missing the following statement, which is to be added right before the call to SaveChanges():
_libManager.RecompileItemUrls(album);
Hello,
I am glad to hear that the problem has been resolved using the code we have sent you. Thank you for sharing it with the community.
Regards,
Stefani Tacheva
Telerik