Please provide more documentation on programmatically queryi

Posted by Community Admin on 05-Aug-2018 16:30

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

All Replies

Posted by Community Admin on 08-Mar-2012 00:00

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);
    

Posted by Community Admin on 08-Mar-2012 00:00

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

Posted by Community Admin on 08-Mar-2012 00:00

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 

Posted by Community Admin on 08-Mar-2012 00:00

- Get A case by ID (or Get a collection of cases)
- Change the data
- Call SaveChanges()

?

Posted by Community Admin on 08-Mar-2012 00:00

Thanks everyone for helping. I got it working and will post it soon as I do some more testing.

Posted by Community Admin on 08-Mar-2012 00:00

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?

Posted by Community Admin on 08-Mar-2012 00:00

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. 

Posted by Community Admin on 08-Mar-2012 00:00

Can you post your code?

Posted by Community Admin on 08-Mar-2012 00:00

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();
 



Posted by Community Admin on 08-Mar-2012 00:00

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); 

Posted by Community Admin on 18-Apr-2013 00:00

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?

Posted by Community Admin on 23-Apr-2013 00:00

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

This thread is closed