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

Forums / Suggestions / Please provide more documentation on programmatically querying and updating dynamic module content

Please provide more documentation on programmatically querying and updating dynamic module content

12 posts, 0 answered
  1. David
    David avatar
    99 posts
    Registered:
    06 Sep 2012
    08 Mar 2012
    Link to this post
    I could not find any samples or documentation on how to edit existing (Module Builder) content. All of the code samples I have found on your site deal with programmatically creating and publishing new content.

    In particular I'd like to see a sample on how to get a dynamic content item by Id and then edit and save the changes.

    I did find the following code snippet for editing an existing News module. Is this also the way we have to edit Module Builder content as well? I have been unable to modify it to work with Module Builder content.

    Can you explain why the FullName field is placed in dictionary object but none of the other fields are?

    How come the code snippet uses Workflowmanager instead of manager.Lifecycle.Publish(temp)  ?



    http://www.sitefinity.com/documentation/documentationarticles/developers-guide/sitefinity-essentials/modules/content-lifecycle/editing-content 



    private void ModifyItemByLiveIDNativeAPI(Guid liveId)
    {
        NewsManager manager = NewsManager.GetManager();
        NewsItem live = manager.GetNewsItems().Where(newsItem => newsItem.Id == liveId).FirstOrDefault();
      
        if (live != null)
        {
            //Edit the item to get the master version.
            NewsItem master = manager.Lifecycle.Edit(live) as NewsItem;
      
            //Check out the master to get a temp version.
            NewsItem temp = manager.Lifecycle.CheckOut(master) as NewsItem;
      
            //Make the modifications to the temp version.
            temp.Title = "New Title";
            temp.LastModified = DateTime.UtcNow;
            temp.Urls.Clear();
            temp.UrlName = Regex.Replace(temp.Title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
      
            //Checkin the temp and get the updated master version.
            //After the check in the temp version is deleted.
            master = manager.Lifecycle.CheckIn(temp) as NewsItem;
      
            manager.SaveChanges();
      
            //Publish the news item.
            var bag = new Dictionary<string, string>();
            bag.Add("ContentType", typeof(NewsItem).FullName);
            WorkflowManager.MessageWorkflow(master.Id, typeof(NewsItem), null, "Publish", false, bag);
        }
    }

  2. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    08 Mar 2012
    Link to this post
    It pretty much self-documents though

    - Nav: ~/Sitefinity/Administration/Module-builder
    - Click the Module
    - Click the "Code References for <module name>" on the far right

    Gives you exact cut and paste code snippets for all these cases using your custom modules EXACT definition

    (Assuming your module stored "cases")
    Create a case
    Delete a case
    Get a case by ID
    Get a collection of cases
    Get a case through Filtering
    Publish a case
    Check in and Check out a case
    Integration example for a case
  3. David
    David avatar
    99 posts
    Registered:
    06 Sep 2012
    08 Mar 2012
    Link to this post
    Steve, none of those examples deal with updating existing content.

    Create a case
    Delete a case
    Get a case by ID
    Get a collection of cases
    Get a case through Filtering
    Publish a case
    Check in and Check out a case
    Integration example for a case 
  4. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    08 Mar 2012
    Link to this post
    - Get A case by ID (or Get a collection of cases)
    - Change the data
    - Call SaveChanges()

    ?
  5. David
    David avatar
    99 posts
    Registered:
    06 Sep 2012
    08 Mar 2012
    Link to this post
    Thanks everyone for helping. I got it working and will post it soon as I do some more testing.
  6. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    08 Mar 2012
    Link to this post
    Oh good thanks ;)  IMO very neat self documenting feature....one of those things you never thought you needed until you see it.

    If you have a moment would you mind marking the post as answered by any chance?
  7. David
    David avatar
    99 posts
    Registered:
    06 Sep 2012
    08 Mar 2012
    Link to this post
    Everything appeared to be working in the custom pages I added to the front-end but none of the edits appear in the back-end. 
  8. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    08 Mar 2012
    Link to this post
    Can you post your code?
  9. David
    David avatar
    99 posts
    Registered:
    06 Sep 2012
    08 Mar 2012
    Link to this post
    Steve, Sure, here is the code. All of my edits show up in the ContestantsGridView that gets bound in the last line of code. When I view the content from the backoffice, none of the edits show up.

    protected void SaveButton_Click(object sender, EventArgs e)
    {
     
        DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
        Type contestantType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.RioGrandeFishingTournamentContestants.Contestant");
         
     
        // This is how we get the contestant item by ID
        DynamicContent contestantItem = new DynamicContent();
     
        if (Contestant1IdHiddenField.Value.Trim().Length > 0)
        {
            contestantItem = dynamicModuleManager.GetDataItem(contestantType, new Guid(Contestant1IdHiddenField.Value.Trim()));
     
            DynamicContent master = dynamicModuleManager.Lifecycle.Edit(contestantItem) as DynamicContent;
            DynamicContent temp = dynamicModuleManager.Lifecycle.CheckOut(master) as DynamicContent;
     
            temp.SetValue("FirstName", FirstName1TextBox.Text);
            temp.SetValue("LastName", LastName1TextBox.Text);
            temp.SetValue("Email", FirstName1TextBox.Text);
     
            //contestantItem.SetValue("PublicationDate", DateTime.Now);
            temp.SetValue("Address1", Address11TextBox.Text);
            temp.SetValue("Address2", Address21TextBox.Text);
            temp.SetValue("City", City1TextBox.Text);
            //contestantItem.SetValue("IsBoatCaptain", true);
            temp.SetValue("Zip", Zip1TextBox.Text);
            temp.SetValue("Phone", Phone1TextBox.Text);
     
            master = dynamicModuleManager.Lifecycle.CheckIn(temp) as DynamicContent;
            dynamicModuleManager.Lifecycle.Publish(master);
            dynamicModuleManager.SaveChanges();
        }
     
        if (Contestant2IdHiddenField.Value.Trim().Length > 0)
        {
            contestantItem = dynamicModuleManager.GetDataItem(contestantType, new Guid(Contestant2IdHiddenField.Value.Trim()));
     
            DynamicContent master = dynamicModuleManager.Lifecycle.Edit(contestantItem) as DynamicContent;
            DynamicContent temp = dynamicModuleManager.Lifecycle.CheckOut(master) as DynamicContent;
     
            temp.SetValue("FirstName", FirstName2TextBox.Text);
            temp.SetValue("LastName", LastName2TextBox.Text);
            temp.SetValue("Email", FirstName2TextBox.Text);
     
            //contestantItem.SetValue("PublicationDate", DateTime.Now);
            temp.SetValue("Address1", Address12TextBox.Text);
            temp.SetValue("Address2", Address22TextBox.Text);
            temp.SetValue("City", City2TextBox.Text);
            //contestantItem.SetValue("IsBoatCaptain", true);
            temp.SetValue("Zip", Zip2TextBox.Text);
            temp.SetValue("Phone", Phone2TextBox.Text);
     
            master = dynamicModuleManager.Lifecycle.CheckIn(temp) as DynamicContent;
            dynamicModuleManager.Lifecycle.Publish(master);
            dynamicModuleManager.SaveChanges();
        }
     
        if (Contestant3IdHiddenField.Value.Trim().Length > 0)
        {
            contestantItem = dynamicModuleManager.GetDataItem(contestantType, new Guid(Contestant3IdHiddenField.Value.Trim()));
     
            DynamicContent master = dynamicModuleManager.Lifecycle.Edit(contestantItem) as DynamicContent;
            DynamicContent temp = dynamicModuleManager.Lifecycle.CheckOut(master) as DynamicContent;
     
            temp.SetValue("FirstName", FirstName3TextBox.Text);
            temp.SetValue("LastName", LastName3TextBox.Text);
            temp.SetValue("Email", FirstName3TextBox.Text);
     
            //contestantItem.SetValue("PublicationDate", DateTime.Now);
            temp.SetValue("Address1", Address13TextBox.Text);
            temp.SetValue("Address2", Address23TextBox.Text);
            temp.SetValue("City", City3TextBox.Text);
            //contestantItem.SetValue("IsBoatCaptain", true);
            temp.SetValue("Zip", Zip3TextBox.Text);
            temp.SetValue("Phone", Phone3TextBox.Text);
     
            master = dynamicModuleManager.Lifecycle.CheckIn(temp) as DynamicContent;
            dynamicModuleManager.Lifecycle.Publish(master);
            dynamicModuleManager.SaveChanges();
        }
     
        if (Contestant4IdHiddenField.Value.Trim().Length > 0)
        {
            contestantItem = dynamicModuleManager.GetDataItem(contestantType, new Guid(Contestant4IdHiddenField.Value.Trim()));
     
            DynamicContent master = dynamicModuleManager.Lifecycle.Edit(contestantItem) as DynamicContent;
            DynamicContent temp = dynamicModuleManager.Lifecycle.CheckOut(master) as DynamicContent;
     
            temp.SetValue("FirstName", FirstName4TextBox.Text);
            temp.SetValue("LastName", LastName4TextBox.Text);
            temp.SetValue("Email", FirstName4TextBox.Text);
     
            //contestantItem.SetValue("PublicationDate", DateTime.Now);
            temp.SetValue("Address1", Address14TextBox.Text);
            temp.SetValue("Address2", Address24TextBox.Text);
            temp.SetValue("City", City4TextBox.Text);
            //contestantItem.SetValue("IsBoatCaptain", true);
            temp.SetValue("Zip", Zip4TextBox.Text);
            temp.SetValue("Phone", Phone4TextBox.Text);
     
            master = dynamicModuleManager.Lifecycle.CheckIn(temp) as DynamicContent;
            dynamicModuleManager.Lifecycle.Publish(master);
            dynamicModuleManager.SaveChanges();
        }
     
     
        Guid orderId = Guid.Parse(OrderIdHiddenField.Value);
     
     
        //Type contestantType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.RioGrandeFishingTournamentContestants.Contestant");
        var orders = dynamicModuleManager.GetDataItems(contestantType).Where(i => i.Status == ContentLifecycleStatus.Live && i.Visible == true && i.GetValue<Guid>("OrderId") == orderId);
     
     
        RegistrationMultiView.ActiveViewIndex = 2;
        ContestantsGridView.DataSource = orders;
        ContestantsGridView.DataBind();
     
    }



  10. David
    David avatar
    99 posts
    Registered:
    06 Sep 2012
    08 Mar 2012
    Link to this post
    I added the publication date which I had previously commented out and it appears to be working. Still need to some more testing though. 

    contestantItem.SetValue("PublicationDate", DateTime.Now); 
  11. Alex
    Alex avatar
    1 posts
    Registered:
    13 Apr 2013
    18 Apr 2013 in reply to David
    Link to this post
    Thanks for the code. It's not rocket science but there was always some confusion over updating existing content particularly the bit about Lifecycle.Edit & Lifecycle.CheckOut.

    Can someone confirm if we should be using DateTime.UtcNow? Isn't everything in Sitefinity UTC?
  12. Stefani Tacheva
    Stefani Tacheva avatar
    718 posts
    Registered:
    09 Sep 2016
    23 Apr 2013
    Link to this post
    Hello,

    When you create an item in the Sitefinity backend and you are for example UTC/GMT +2 hours and it is 09:00 pm (the time of your server) in the database this time is saved in standard UTC, two hours earlier e.g. 07:00 pm. When a user from German for example want to see the time of the item, this item is taken from the database in UTC time and is converted to the server time. So the user will see that the item is created in 09:00 pm. If you want to change the time of your server go to Administration -> Settings -> TimeZone and the Browser or Specific timezone offset configuration is located under Administration -> Settings -> Advanced ->System ->UI Time Zones Config.

    When you create the items via code. If you say for example DateTime.UtcNow it will take your the UTC time, in the database will e save as UTC and then will be converted to the server time when someone try to see it. Please note that you should always persist time according to a unified standard that is not affected by daylight savings. GMT and UTC have been mentioned by different people, though UTC is used in Sitefinity.

    Regards,
    Stefani Tacheva
    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
12 posts, 0 answered