1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Associating with an existing CmsPage

Associating with an existing CmsPage

10 posts, 0 answered
  1. Gabe Sumner
    Gabe Sumner avatar
    440 posts
    Registered:
    09 Sep 2007
    14 Sep 2007
    Link to this post

    I am using Nolics to create a new class.  I want to create a new object and associate it with a Sitefinity page.  In other words, I want my object to "belong" to an existing Sitefinity page.

    When defining my Nolics dbclass I have added the following line:

    link "Telerik.Cms.Data.CmsPage"

    This seems like it should work, but it does not.  Can anyone give me an idea of what the right link reference to a CmsPage would be?

    Thanks,

    Gabe
    =============

  2. Gabe Sumner
    Gabe Sumner avatar
    440 posts
    Registered:
    09 Sep 2007
    14 Sep 2007
    Link to this post
    I might have figured it out.  The following code compiled:

    link "Telerik.Cms.Data.PageBase" PageBase_lnk : string app, guid pagebaseid;

    Not sure if this is correct or not.  I'll continue to experiment tomorrow.
  3. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    14 Sep 2007
    Link to this post
    Yes Gabe,
    This should be the correct link derfinition:
    link "Telerik.Cms.Data.PageBase" PageBase_lnk : string app, guid pagebaseid;
     PageBase class is the base class for CmsPage and PageTemplate.

    Sincerely yours,
    Pepi
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  4. Gabe Sumner
    Gabe Sumner avatar
    440 posts
    Registered:
    09 Sep 2007
    17 Sep 2007
    Link to this post
    I understand that I can get the current PageID via the following code:

    CmsUrlContext.Current.PageID

    How do I get the current "PageBase" object?

    Thanks,

    Gabe
    ===================
  5. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    18 Sep 2007
    Link to this post
    Hi Gabe,

    You can get the current page in this way for example:

    // Init current page 
    object id = HttpContext.Current.Items[UrlHelper.PageIdKey]; 
    if (id == null) 
        throw new HttpException("Invalid page request."); 
     
    CmsManager man = new CmsManager(); 
    this.cmsPage = man.GetPage((Guid)id, true); 
     

    cmsPage is an object of type IPage.

    The other way to get the description for the current page is:

    ICmsUrlContext context = CmsUrlContext.Current; 

    The first implementation works both for pages and templates and the second one works for pages only.

    Hope this helps.

    Regards,
    Pepi
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  6. Gabe Sumner
    Gabe Sumner avatar
    440 posts
    Registered:
    09 Sep 2007
    18 Sep 2007
    Link to this post
    I haven't yet gotten a chance to test the code you show above; however I'm wondering can a "ICmsUrlContext" equal a "PageBase" object?  Would I have to cast the "ICmsUrlContext" as a "PageBase" object?

    For example:

    PageBase newBase = new PageBase();
    newBase = (PageBase)(CmsUrlContext.Current);

    Does something like this work?  In the end I need a PageBase object, not a ICmsUrlContext object.   I think.

    Gabe
    ===================
  7. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    19 Sep 2007
    Link to this post
    Hello Gabe,

    You cannot cast ICmsUrlContext object to PageBase type. Please try the examples from our previous post because we think that is what you need.

    Kind regards,
    Pepi
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  8. Gabe Sumner
    Gabe Sumner avatar
    440 posts
    Registered:
    09 Sep 2007
    19 Sep 2007
    Link to this post

    Ok, I'm beginning to fear that I'm going about this wrong.  In the end I want to end up with a "Telerik.Cms.Data.PageBase" object that represents the page currently being viewed.

    The examples above yield either an "IPage" object or an "ICmsUrlContext" object.  Neither of which seem to easily be convertible to a "PageBase" object.

    But let me back off of this and simply describe what I'm trying to do:

    I want to create a small control that I can drag & drop onto Sitefinity pages.  This control would allow me to associate one or more lines of data with the current page.

    To accomplish this, I first created a simple Nolics "dbclass" file.  It is as follows:

    namespace Tests.Data;  
     
    dbclass Test [TableName="Tests_Test",WebBinder=true] {  
        primary key guid ID[AutoGenGUID=True];  
        string TestField;  
        link "Telerik.Cms.Data.PageBase" PageBase_lnk : string app[50], guid pagebaseid;  
    }  
     
    query TestQuery for Test {  
     

     

    This is really simple stuff, but notice the "link" I added. Maybe I don't understand this well enough, but I thought that this link would allow me to associate data I insert with existing Sitefinity page.  The Sitefinity page, I thought, was represented by the "Telerik.Cms.Data.PageBase" object. 

    Perhaps I should stop right here though.  Is this an incorrect way of doing this?  Should I instead just associate my data with the page's Guid?  You examples above make it easy to get ahold of this Guid.  Should I not make use of the Nolics "link" feature?

    The "dbclass" above compiled just fine.  My next step was to insert data via Nolics and my control.  Here is the code I was trying to use:

    DataConnection.Initialize();  
    Transaction trans = new Transaction();  
     
    Test ourTest = new Test();  
    OdbClass.Create(ourTest, trans);  
    ourTest.TestField = "My string";  
    ourTest.PageBase_lnk = ((PageBase)(CmsUrlContext.Current));  
    trans.Commit(); 


    This obviously doesn't work.  "CmsUrlContext.Current" exists, but it is not something that can be converted to a "PageBase" object.  "ourTest.PageBase_lnk" is a "PageBase" object.  I can't associate objects of another type with it.  I have played with your examples, but none of them yield a "PageBase" object in the end. 

    Perhaps my core approach is wrong though.  Would it be better to just associate my data with a Guid instead of "linking" to a "PageBase" object?  If I do that, however, do I forfeit performance gains that Nolics might offer if it were aware of the "link"?

    Sorry for all the questions.  I'm struggling to understand how best to work in this framework.

    Gabe
    =============

  9. Bob
    Bob avatar
    330 posts
    Registered:
    30 Dec 2016
    20 Sep 2007
    Link to this post
    Hi Gabe,

    You can do it both ways, by loosely coupling your objects to Sitefinity pages using IDs or directly linking to PageBase object. I suggest the first approach and here are the reasons why:

    1.  PageBase is defined in Telerik.Cms.Data assembly which represents the data access layer. The class also implements Telerik.Cms.IPage interface. The data manger is accessing the underlying data through interfaces only. This way, even if you change the data provider, let’s say we may provide an alternative data provider based on Entity Framework in the future, your code will still work.
    2. To use PageBase object, you have to have very good understanding of Sitefinity’s architecture. For example, PageBase class is inherited by CmsPage, StagedPage, PageRoot and PageTemplate classes. One way to obtain a reference to the class containing the actual data in a User Control for example is like this:
      protected void Page_Load(object sender, EventArgs e)  
      {  
          Telerik.Cms.Web.InternalPage internalPage = this.Page as Telerik.Cms.Web.InternalPage;  
          if (internalPage != null)  
          {  
              Telerik.Cms.Data.PageBase pageBase = internalPage.CmsPage as Telerik.Cms.Data.PageBase;  
              // pageBase is actually the dbclass  
          }  
      }  
       

    Note that this code will return reference to CmsPage object when the control is viewed on the public side but in preview or edit mode, it will return StagedPage if placed on a page or PageTemplate if the control is placed on a template. Unfortunately, we still don’t have good documentation for the API and architecture.

    Just to clarify on CmsUrlContext, it does not hold references to data classes. It only holds lightweight information about pages that is cached for quickly serving requests. Therefore, you cannot cast CmsUrlContext to PageBase but you can retrieve the page ID for the current requested and obtain reference to PageBase instance with CmsManger:

    protected void Page_Load(object sender, EventArgs e)  
    {  
        CmsManager manger = new CmsManager();  
        Guid pageId = CmsUrlContext.Current.PageID;  
        IPage cmsPage = manger.GetPage(pageId) as IPage;  
        if (cmsPage != null)  
        {  
            // cmsPage will be PageBase instance  
        }  

    This is an alternative to the above code.

    All the best,

      Bob
      the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  10. Gabe Sumner
    Gabe Sumner avatar
    440 posts
    Registered:
    09 Sep 2007
    20 Sep 2007
    Link to this post
    Great reply Bob!  That helped a lot. 

    I am going to go with loosely coupling my data using the Sitefinity page Guid's.  This seems easy enough.

    This means my Nolics "dbclass"  will look similar to this:

    namespace Tests.Data;     
        
    dbclass Test [TableName="Tests_Test",WebBinder=true] {     
        primary key guid ID[AutoGenGUID=True];     
        string TestField;     
        guid PageID;  
    }     
        
    query TestQuery for Test {     
        
    }    
     

    It looks like I made this vastly more complicated than it needed to be.  Sorry about that. 

    Thanks again,

    Gabe
    ==============
Register for webinar
10 posts, 0 answered