问题
I can get images that way:
foreach (var pic in AsAdam(Content, "Images").Files)
1.What is the right way to sort images by name or upload date?
2.How to custom reorder images?
回答1:
The "folder" itself doesn't provide ordering. I have two suggestions
Either do something where the files have a leading number - which is not shown in the output. This is what I do on the app-catalog so each file has a "31 output editing" - with this I can pre-sort them in my local system before batch uploading them
Since each file can have metadata you could add a priority or sort field to the metadata and use that to sort.
you can also always sort the object by file-info (like date) - this uses the DNN FileInfo object which
回答2:
To sort on metadata field I used this code:
var filesAll = AsDynamic(AsAdam(Content, "Images").Files) as IEnumerable<dynamic>;
var filesWithMetadata = filesAll.Where(x=>x.HasMetadata).Where(x=>x.Metadata.Enabled).OrderBy(x=>x.Metadata.Sort);
var files = filesWithMetadata.Concat(filesAll.Where(x=>!x.HasMetadata));
}
@foreach(var pic in files){
<div style="clear: both">
<img src="@pic.Url?w=200&h=200&mode=crop" title="@pic.FileName" style="float: right">
<h3>@pic.Metadata.Title</h3>
Has Meta: @pic.HasMetadata
<div>Description: @Html.Raw(pic.Metadata.Description)</div>
</div>
}
回答3:
just an update for anyone having issues with this answer, for 2sxc v8.7 (not sure about other versions), I had to remove the .Where(x=>x.Metadata.Enabled) portion of the one line to get rid of it failing each time I added any metadata to any photo in the particular collection, not sure why that is the case, but it still seems to work fine without that piece.
Also, if its not clear to folks, the .OrderBy(x=>x.Metadata.Sort means its looking for a piece of metadata you define called "Sort", if you call the metadata you define something else (like SortOrder, defined as a number), then just change that statement to be the name of the piece of metadata you use (ex. .OrderBy(x=>x.Metadata.SortOrder ).
thanks for posting this question and solution, it's been a big help.
来源:https://stackoverflow.com/questions/37517683/sort-2sxc-adam-files