Posts | Comments

Planet Arduino

Archive for the ‘Generic Lincoln University’ Category

Jul
30

New Generic EPiServer Page Type

C, EPiServer, Generic Lincoln University, HTML, qualifications Comments Off on New Generic EPiServer Page Type 


New Generic EPiServer Page Type

For you budding coders new or otherwise.

A simple piece of code which when its added to the load event of a EPiServer page can pull any variable from EPiServer or SharePoint using a HTML template with some tags embedded in it


EPiServer page templates in Visual Studio 2008



A bit of the HTML template. For example  the tags  labelled [sp:Title] with the value of a SharePoint variable called Title The same thing applies to EPI server values when a [es:TemplateFileName] tag is found its value is replaced with the EPiServer value for the page in question called TemplateFileName 

Here is the EPiServer page property setup for the course2014 page type. New property variable can be added here and displayed on a page without recompiling the whole website which improves the process with much lower technical risk and more flexibility, allowing user editing of the content in SharePoint and/or EPiServer as user currently do for any page  and allowing the developer to create rich web content and test it of line.

A side effect of this development will allow us to upgrade the new Quals pages so the actual content can be edited in the same way as any other EPiServer page 



A bit of the template file

<link rel="stylesheet" type="text/css" href="/QualsReform2014/Styles/coursereform2014.css" />
<script type="text/JavaScript" src="/QualsReform2014/script/coursereform2014.js"></script>

<div style="background:red">

     
    <div class="course_id_container">
        <h1><script type="text/JavaScript">CourseID('[sp:CourseCode]')</script> <script type="text/JavaScript">CourseDescription('[sp:Title]')</script></h1>
    </div>

    <div id="course_info">
     
        
        <div id="course_container">
           
              <br />
           
              
              <div id="course_desciption">
              <script type="text/JavaScript">Introduction('[sp:Introduction]')</script>
              </div>
           
              <br />
           

The C# code that loads and parses the HTML template and replaces the tags with values

            try
            {
                string filename = TemplateFileName;


                if (!File.Exists(filename))
                {
                    template.Append("Template file not found

"
 + filename + "
");
                    return;
                }

                string courseCode = Request[StructuredSearchController.CourseQueryID];

                if (courseCode == null)
                {
                    template.Append("Course code argument supplied is invalid
"
);
                    return;
                }

                if (courseCode.Length < 3)
                {
                    template.Append("No course code argument supplied
"
);
                    return;
                }

                myStructuredRecored = DynamicStructuredDataUtility.GetCourse(courseCode);

                if (myStructuredRecored == null)
                {
                    template.Append("Invalid course code supplied
"
);
                    return;
                }
           

                //Grab the template and do the work
                using (StreamReader sr = new StreamReader(filename))
                {
                   
                    while (!sr.EndOfStream)
                    {
                        string fline = sr.ReadLine();


                        //Handle SharePoint fields
                        int FSPfldi = 0;

                        int SSPfldi = fline.IndexOf("[sp:", FSPfldi);

                        while (SSPfldi > -1)
                        {
                            FSPfldi = fline.IndexOf("]", SSPfldi);

                            string SPfieldName = fline.Substring(SSPfldi+4, FSPfldi- SSPfldi - 4);

                            //template.Append(SPfieldName + "
");

                            if (!string.IsNullOrEmpty(SPfieldName))
                            {
                                //string FieldValue = getStructuredData(SPfieldName);
                               
                                object obj = null;

                               try {
                                    obj = getStructuredData(SPfieldName); }
                                catch { obj = "SharePoint Field: " + SPfieldName + " Not Found
"
; }

                                if (obj != null)
                                {
                                    string FieldValue = obj.ToString();

                                    //template.Append(SPfieldName + " = " + FieldValue + "
");

                                    fline = fline.Replace("[sp:" + SPfieldName + "]", FieldValue);
                                }
                            }
                           
                            if (FSPfldi > fline.Length)
                                break;

                            SSPfldi = fline.ToUpper().IndexOf("[SP:", FSPfldi);


                        }//End of while (SSPfldi > -1)



                        //Handle EPiServer Fields
                        int FEPifldi = 0;

                        int SEPifldi = fline.IndexOf("[es:", FEPifldi);

                        while (SEPifldi > -1)
                        {
                            FEPifldi = fline.IndexOf("]", SEPifldi);

                            string EPifieldName = fline.Substring(SEPifldi + 4, FEPifldi - SEPifldi - 4);

                            //template.Append(EPifieldName + "
");

                            if (!string.IsNullOrEmpty(EPifieldName))
                            {
                                //string FieldValue = getStructuredData(SPfieldName);

                                object obj = null;

                                try
                                {
                                    obj = CurrentPage[EPifieldName].ToString();
                                }
                                catch { obj = "EPiServer Field: " + EPifieldName + " Not Found
"
; }

                                if (obj != null)
                                {
                                   string FieldValue = obj.ToString();

                                    //template.Append(EPifieldName + " = " + FieldValue + "
");

                                    fline = fline.Replace("[es:" + EPifieldName + "]", FieldValue);
                                }
                            }

                            if (FEPifldi > fline.Length)
                                break;

                            SEPifldi = fline.ToUpper().IndexOf("[ES:", FEPifldi);


                        }//End of while (SEPifldi > -1)

                        //Add modified line to output
                        template.Append(fline);
                    }
                }


               

                //Response.Write(template);

            }
            catch (Exception ex)
            {
                template.Append(ex.Message);
                template.Append(ex.StackTrace);
            }





  • Newsletter

    Sign up for the PlanetArduino Newsletter, which delivers the most popular articles via e-mail to your inbox every week. Just fill in the information below and submit.

  • Like Us on Facebook