How to add Count() method on my content type in orchard? - orchardcms

I have a content type named "News" with Title, Body, Autoroute and a TextField. I have 2 problems:
To Count value of content items by text field named Author (can't use taxonomy terms).
To Count total sum of content items by Date (from 2017-01-01 to 2017-12-31)
Now, I want to add a Count() method on my content type, so I can filter, sort and use Projection.
How can I do this?
Or do you have a better method for it?
Thanks!

I'd use Orchards ISearchService for this. Just add your ContentType to a search index (you can have multiple indices) and check the fields you want to include, like created and author.
Then you can search like this and use the TotalItemCount property of the ISearchService:
var pager = new Pager(this.OrchardServices.WorkContext.CurrentSite, page, pageSize);
var searchSettingsPart = this.OrchardServices.WorkContext.CurrentSite.As<SearchSettingsPart>();
// Orchard.Search.Services.ISearchService
var searchHits = this.searchService.Search.Query(
searchText,
pager.Page,
pager.PageSize,
searchSettingsPart.FilterCulture,
searchSettingsPart.SearchIndex,
searchSettingsPart.GetSearchFields(searchSettingsPart.SearchIndex),
searchHit => searchHit);
var count = searchHits.TotalItemCount;
As far as the description of your ContentType goes, I think Orchard already provides all the necessary filters for a projection.
If you need something special you'd need to implement your own FilterProvider. The ContentTypesFilter is a good example on how to do this.

Related

How to get source list types of particular list/record field?

Here is I have two entity custom fields with list/record type,
custom_dev_j15 entity field has a custom source list (eg: one, two, three, four, etc)
custom_qa_v93 entity field has a standard source list as an object (eg: customer )
I've two vendor entity custom fields as stated in screenshots of question,
custentity473 --> customer is selected as list source
custentity474 --> custom_dev_j15_m_list as selected as list source ( which is custom list)
Here is snippet that i used to get the options of these fields,
// Snippet
var fieldDetails = {};
var record = nlapiCreateRecord("Vendor");
var field = record.getField("custentity473");
var selectoptions = field.getSelectOptions();
for ( var i in selectOptions) {
var Option = {
id : selectOptions[i].getId(),
label : selectOptions[i].getText()
}
Options.push(Option);
}
fieldDetail["options"] = Options;
But my need is to get source list information like name of the list source (customer or custom_dev_j15_m_list) via suitescript
any idea on how to get this information?
Thanks in advance
I'm not sure I understand this question for what you're trying to do.
In NetSuite almost always, you accommodate to the source list types because you know that's the type, and if you need something else (e.g. a selection which is a combination/or custom selection you'll use a scripted field)
Perhaps you can expand on your use case, and then we can help you further?

Orchard CMS update record directly in database

I have got list of content items , for each content items I need to add a meta keyword and descriptions. I am using VandelayIndustries module for this. I have got list of keywords and descriptions along with the contentItemId .I can find out the published ContentItem from ContentItemVersionRecord.
Is there a way I can directly insert record in Vandelay_Industries_MetaRecord table with Id as ContentItemId and keywords and Descriptions.
You will need to create an import method. Your ContentItems should have a MetaPart. You can achieve this either by going to Content Definition in the Orchard dashboard or by modifing the ContentType in the migrations.
ContentDefinitionManager.AlterTypeDefinition("MyContentType",
type => type
.WithPart("MetaPart"));
Create a method that iterates thru the csv file and will add the entries:
foreach(var entry in listFromFile) {
var item = _contentManager.Get(entry.Id).As<MediaPart>();
item.Key = entry.Key;
item.Description = entry.Description;
}
And that's it.

How to iterate over list of terms within a taxonomy

Provided a taxonomy name or ID, how do I iterate over a list of terms within the said taxonomy?
E.g. I want to do something like this:
var taxonomyId = 15;
foreach(dynamic item in get_terms(taxonomyId)) {
#Display(item.text)
}
You need to inject ITaxonomyService in your controller/driver, and ITaxonomyService has a method which returns the list of terms for a taxonomyId.
IEnumerable<TermPart> GetTerms(int taxonomyId);
you can pass these terms to the view using model/viewmodel.
you can look into ITaxonomyService implementation in taxonomy module for further details.

