More in this section

Forums / Developing with Sitefinity / Pages API help

Pages API help

7 posts, 0 answered
  1. Chris
    Chris avatar
    87 posts
    Registered:
    11 May 2010
    24 May 2010
    Link to this post
    Hi,

    I am evaluating SiteFinity for my company who is looking at switching the CMS integration we include for our sites. I am struggling slightly with the pages API to achieve what would be a very important feature for us.

    What I need to do is...

    1. Programatically get a published page (by name) and it's controls/content.
    2. Programatically get the latest draft (or published if there is no draft) version of the page and it's controls/content.

    Getting the published paged doesn't seem to be much of an issue however I have found several different ways of doing this (using GetPages and iterating through to match on name - using LINQ Where function to match the name doesn't seem to work; using GetPages with CmsQueryConditions to match on name and then accessing  CmsPageInfo.CmsPage.LanguageVersions) so I'm hoping you could advise on the cheapest method (in terms of performance/data access/processing) of getting the published page.

    So far I have not been able to work out how to get near the latest draft.

    Any help would be gratefully received. Thanks.
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    24 May 2010
    Link to this post
    Hello Chris,

    1. There is no direct method that will return the page by name. First you could get the pages and resolve the name by Id. another option is using the SiteMapNode and getting the page by url - the page name is added to the page url. The third option is using List of CmsQueryCondition

    var searchField = "Radius";
    int totalRows;
    List<CmsQueryCondition> conditions = new List<CmsQueryCondition>();
    conditions.Add(new CmsQueryCondition(
                           "Name",
                           Nolics.ORMapper.Base.QueryCondition.Like,
                           string.Concat("%", searchField, "%")));
     
    var Manager = new CmsManager();
    var result = Manager.GetPages(0, 0, string.Empty, System.ComponentModel.ListSortDirection.Descending, conditions, out totalRows);

    2. You could get a Draft version of  page when you work with workflow enabled. The Draft is ApprovalStatus of the workflow life cycle.

    sample code ( you should have the page and its ID)

    ICmsPage cmsPage =(ICmsPage)cmsManager.GetPage(pageID, true);
    WorkflowInstance workflow = cmsPage.GetWorkflow();
    PageWorkflow activity = (PageWorkflow)workflow.Activity;
    if (activity.ApprovalStatus == ApprovalStatus.Draft)
    {
      // the page is in status draft
    }


    or directly get the page status by passing the ID and creating CmsPageInfo object.

    var cmsPage1 = (ICmsPage)Manager.GetPage(pageID);
     Telerik.Cms.CmsPageInfo pageInfo = new Telerik.Cms.CmsPageInfo(cmsPage1);
    string s = pageInfo.Status


    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. Chris
    Chris avatar
    87 posts
    Registered:
    11 May 2010
    25 May 2010
    Link to this post
    Thanks for your help Ivan. I have got this working now. Can I do the same for content?

    e.g. Get latest version of content

    var icontent = GetContentItem();
    var versionItems = new VersionManager()
        .GetItems(icontent.ID, GetCulture("en"))
        .Cast<IVersionItem>().ToList();
                    
    var latestContent = new StagedContent(versionItems.First(),     ContentManager.Providers[contentItem.ProviderName]).Content;

    That seems to work, but how do I get the published version of this content? The following code doesn't work for selecting the published version as each versionItems's status is Published - I always end up with the latest version even if it is not published.

    var publishedItem = versionItems.Select(vi => new StagedContent(vi, ContentManager.Providers[contentItem.ProviderName])).Where(
                    i => i.Status.Equals(ContentStatus.Published)).FirstOrDefault();

    Thanks again.
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    25 May 2010
    Link to this post
    Hi Chris,

    To get a certain version of a content item you can use GetItem method of VersionManager

    GetItem(Guid id, CultureInfo culture, int version) - Gets version of an item specified by ID, culture and version number. The method returns IVersionItem object. To return the IContent object for a given version you could use the code below

    public IContent GetContentVersion(IContent content, int version)
    {
        VersionManager manager = new Telerik.Versioning.VersionManager();
        IVersionItem item = manager.GetItem(content.ID, System.Globalization.CultureInfo.InvariantCulture, version);
        if (item != null)
        {
            return new StagedContent(item, ContentManager.Providers[content.ProviderName]);
        }
        return null;
    }

    ContentManger methods - GetContent and GetStagedContent return the latest version of an content item.

    All the best,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  5. Chris
    Chris avatar
    87 posts
    Registered:
    11 May 2010
    26 May 2010
    Link to this post
    Hi Ivan,

    Thanks for the reply. This makes sense but my problem is how I identify which version is published without knowing in advanced? For example I may have the following scenario with a Generic Content item:

    Version 4 - Draft
    Version 3 - Approved
    Version 2 - Published
    Version 1 - Previously Published

    How can I tell what the latest published version is (in this case version 2)?
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    28 May 2010
    Link to this post
    Hi Chris,

    You could try to use GetCurrentState which returns the current state for the specified generic content item id. This will allow  you to check whether the content item is published or not. If the content item is not published you can get the previous version which should be published.

    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  7. Chris
    Chris avatar
    87 posts
    Registered:
    11 May 2010
    04 Jun 2010
    Link to this post
    I don't think this would work for us as it's quite likely that a user could edit some content twice without publishing so version 1 could be published, but version 2 and 3 could be drafts.

    I think I have solved this now by getting ContentVersions from the CmsContentBase object and then getting the ones with the published status:

    // content is IContent
    var versions = ((CmsContentBase) content).ContentVersions;
    var publishedLanguageVersion = versions.Where(v => v.Status.Equals(ContentStatus.Published)).FirstOrDefault();

    Not sure if the first one will necessarily be the one I want but I should be able to work with this more easily.

    Thanks for your help Ivan.
Register for webinar
7 posts, 0 answered