Adding a Content Reference

The following code example adds a content reference that's a target component to the Approve Expenses folder.

The folder hierarchy for this example is:

Root folder
   Expenses folder
      Create Expenses folder
      Review Expenses folder
      Approve Expenses folder

The following is the complete code sample: the steps explain each line.

Local ApiObject &MyPortal;
Local ApiObject &CRef, &CRefColl;
Local ApiObject &Fldr;

&MyPortal = %Session.GetPortalRegistry();

If NOT &MyPortal.Open(MYRECORD.PORTAL_NAME) Then
   /* Do error handling */
End-if;
&Fldr = &MyPortal.RootFolder.Folders.ItemByName("Expenses").Folders.ItemByName⇒
("Approve Expenses");

/* add content reference */

&CRef = &Fldr.ContentRefs.InsertItem(&CRefname, MYCP, MYRECORD.MYURL);
   &CRef.Description = &CRefname;

/* Set dates */
&CRef.ValidFrom = %Date;
&ToDate = AddToDate(%Date, 1, 1, 0);
&CRef.ValidTo = &ToDate;

/* Set content reference  types */
&CRef.UsageType = "TARG";   /* Target content reference */
&CRef.URLType = "UPGE";     /* Component type of content reference */
&CRef.StorageType = "RMTE";  /* Template is remote. */
&CRef.Template = "EXPENSES_TEMPLATE";      /* name of template */
&CRef.TemplateType = "HTML";   /* type of Template */
/* Set URL */
&CRef.URL = "ICType=Panel&Menu=MANAGE_EXPENSES&Market=GBL&Component=APPROVE_FINAL"

/* Save the content reference */

&CRef.Save();

To add a content reference:

  1. Get a Session object and a PortalRegistry.

    Before you can access a PortalRegistry object, you must get a session object. The session controls access to the registry, provides error tracing, enables you to set the runtime environment, and so on. Because you want to use the existing session, use the %Session system variable (instead of the GetSession function.) In addition, you want to get a PortalRegistry. Using the GetPortalRegistry method returns a reference to an unpopulated PortalRegistry object.

    &MyPortal = %Session.GetPortalRegistry();
  2. Open the PortalRegistry.

    After you get a PortalRegistry object, you want to open it, that is, populate it with data. Use the Open method to do this. The Open method returns a Boolean value, and this example uses that value to do error checking to make sure that the PortalRegistry is actually opened. In addition, the name of the PortalRegistry is kept in the record MYRECORD, in the field PORTAL_NAME.

    If NOT &MyPortal.Open(MYRECORD.PORTAL_NAME) Then
       /* Do error handling */
    End-if;
  3. Access the folder collection.

    One of the strengths of dot notation is being able to string together many methods and properties into a single line of code. The result of this single line of code is to return a reference to the folder collection within the folder.

    &Fldr = &MyPortal.RootFolder.Folders.ItemByName("Expenses").Folders.ItemByName⇒
    ("Approve Expenses");

    The Adding a Folder example has more explanation on the break down of this line of code.

  4. Add the content reference.

    You can only add content references from the collection of content references for the folder. After you have the content reference collection, use the InsertItem method to add the content reference. The ContentProvider for this content reference is named MYCP. The URL is stored in the record MYRECORD in the field MYURL.

    &CRef = &Fldr.ContentRefs.InsertItem(&CRefname, MYCP, MYRECORD.MYURL);
    &CRef.Description = &CRefname;

    Note that the InsertItem method adds the content reference to the database as soon as it's executed. You could check for errors at this point to see if the content reference was added correctly.

  5. Set the ValidFrom and ValidTo dates.

    For this example, the content reference should be accessible from the date it's created, so the example sets the ValidFrom property to be today's date.

    When you first create a content reference, the ValidTo date, that is, the date that this content reference "expires", is set to null, that is an empty string. This means it never expires. In this example, we set the ValidTo date to one year and one day after the current date.

    &CRef.ValidFrom = %Date;
    &ToDate = AddToDate(%Date, 1, 1, 0);
    &CRef.ValidTo = &ToDate;
  6. Set the type parameters.

    Many content reference properties work together to set the type of content reference.

    In this example, the content reference is a target. It's a component type of content reference, which means it's a PeopleSoft definition. The template to be used for displaying this content reference is stored remotely, its name is EXPENSES_TEMPLATE, and it's an HTML template.

    &CRef.UsageType = "TARG"; /* Target content reference */
    &CRef.URLType = "UPGE"; /* Component type of content reference */
    &CRef.StorageType = "RMTE"; /* Template is remote. */
    &CRef.Template = "EXPENSES_TEMPLATE"; /* name of template */
    &CRef.TemplateType = "HTML"; /* type of Template */
  7. Set the URL.

    The URL property of the content reference indicates where the content actually is. As this content reference is referencing a page, the URL has a specific format to indicate which page.

    &CRef.URL = "ICType=Panel&Menu=MANAGE_EXPENSES&Market=GBL&Component=APPROVE_FINAL"
  8. Save the content reference.

    After you have finished adding the content reference, you should save it.

       &CRef.Save();

    You may want to check for errors after you save each content reference.

See Adding a Folder.