Skip to main content
Home  ›  Docs › Feature

PetaPoco Support

Warning ⚠️: New Docs Available

These are old docs and we haven't found the time to completely move them. You will find comprehensive new docs on docs.2sxc.org.

Introduced in Version 06.00.06

Technically, this has always been possible - but for lack of sample code people didn't know how to use it. So here goes:

@functions

{

       // for PetaPoco you must first create a class containing the fields you want

       private class fileRecord

       {

             public int FileId { get; set; }

             public string FileName { get; set; }

             public int Size { get; set; }

             public int FolderId { get; set; }

       }

 

       private IEnumerable<fileRecord> files;

      

       // Official place to provide data preparation. Is automatically called by 2sxc

       public override void CustomizeData()

       {

             var sqlCommand = "Select Top 10 * from Files Where PortalId = @0"; // PetaPoco requires numbered parameters like @0 instead of @PortalId

 

             var db = new PetaPoco.Database(Content.ConnectionName);

             files = db.Query<fileRecord>(sqlCommand, Dnn.Portal.PortalId);

       }

 

}

<div class="sc-element">

       @Content.Toolbar

       <h1>Simple Demo with PetaPoco Data access</h1>

       <p>This demo uses PetaPoco as a mini-ORM to get the data. More info on <a href="http://www.toptensoftware.com/petapoco/" target="_blank">PetaPoco here</a>.</p>

       <h2>The top 10 files found in this portal as returned by PetaPoco</h2>

       <ol>

             @foreach (var file in files)

             {

                    <li>@file.FileName (@file.FileId)</li>

             }

       </ol>

</div>