Search other items based on Taxonomy term - orchardcms

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();

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?

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

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.

Orchard - how to access taxonomy field of a custom content type programmatically

I have a custom content type called Store, which has a Brands taxonomy field. A Store can have multiple Brands associated with it.
I have been tasked with building an import/export routine that allows the user to upload a CSV file containing new Stores and their associated Brands.
I can create the Stores other fields OK, but can't work out how to set the taxonomy field?
Can anyone tell me how I access the Taxonomy field for my custom content type?
Thanks in advance.
OK so (as Bertrand suggested), using the Import/Export feature might be a better way to go, but as a relative noob on Orchard I don't have the time to spend looking at it and couldn't find a good tutorial.
Below is an alternative approach, using the TaxonomyService to programatically assign Terms to a ContentItem.
First of all, inject the ContentManager and TaxonomyService into the constructor...
private ITaxonomyService _taxonomyService;
private IContentManager _contentManager;
public MyAdminController(IContentManager contentManager, ITaxonomyService taxonomyService)
{
_contentManager = contentManager;
_taxonomyService = taxonomyService;
}
Create your ContentItem & set the title
var item = _contentManager.New("MyContentType");
item.As<TitlePart>().Title = "My New Item";
_contentManager.Create(item);
Now we have a ContentItem to work with. Time to get your taxonomy & find your term(s)...
var taxonomy = _taxonomyService.GetTaxonomyByName("Taxonomy Name");
var termPart = _taxonomyService.GetTermByName(taxonomy.Id, "Term Name");
Add the terms to a List of type TermPart...
List<TermPart> terms = new List<TermPart>();
terms.Add(termPart);
Finally, call UpdateTerms, passing in the ContentItem, terms to assign and the name of the field on the ContentItem you want to update...
_taxonomyService.UpdateTerms(item, terms.AsEnumerable<TermPart>(), "My Field");
Hope this helps someone. Probably me next time round! : )

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.

Resources