Search code examples
orchardcmsorchardcms-1.8

How can I set an item in Content Picker outside of AdminUI?


I have ContentTypeA with a ContentPickerA field. I also have ContentTypeB with other fields, etc. These are created in a Migrations.cs file, which currently is the only code (other than the Workflow Activity below) in my module. So, no parts, drivers, etc.

I am in the process of creating a Worfklow that, after the user registers, will create ContentItem1 off of ContentTypeA, and ContentItem2 off of ContentTypeB.

I'd like to automatically link the content items, such that I am filling in ContentPickerA with ContentItem2. The point being to avoid having to go into AdminUI to manually link these items.

I've searched for quite a bit and have not been able to find any concrete examples, let alone anything that puts me on the right track.

Any help is much appreciated.


Solution

  • In Execute of an Activities class I was able to get this working with the following code:

    public override IEnumerable<LocalizedString> Execute(WorkflowContext workflowContext, ActivityContext activityContext)
        {
            LocalizedString outcome = null;
    
            try
            {
                //VARIABLES: DATA
                var mainTitle = "Test Content Title From Workflow Activity";
                var matterTitle = "Matter Type Title From Workflow Activity";
    
                //VARIABLES: NEW CONTENT ITEMS 
                var spacesMain = _contentManager.New("TestContentType");
                var spacesMatter = _contentManager.New("MatterType");
    
                //BIND DATA
                spacesMain.As<TitlePart>().Title = mainTitle;
                spacesMatter.As<TitlePart>().Title = matterTitle;
    
                //CREATE DRAFT CONTENT ITEMS
                _contentManager.Create(spacesMain, VersionOptions.Published);
                _contentManager.Create(spacesMatter, VersionOptions.Published);
    
                //SET CONTENT PICKER
                var contentPickerTest1 = ((dynamic)spacesMain).TestContentType.ContentPickerTest1;
                contentPickerTest1.Ids = new[] { spacesMatter.Id };
    
                outcome = T("Done");
            }
            catch (Exception exc)
            {
                workflowContext.SetState("Exception", exc);
    
                outcome = T("Error");
            }
    
            yield return outcome;
    
        }
    

    Hope this helps someone, because as a non-programmer I was pretty glad to have finally seen the Content Item in the AdminUI have the ContentPickerField set to the other Content Item I created.