November Happy Hour will be moved to Thursday December 5th.

How to get Campaign by name?

Vote:
 

Hi,

I'm trying to do this:

var campaignLink = ??????

var campaignPromos = promoEngine.GetPromotionItemsForCampaign(campaignLink);

My desired campaign is named "FlashCampaign"...

How can I get the campaignLink programmatically?

#183574
Oct 17, 2017 21:01
Vote:
 

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

#183576
Oct 17, 2017 21:25
Vote:
 

Thank you Joel, I will try it now!

Is there another way you prefer to find one particular campaign, other than by name?

#183577
Oct 17, 2017 21:35
Vote:
 

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);
#183578
Oct 17, 2017 21:39
Vote:
 

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 :)

#183579
Oct 17, 2017 21:42
Vote:
 

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.

#183585
Oct 17, 2017 23:22
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.