• Decrease Text SizeIncrease Text Size

CpScripting

Posted Date: 5/3/2023
    Printer Friendly Version   Email A Friend   Add This   Increase Text Size   Decrease Text Size
CpScripting is a piece of code that will be executed in place of CpScript. It needs to follow a certain format. . key and id are required any custom properties are optional. The content of the code block can be placed into a new file My_CpScripting.cs


using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;

namespace Centralpoint.WebSite
{
 /// <remarks>
 /// This class is used to execute scripts.
 /// </remarks>
 public partial class CpScripting
 {
  /// <summary>
  /// This method executes custom scripts or override existing scripts.
  /// </summary>
  /// <param name="key">The lowered value of the key property of the script.
  /// <param name="collection">A collection of script properties and their values.
  /// <param name="result">The result returned from the script.
  /// <param name="found">Whether the script (key) was found and is returning a result.
  partial void ExecuteMy(string key, NameValueCollection collection, ref string result, ref bool found)
  {
   switch (key)
   {
    case "my_helloworldcpscript":
     result = My_HelloWorldCpScript(collection);
     break;
    default:
     return;
   }
   found = true;
  }

  /// <summary>
  /// This method executes a custom script.
  /// </summary>
  /// <param name="collection">A collection of script properties and their values.
  /// <returns>The result of the script.</returns>
  public string My_HelloWorldCpScript(NameValueCollection collection)
  {
   return "Hello World!";
  }

  #region forms
  #region pre-processing
  ///// <summary>
  ///// This method is used to modify form elements before the form is executed.
  ///// </summary>
  ///// <param name="key">The lowered value of the key property of the pre-processor.
  ///// <param name="fg">The form group that is being pre-processed.
  ///// <param name="isValid">Whether the current form is valid.
  ///// <param name="isSubmit">Whether the form is going to be processed (final submission) if isValid and this method returns true.
  ///// <param name="itemId">The id of the form item being processed.
  //partial void FormPreProcessorMy(string key, FormGroup fg, ref bool isValid, bool isSubmit, string itemId)
  //{
  //    switch (key)
  //    {
  //        case "my_formpreprocessor":
  //            isValid = this.My_FormPreProcessor(fg, isValid, isSubmit, itemId);
  //            break;
  //    }
  //    return;
  //}

  ///// <summary>
  ///// This method processes a form item before the form is executed.
  ///// </summary>
  ///// <param name="form">The form that is being pre-processed.
  ///// <param name="isValid">Whether the current form is valid.
  ///// <param name="isSubmit">Whether the form is going to be processed (final submission) if isValid and this method returns true.
  ///// <param name="itemId">The id of the form item being processed.
  ///// <returns>Whether the form is still valid.</returns>
  //public bool My_FormPreProcessor(FormGroup form, bool isValid, bool isSubmit, string itemId)
  //{
  //    Centralpoint.WebSite.FormItem fi = form.Get(itemId);
  //    FormItem item = FormItemInit(fi.Properties);
  //    //if (!this.Page.FormButton.Value.Equals(item.Id + "_Update")) return isValid;	// comment this in so to make this method only run when the related button is clicked, otherwise it will run with any form submission (paging, etc.)
  //    // update isValid as needed to validate the page, use isSubmit to only execute code when the form is entering its final submission
  //    return isValid;
  //}
  #endregion

  #region validation
  ///// <summary>
  ///// This method is used to validate form elements before the form is executed.
  ///// </summary>
  ///// <param name="key">The lowered value of the key property of the validator.
  ///// <param name="fm">The form manager used for validation.
  ///// <param name="fi">The form item that is being validated.
  ///// <param name="isValid">Whether the current form is valid.
  ///// <param name="found">Whether the validator (key) was found and is returning a result.
  //partial void FormValidatorMy(string key, FormManager fm, Centralpoint.WebSite.FormItem fi, ref bool isValid, ref bool found)
  //{
  //    switch (key)
  //    {
     // a case must also be added to primary CpScripting Execute method which sets result = this.FormValidator(collection); 
  //        case "my_formvalidator":
  //            isValid = this.My_FormValidator(fm, fi, isValid);
  //            break;
  //        default:
  //            return;
  //    }
  //    found = true;
  //    return;
  //}

