How to: set access right to folders

Today I stumped upon this question Solution for Handling File Upload Permissions in Episerver CMS 12, and there is a simple solution for that

Using the  EditSecurity.aspx comes in handy but it is not very future proof as it is now removed in CMS 12 with its siblings WebForms part. However, we can easily make up for that by using the APIs – which is meant to remain for a long time. Even better, this could be set up to be done automatedly and repeatedly. The little secret is IContentSecurityRepository which is what EditSecurity.aspx used under the hood.

To set the access right of a content, this is what you need

        var contentlink = new ContentReference(100);
        var content = _contentRepository.Get<IContent>(contentlink);
        if (content is IContentSecurable securable)
        {
            var descriptor = securable.GetContentSecurityDescriptor();
            //You might need to add this if your content is not Catalog
            descriptor = (ContentAccessControlList)descriptor.CreateWriteableClone();
            descriptor.Clear();
            descriptor.AddEntry(new AccessControlEntry("Everyone", AccessLevel.Read, SecurityEntityType.Role));
            descriptor.AddEntry(new AccessControlEntry("Author", AccessLevel.Read | AccessLevel.Create | AccessLevel.Edit | AccessLevel.Delete | AccessLevel.Publish, SecurityEntityType.Role));
//any other access rights that you need to set
            _contentSecurityRepository.Save(contentlink, descriptor, SecuritySaveType.Replace);
        }

The first two lines are from for demonstration, and not very “automated”. You might have some hard coded value there or might have to work your magic to find the right content. (Just a kind note, if you want your code to work consistently between sites, make sure to use ContentGuid instead ContentId which is changed depending on the database).

The rest of the code is quite self-explanatory. You check if your content implements IContentSecurable which is required to have access rights. Then you clear any existing access rights and add your desirable ones. Finally save it with the SecuritySaveType.Replace to make sure that only access rights you wanted, exist.

This code can be run multiple times (Idempotent). You can add it to a scheduled job, or even better, a startup routine, to make sure that you always have the right access rights of specific content.

Be careful with your (order) notes

This happened a quite ago but only now I have had time to write about it.

Once upon a time, I was asked to look into a customer database (in a big engagement of helping them improving performance overall)

One thing stand out is this query

WITH CTE AS
(SELECT * FROM dbo.OrderGroupNote 
WHERE OrderGroupId = @OrderGroupId)
MERGE CTE AS T
USING @OrderGroupNotes AS S
ON T.OrderNoteId = S.OrderNoteId
WHEN NOT MATCHED BY TARGET
	THEN INSERT (
		[OrderGroupId],
		[CustomerId],
		[Title],
		[Type],
		[Detail],
		[Created],
		[LineItemId],
		[Channel],
		[EventType])
	VALUES(S.OrderGroupId,
		S.CustomerId,
		S.Title,
		S.Type,
		S.Detail,
		S.Created,
		S.LineItemId,
		S.Channel,
		S.EventType)
WHEN NOT MATCHED BY SOURCE
	THEN DELETE
WHEN MATCHED AND (S.IsModified = 1) THEN 
UPDATE SET
	[OrderGroupId] = S.OrderGroupId,
	[CustomerId] = S.CustomerId,
	[Title] = S.Title,
	[Type] = S.Type,
	[Detail] = S.Detail,
	[Created] = S.Created,
	[LineItemId] = S.LineItemId,
	[Channel] = S.Channel,
	[EventType] = S.EventType;

If you can guess, that is the query to save the notes of an order. Normally it’s … fine. But for this customer, it is not, each save could result in almost 10GB, yes, you read it right, ten gigabytes of logical reads. Insane

The reason was, this customer has some orders with an absurd number of notes attached to it. The most one has 52k notes. And there are, in total, 94 orders with more than 1000 notes.

Upon investigation, they have a job to validate payment of invalid orders, which runs every 10 minutes. If the validation failed, a note will be added to the order. But because of no “limit” or “cut off”, that’s kept going for forever and continuing to add notes to orders. Each time, the operation becomes more expensive.

As a side note, this is note only expensive on the saving (to the database). It’s expensive to load from database, and it’s expensive to create all the objects in the memory.

The fix in this case is obviously to trim old notes, and to make sure that if the validation failed for X times, stop processing further.

But you might ask, could we do better. could we not save the entire order notes collection just because one note is added? That’s a good question. A really good one. I took a shot at that, but it’s … complicated. This is where we are held back by our promises of keeping thing backward compatible. When we try to make it better – you can do it better yourself as well. Make sure you do not have orders with an unusually high amount of notes.

SELECT 
    ordergroupid, 
    COUNT(OrderNoteId) AS notecount
FROM 
    OrderGroupNote
GROUP BY 
    ordergroupid
ORDER BY 
    notecount DESC;

If the most you have is less than 10, all good, less than 20 is fine. More than that and you might want to check why those orders have that many notes.

How to: create Decimal metafield with custom precision

If you are using catalog system, the way of creating metafields are easy – in fact, you can forget about “metafields”, all you should be using is the typed content type. Adding this attribute to your property is enough to set the precision as you wish.

        [DecimalSettings(10, 2)]
        public virtual Decimal MyAttributesDecimal { get; set; }

