jxycms
3/31/2017 - 5:33 AM

Macro file sample

Macro file sample

using System;
using System.Collections.Generic;
using System.Web;

using CMS;
using CMS.Base;
using CMS.MacroEngine;
using CMS.Helpers;
using CMS.Membership;
using CMS.WebAnalytics;
using CMS.MediaLibrary;
using CMS.DataEngine;
using CMS.SiteProvider;
using CMS.DocumentEngine;
using CMS.EventLog;
using CMS.PortalEngine.Web.UI;

[assembly: RegisterExtension(typeof(HDY.Macros), typeof(Int32))]
[assembly: RegisterExtension(typeof(HDY.Macros), typeof(string))]

namespace HDY
{
    public class Macros : MacroMethodContainer
    {

        [MacroMethod(typeof(string), "Get Related Page DocumentName", 0)]
        [MacroMethodParam(0, "Node ID", typeof(Int32), "Node ID")]
        [MacroMethodParam(1, "Field Name", typeof(string), "Field Name")]
        [MacroMethodParam(2, "Class Name", typeof(string), "Class Name")]
        [MacroMethodParam(3, "RelationshipName", typeof(string), "RelationshipName")]
        public static object GetRelatedPageValue(EvaluationContext context, params object[] parameters)
        {
            try
            {
                string returnValue = "";

                int nodeId = ValidationHelper.GetInteger(parameters[0], 0); // this page's node id
                string fieldName = ValidationHelper.GetValue(parameters[1], "");// related page's field name
                string className = ValidationHelper.GetValue(parameters[2], "");// related page's page type name
                string relationshipName = ValidationHelper.GetValue(parameters[3], "");// relationship name

                TreeProvider treeProvider = new TreeProvider(MembershipContext.AuthenticatedUser);
                TreeNode treeNode = treeProvider.SelectSingleNode(nodeId);

                if (treeNode != null)
                {
                    TreeNode relatedTreeNode = DocumentHelper
                        .GetDocuments(className)
                        .InRelationWith(treeNode.NodeGUID, relationshipName)
                        .FirstObject;
                    
                    if (relatedTreeNode != null)
                    {
                        returnValue = relatedTreeNode.GetValue(fieldName, "");
                    }
                }

                return returnValue;
            }

            catch (Exception ex)
            {
                EventLogProvider.LogException("Macros", "GetRelatedPageDocumentName", ex);
                return "";
            }
        }

        [MacroMethod(typeof(string), "Get Insight Default Image", 0)]
        [MacroMethodParam(0, "Node ID", typeof(Int32), "Node ID")]
        public static object getInsightDefaultImage(EvaluationContext context, params object[] parameters)
        {
            int nodeID = ValidationHelper.GetInteger(parameters[1], 0);
            TreeProvider treeProvider = new TreeProvider(MembershipContext.AuthenticatedUser);
            TreeNode node = treeProvider.SelectSingleNode(nodeID);
            var insightDefaultImage = node.GetStringValue("Image", "");
            return insightDefaultImage;
        }

    }
}