  ///// <summary>
  ///// This method validates a form item.
  ///// </summary>
  ///// <param name="fm">The form manager used for validation.
  ///// <param name="fi">The form item that is being validated.
  ///// <param name="isValid">Whether the current form is valid.
  ///// <returns>Whether the form is still valid.</returns>
  //public bool My_FormValidator(FormManager fm, Centralpoint.WebSite.FormItem fi, bool isValid)
  //{
  //    // update the value of isValid accordingly using fi.Properties and fi.Value
  //    return isValid;
  //}
  #endregion
  #endregion
 }
}

As you can see from the example we have one custom method. My_HelloWorldCpScript In ExecuteMy, we tell the system to look for My_HelloWorldCpScript and execute that script which triggers the execution of the My_HelloWorldCpScript method. If you need to pass any custom parameters to the script they will be passed as a Collection of script properties. Example:


/// <summary>
/// This method executes a custom script.
/// </summary>
/// <param name="collection">A collection of script properties and their values.
/// <returns>The result of the script.</returns>
public string My_HelloWorldCpScript(NameValueCollection collection)
{
    string value1 = collection["CustomValue1"];
    string value2 = collection["CustomValue2"];
 return String.Format("Hello World! {0} {1} ", value1, value2);
}


The result will be: Hello World! Here is my first CpScript.
inside the My_HelloWorldCpScript method, you have full access to all Centralpoint objects.
By typing this.Page inside of the CpScript IntelliSense will give you everything that is available to you on the Centralpoint page.
Most common options you may need:


this.Page.AudienceInfo - This property gets the current audience information.
this.Page.SiteMapInfo - This property gets the current site map item information.
this.Page.User - his property is used to get or set the website user.
and some others below.


//This property gets the current document information.
   #region CurrentInfo
   if ((this.Page.CurrentInfo != null) && this.Page.CurrentInfo.Exists)
   {
       HttpContext.Current.Trace.Warn("CurrentInfo", String.Format("{0}", this.Page.CurrentInfo.Title));
       HttpContext.Current.Trace.Warn("CurrentInfo - to get value out of Attributes", String.Format("{0}", this.Page.CurrentInfo.Attributes.Get("Caption")));
   }
   #endregion

   // This property gets the current site map item information.
   #region SiteMapInfo
   if ((this.Page.SiteMapInfo != null) && this.Page.SiteMapInfo.Exists)
       HttpContext.Current.Trace.Warn("SiteMapInfo", String.Format("{0}", this.Page.SiteMapInfo.Attributes.Get("SystemName")));
   #endregion

   //This property is used to get or set the web site user.
   #region User
   if (this.Page.User.IsAuthenticated)//user login
       HttpContext.Current.Trace.Warn("User", String.Format("{0}", this.Page.User.Name));
   #endregion

   //This property gets the current audience information.
   #region AudienceInfo
   HttpContext.Current.Trace.Warn("AudienceInfo", String.Format("{0}", this.Page.AudienceInfo.SystemName));
   #endregion

   //This property gets the current design information.
   #region DesignInfo
   HttpContext.Current.Trace.Warn("DesignInfo", String.Format("{0}", this.Page.DesignInfo.Attributes.Get("PrinterIcon")));
   #endregion

   //Retrieve Properties of specific navigation Item.
   #region NavigationItemInfo Properties
   HttpContext.Current.Trace.Warn("NavigationItemInfo:Properties", String.Format("{0}", CpSystem.NavigationItemInfo("StayWellProducts").Properties.Get("LicenseName").Trim()));
   #endregion

   //contains website settings and properties
   #region Management object
   //SelectConnectionString
   HttpContext.Current.Trace.Warn("Management", String.Format("{0}", this.Page.Management.SelectConnectionString)); 
   //ExecuteConnectionString = this.Page.Management.ExecuteConnectionString
   #endregion

   //This property gets the current form if it's initialized on the page using 'fdi' query string parameter.
   #region FormInfo
   if ((this.Page.FormInfo != null) && this.Page.FormInfo.Exists)
       HttpContext.Current.Trace.Warn("FormInfo", String.Format("{0}", this.Page.FormInfo.Attributes.Get("Title"))); 
   #endregion







Related Taxonomy

Comments:

Be the first to leave a comment.
Please Login to post comments.