Thing is a little different if you are using order system. You don’t have the strongly typed order types to work with. To automate the metafield creation, you will have to use the underlying MetaField API yourself. You probably know how to create a metafield and add it to a desirable metaclass

            var metaField = MetaField.Create(MetaDataContext.Instance, "Mediachase.Commerce.Orders.System.Shipment", "NewMetaField3", "new", "temp",
                MetaDataType.Decimal, 17, true, true, false, false);
            var metaClass = MetaClass.Load(MetaDataContext.Instance, "ShipmentEx");
            metaClass.AddField(metaField);

However, metafield created this way will be added to the metaclass with the default (18, 0) precision, which is kind of pointless. How to control the precision of the decimal metafields?

The little secret is with the MetaField.Attributes. There are two attributes that control the precision of decimal type: MdpPrecision, and MdpScale. You have to set those after the metafield is created, but before it’s added to the metaclass (the reason was simple: the underlying query to add the column to the table looks for those values to set the precision of the column). Your code should look like this

var metaField = MetaField.Create(MetaDataContext.Instance, "Mediachase.Commerce.Orders.System.Shipment", "NewMetaField5", "new", "temp",
    MetaDataType.Decimal, 17, true, true, false, false);
metaField.Attributes["MdpPrecision"] = "38";
metaField.Attributes["MdpScale"] = "9";
var metaClass = MetaClass.Load(MetaDataContext.Instance, "ShipmentEx");
metaClass.AddField(metaField);

A small but important note to remember: both of the attributes must be set, even if you want to leave one to default value.

And Tada!

Best deals on Amazon.se week 3/2025

Anker MagGo Power Bank, Qi2 Certified 15W MagSafe Compatible Portable Charger, 10.000mAh Battery Pack 

Reduced to kr799.00

https://amzn.to/3Wj6jlb

Soundcore by Anker Q20i Hybrid Active Noise Cancelling Headphones Wireless Over-Ear

Reduced to kr399.00

https://amzn.to/4fYN7QX

Regina Chamomile Paper 3-Ply Toilet Paper | 56 Roll Packs 

Reduced to kr250.63

https://amzn.to/3WnvtPP

OXO Good Grip 3-Piece Kitchen Tools & Utensil Set, Swivel Peeler, Can Opener, Whisk [Amazon Exclusive] : Amazon.se: Home & Kitchen

Reduced to kr131

Best deals on Amazon.se week 1/2025

Fjällräven Greenland Winter Jacket, size S

Normally 3999kr, reduced to kr2,363.89

https://amzn.to/4a0wwux

Samsung Galaxy Buds3 Pro

Reduced to kr1,727.08

https://amzn.to/3W6En3P

Kenwood Titanium Chef Baker Silver KVC85.004SI, Kitchen Assistant Food Processor, Integrated Scale, 3 Hook and Beaters for Mixing Stainless Steel and Rubber Beater, Bowls of 5L and 3.5L, 1200W, Silver

Reduced to kr4,058.00

https://amzn.to/3W2qBPV

Fluke Fluke 117 Multimeter, 600 V, Black, Yellow


Reduced to kr3,097.00

https://amzn.to/3ZVMC40

8Bitdo Ultimate C Bluetooth Controller for Switch with 6-axis Motion Control and Rumble Vibration (Pink)

Reduced to kr249.00, and a further 15% if buy 10 qualifying items with Prime membership

https://amzn.to/3BRKi69

Anker Prime Charger, 200W 6-Port GaN Charging Station

Reduced to kr749.00

https://amzn.to/3BMDg2B

Philips Airfryer Combi 7000 Series XXL – 8.3L (2kg), 22-in-1 airfryer (HD9876/90)

Reduced to kr2,499.00

https://amzn.to/4achn9t

Anker 335 67W Charger

Reduced to  kr299.00

https://amzn.to/3BXg3e4

Best deals on Amazon.se week 52/2024

AUDIO TECHNICA AudioT AT2035 Condenser Microphone

Reduced to  kr1,347.43

https://amzn.to/4fwYSxH

Fjällräven Women’s Expedition Parka No.1 W Jacket

Reduced to kr4,884.51

https://amzn.to/406jIzf

Philips Connected AC4236/10 Air Purifier

Reduced to kr2990

https://amzn.to/4fwFQaO

Levi’s herr Jeans 501 Original Fit

Reduced to kr699

https://amzn.to/3ZUD0q6

LG 32GS60QC-B – Ultragear monitor, 32″ QHD, 2560 x 1440, VA panel, 16:9, HDMI, 1 ms, 180Hz, FreeSync

Reduced to kr2,458.00

https://amzn.to/3ZROVoD

Fjallraven Expedition Long Down Parka W Jacket for Women

Reduced to kr4,546.31

https://amzn.to/3ZKjwUY

PC GAMING Aqua PC Gamer – NVIDIA RTX 4060 8G – Watercooling 240mm white – Aqua View XT Case – B760-WIFI – Corsair Vengeance SL 2X8GB DDR4 3600 WH – SSD 1TB PCI 4.0 – Windows 11 Pro

Reduced to kr8,635.64

https://amzn.to/4fAI5JW

Merrell Herr Moab 3 GTX vandringssko

Reduced to kr773.00

https://amzn.to/41Skx0e

PlayStation VR2

Reduced to kr4,697.07

https://amzn.to/3PdiX1b