Liferay searchContext search by AssetTags and Keywords

I'm trying to implement custom search portlet through AssetEntries. Currently AssetEntryQuery doesn't allow to search with keywords. I'm trying to search through FacetedSearcher. Search by keywords seems to be ok. But when I'm trying to search by AssetTagNames
searchContext.setAssetTagNames(assetTagNames)
it doesn't work at all.
Here's my piece of code
SearchContext searchContext = new SearchContext();
Facet assetEntriesFacet = new AssetEntriesFacet(searchContext);
assetEntriesFacet.setStatic(true);
searchContext.addFacet(assetEntriesFacet);
/*MultiValueFacet multiValueFacet=new MultiValueFacet(searchContext);
multiValueFacet.setFieldName("assetTagNames");
multiValueFacet.setStatic(false);
searchContext.addFacet(multiValueFacet);*/
searchContext.setCompanyId(themeDisplay.getCompanyId());
String []assetTagNames=new String[1];
assetTagNames[0]= assetTagName;
searchContext.setAssetTagNames(assetTagNames);
searchContext.setKeywords(keywords);
String[] entryClassName = {JournalArticle.class.getName()};
searchContext.setEntryClassNames(entryClassName);
Indexer indexer = FacetedSearcher.getInstance();
// searchContext.setAndSearch(true);
Hits hits = indexer.search(searchContext);
System.out.println("Hits: " + hits.getLength());
Resulted query for request
searchKeyword: key1key1
assetTagName: sometag
+(+(companyId:1) +((+(entryClassName:com.liferay.portlet.journal.model.JournalArticle) +(status:0)))) +(assetCategoryTitles:*key1key1* assetCategoryTitles_en_US:*key1key1* assetTagNames:*key1key1* comments:key1key1 content:key1key1 description:key1key1 properties:key1key1 title:key1key1 url:key1key1 userName:*key1key1* classPK:key1key1 content_en_US:key1key1 description_en_US:key1key1 entryClassPK:key1key1 title_en_US:key1key1 type:key1key1)
As you see AssetTag isn't applied to the query.
I've already tried to set it through
searchContext.setAttribute("assetTagNames",assetTagName);
and commented MultiValueFacet code but wih no result.
For further i need to search by dateRange and Categories. Has anybody any idea?
Fortunately solved this.
If you'd like to search through tags you've to use a separate facet for this, e.g.
MultiValueFacet assetTagsFacet = new MultiValueFacet(searchContext);
assetTagsFacet.setFieldName(Field.ASSET_TAG_NAMES);
searchContext.addFacet(assetTagsFacet);
Also use searchContext.setAttribute("assetTagNames", assetTagName); instead of searchContext.setAssetTagNames(assetTagName);
For searching through Categories the same thing:
MultiValueFacet assetCategoriesFacet = new MultiValueFacet(searchContext);
assetCategoriesFacet.setFieldName("assetCategoryTitles");
searchContext.addFacet(assetCategoriesFacet);
searchContext.setAttribute("assetCategoryTitles", assetCategoryName);
Also i wanted to search by custom type of JournalArticle, Ive created facet for this, but got "type" twice in query. As a solution i used MultiValueFacet instead of AssetEntriesFacet during setting entryClassName
MultiValueFacet assetEntriesFacet = new MultiValueFacet(searchContext);
assetEntriesFacet.setFieldName("entryClassName");
searchContext.setAttribute("entryClassName",JournalArticle.class.getName());
searchContext.addFacet(assetEntriesFacet);

Search other items based on Taxonomy term

I created a few terms using Taxonomy Module. I added a Taxonomy field in my custom content type.
Now, on the page, I want to display other items that have been tagged with the same Taxonomy term, how do I do that?
If you know the term name, you can use the following query -
var _taxonomyService = WorkContext.Resolve<ITaxonomyService>();
var termContentItems = _taxonomyService.GetContentItems(termname)
.Where(c => c.ContentItem.ContentType == "YourContentTypeName")
.ToList();

Resources