A client wanted to have access rights on the campaigns in Episerver Commerce, so marketing managers for one market could not make changes to the campaigns for other markets. As this is not possible out of the box, I tried to find another way.
I remembered a post (https://www.david-tec.com/2017/04/restricting-access-to-episerver-commerce-markets-for-specific-users/) by David Knipe about restricting access to markets for specific users. So I changed and extended his code a bit to filter the visible campaigns based on the market a user has access to.
NOTE: David used the market name for the usergroup, I needed to use the marketId.
I added another helper method to the MarketAccessRightsHelper, to use the marketId for checking the user role which was needed as a campaign uses the marketId for checking the market it belongs to.
The overview of the campaigns uses a content query, GetSalesCampaignChildrenQuery, so I decided to make a custom implementation for it. The thing I needed to override was GetChildrenByType, to which I applied a filter, checking on if the user is in a group that has access to the market.
NOTE: It is a bit of a hack, as the facet count is not filtered. So if there are multiple campaigns, the count will be off.
protected override IEnumerable GetChildrenByType(
if (type == typeof(SalesCampaign) && this.marketAccessRightsHelper.IsFilteredMarketsRequest())
return base.GetChildrenByType(type, parentLink, selector).OfType().Where(
c => this.marketAccessRightsHelper.UserHasAccessToMarket(c.TargetMarket));
return base.GetChildrenByType(type, parentLink, selector);
I overrode the default implementation in the initializable module like this
This filters the campaigns in the overview based on the “access rights”. It also filters the the list of markets in the left menu.
The complete code can be found in a gist (https://gist.github.com/jstemerdink/b94c104b0aabb04bd3d9b1ab87f8b636)which is a fork of David’s gist.
Original article posted on EpiServer Development Blog: Filter your commerce campaigns for access