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

Forums / Developing with Sitefinity / Sample Code: Generic Content Versioning

Sample Code: Generic Content Versioning

16 posts, 2 answered
  1. Jeff
    Jeff avatar
    124 posts
    Registered:
    05 Aug 2007
    30 Jun 2009
    Link to this post
    Hello,

    From what I can see, there is no documentation on working with different versions of Generic Content items (or pages for that matter).

    Can you provide some sample code that demonstrates:
        *  Creating and retrieving a Generic Content item
        *  Modifying the GC item and saving it as a new version
        *  Retrieving the original version of the GC item

    Thanks,
    Jeff
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    30 Jun 2009
    Link to this post
    Hi Jeff,

    You need to work with VersionManager class.

    ContentManager contentManager = new ContentManager("GenericContent");  
    IContent content = (IContent)contentManager.GetContent(0, 1)[0];  
    VersionManager versionManager = new VersionManager();  
    versionManager.RollbackItem(content.ID, null, 2);  
    contentManager.PublishContent(content.ID);  

    For pages you can use the same approach

    VersionManager vm = new VersionManager();       
    bool published = pageForEdit.Status == PageStatus.Published;   
    int whichVersion = vm.GetVersion(pageForEdit.ID, System.Threading.Thread.CurrentThread.CurrentUICulture);  

    Generally when you get a content item (use GetContent() or GetStagedContent) for edit and then publish it you create a new version of this item.

    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. Jeff
    Jeff avatar
    124 posts
    Registered:
    05 Aug 2007
    30 Jun 2009
    Link to this post
    Hi Ivan,

    Thanks for the quick response. However, I'm still having trouble. I can't even manage to create a second version of a Generic Content object.

    Can you please review this short code sample and tell me what I'm doing wrong:
            // Create and publish version 1 
            ContentManager manager = new ContentManager("Generic_Content"); 
            IContent content = manager.CreateContent("text/html"); 
            content.Content = "Version 1 Content"
            manager.SaveContent(content); 
            manager.PublishContent(content.ID); 
     
            // Modify and publish version 2 
            content = manager.GetContent(content.ID); 
            content.Content = "Version 2 Content"
            manager.SaveContent(content); 
            manager.PublishContent(content.ID); // Reverts back to version 1 
     
            VersionManager versionManager = new VersionManager(); 
     
            // Exception: Object reference not set to an instance of an object. 
            // @ Nolics.ORMapper.Base.PrimeryKeyQuery.AddPk(Object[] keys) 
            IVersionItem ContentOld = versionManager.GetItem(content.ID, Thread.CurrentThread.CurrentUICulture, 0); 
     


    Thanks,
    Jeff
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    02 Jul 2009
    Link to this post
    Hi Jeff,

    1. There is not zero version of a content item

    IVersionItem ContentOld = versionManager.GetItem(content.ID, Thread.CurrentThread.CurrentUICulture, 0

    2. The content at this line is null - this is the reason why you are getting an error.

    content = manager.GetContent(content.ID) 


    Below is the correct code.

           ContentManager manager = new ContentManager("News"); 
            IContent content = manager.CreateContent("text/html"); 
            content.SetMetaData("Title""This is my content version1"); 
            content.Content = "Version 1 Content"
            // this content here is version 1 
            manager.SaveContent(content); 
     
            // get the content that we have already created and save it. 
            // Thus we are creating a new version of this content - version 2 
            StagedContent stagedC = manager.GetStagedContent(content.ID) as StagedContent; 
             
            VersionManager Vmanager = new VersionManager(); 
            // Revert the content with version2 to content with version1 - the initial content 
            Vmanager.RollbackItem(stagedC.ID, null, 1); 
            manager.PublishContent(stagedC.ID); 
     


    Greetings,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
    Answered
  5. Jeff
    Jeff avatar
    124 posts
    Registered:
    05 Aug 2007
    02 Jul 2009
    Link to this post
    Thanks Ivan. We're getting closer: I can now save a new version of the content.

    The last thing I need is to retrieve an old version of the content without rolling it back. I don't wish to republish the old version. I just want to access its content.

    Here's the code I have so far:

            ContentManager manager = new ContentManager("Generic_Content"); 
            IContent content = manager.CreateContent("text/html"); 
            content.SetMetaData("Name""Content version 1"); 
            content.Content = "Version 1 Content"
            manager.SaveContent(content); 
            manager.PublishContent(content.ID); // Must publish otherwise status shows "Locked" 
     
            StagedContent stagedC = manager.GetStagedContent(content.ID) as StagedContent; 
            stagedC.SetMetaData("Name""Content version 2"); 
            stagedC.Content = "Version 2 Content"
            manager.SaveContent(stagedC); 
            manager.PublishContent(stagedC.ID);  
      
            // ????? 
            // Now get version 1 Generic Content without rolling it back. 
            // ????? 
     

    Thanks again,
    Jeff
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    02 Jul 2009
    Link to this post
    Hi Jeff,

    Actually using

    StagedContent stagedC = manager.GetStagedContent(content.ID) as StagedContent;  

    you get the last version of a content item. By doing so you automatically create a new version of a content item. To get a certain version you can use Getitem method Vmanager.GetItem(stagedC.ID, null, 2);

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  7. Jeff
    Jeff avatar
    124 posts
    Registered:
    05 Aug 2007
    02 Jul 2009
    Link to this post
    OK. VManager.GetItem works. Kind of. But it returns a VersionItem object not an IContent or ContentBase.

    In the VersionItem, I see the Generic_Content info in a property called "Data" as a Hashtable. Row[0] might be the Content, Row[1] might be the Mime Type, Row[2] might be a meta data item.

    I suppose I could piece together an IContent object from the scraps in the Hashtable but I'm hoping there's a more elegant way to simply get an IContent-based object of a previous version.

    Jeff
  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    03 Jul 2009
    Link to this post
    Hello Jeff,

    You are creating IVersioning item object, but you pass IContent ID in the GetItems method and returns its version.

    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  9. Jeff
    Jeff avatar
    124 posts
    Registered:
    05 Aug 2007
    06 Jul 2009
    Link to this post
    Hi Ivan,

    I didn't understand your last response. How am I supposed to get an IContent object containing a previous version of a generic content item? Can you provide a code sample?

    Thanks,
    Jeff
  10. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    07 Jul 2009
    Link to this post
    Hello Jeff,

    Using RollbackItem you can directly specify the content ID of an item you want to roll back. In this case it is event not necessary to create an IVersioningItem object. You can see this in the code from my second post.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  11. Jeff
    Jeff avatar
    124 posts
    Registered:
    05 Aug 2007
    07 Jul 2009
    Link to this post
    Hi Ivan,

    Thanks but I'm trying to get previous versions as an IContent object without rolling back the Generic Content item.

    Jeff
  12. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    10 Jul 2009
    Link to this post
    Hello Jeff,

    Below is a required sample that returns IContent object version.

        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
        } 
     


    All the best,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
    Answered
  13. Jeff
    Jeff avatar
    124 posts
    Registered:
    05 Aug 2007
    11 Jul 2009
    Link to this post
    Hi Ivan,

    Wonderful. That did the trick.

    Thanks again.

    Jeff
  14. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    12 Jul 2009
    Link to this post
    Hi Jeff,

    Always glad to help.

    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  15. Raj
    Raj avatar
    1 posts
    Registered:
    26 Sep 2011
    26 Sep 2011
    Link to this post
    Hello,

    I am new to Sitefinity and trying to learn it so that I can create a new website using it. I have seen your demo website and it's code. In this site many of the things are actually done at a page level using Sitefinity administrator UI. I could not find much of the ASP.NET code in the code files. 

    I would like to see the samples where Sitefinity APIs are used to 
    # create/modify pages, 
    # adding new controls setting properties, 
    # handling control events 
    # Accessing content repository through code
    # showing content dynamically on page, etc.

    I have seen the code snippets for above things in Developers manual so I understand it's possible with sitefinity. But since I am very new to sitefinity I get a very basic questions like where to actually put this code because actual page created in Sitemap doesn't have physical existence as a .aspx file. 
    So is it to be added to Template or do I need to create control / module every time I need to add a smallest functionality to my site?

    Please redirect me to any step by step tutorials that will help me in understanding the Sitefinity API usage. Or a sample code where similar functionality is achieved using APIs.

    I am really stuck up with API thing. I am able to create many things at design time but I would like to do same using the code at Run time. 
    Please help.

    Thanks,
  16. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    26 Sep 2011
    Link to this post
    Hello Raj,

    You can use custom or user controls to add the code you want to use. You can create standard ASP.NET web user control and put the login inside the cs file or use class libraries with classes in them.

    Regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Register for webinar
16 posts, 2 answered