Feels kinda awkward getting something by the name the editors give in the UI, but you could solve this by doing:
var rootCampaign = _contentLoader.Service.Get<IContent>(SalesCampaignFolder.CampaignRoot); var campaigns = _contentLoader.GetChildren<SalesCampaign>(rootCampaign); SalesCampaign campaign; foreach (var c in campaigns) { if (c.Name == "FlashCampaign") { campaign = c; break; } }
or somthing like that
Thank you Joel, I will try it now!
Is there another way you prefer to find one particular campaign, other than by name?
Yeah, you could have a property on some global page somewhere, we use to have a section of the StartPage properties called "SiteSettings", where we set some global data. So you can add a SalesCampaign property to that site settings, then set your campaign in the admin UI, the one you want to fetch later.
And fetch it by doing
var startPage = _contentLoader.Get<StartPage>(ContentReference.StartPage); var campaign = _contentLoader.Get<SalesCampaign>(startPage.thePropertyYouAddedThatRefersToACampaign);
You might need to do a UIHint to the property in order to actually get the campaign list when you're setting the property on the StartPage, can't recall in my head how that was done, but it was googlable when I did it :)
While the code by Joel is correct in idea, it should be like this - GetChildren takes a ContentReference as a parameter and we don't have to load the CampaignRoot
var campaigns = _contentLoader.GetChildren<SalesCampaign>(SalesCampaignFolder.CampaignRoot); SalesCampaign campaign; foreach (var c in campaigns) { if (c.Name == "FlashCampaign") { campaign = c; break; } }
It depends on the way you define the campaign to load, but usually if you link a campaign to a CMS page, you will get a contentreference for that, then you can load the campaign by that contentreference.
Hi,
I'm trying to do this:
My desired campaign is named "FlashCampaign"...
How can I get the campaignLink programmatically?