3

I would like to make a new Structure Group when we create a new Folder. In older versions of Tridion we might do this with the TDSE object.

I'm on Tridion Sites 9 and wanted to use something similar and tried with the following code.
StructureGroup newSg = (StructureGroup) session.GetNewObject();

Error CS0311 The type 'Tridion.ContentManager.CommunicationManagement.StructureGroup' cannot be used as type parameter 'T' in the generic type or method 'Session.GetNewObject()'. There is no implicit reference conversion from 'Tridion.ContentManager.CommunicationManagement.StructureGroup' to 'Tridion.ContentManager.SystemWideObject'.

Is my only option here to use the Tridion Core Service?

7

Use the StructureGroup constructor to get a new SG, something like this:

StructureGroup newSg = new StructureGroup(session, parentSGTcmId);

Here is a snippet from the API docs:

enter image description here

  • 1
    Thanks Nick! I also found another way to solve it as well :) – robrtc Apr 22 at 18:24
4
     [TcmExtension("NewStructureGroupForImageFolder")]
        public class NewStructureGroupForImageFolder : TcmExtension
        {
            public NewStructureGroupForImageFolder()
            {
                Subscribe();
            }

            private void Subscribe()
            {
                EventSystem.Subscribe<Folder, SaveEventArgs>(HandleFolderSave, EventPhases.TransactionCommitted);
            }

            private void HandleFolderSave(Folder folder, SaveEventArgs args, EventPhases phase)
            {
                // create new Structure Group
                Publication pub = folder.ContextRepository as Publication;
                StructureGroup rootSg = pub.RootStructureGroup;
                StructureGroup newSg = rootSg.GetNewObject<StructureGroup>();
                newSg.Title = "a new structure group";
                newSg.Directory = "newsg";
                newSg.Save();
            }
      }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.