Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sukhwinder 6 posts 87 karma points
    Feb 06, 2018 @ 07:25
    Sukhwinder
    0

    Deadlock problem while updating properties of nodes

    I have 1000 nodes in my project and i need to update some of the properties of all nodes. while updating nodes i faced deadlock and some time memory out of bound exception

    Here is the sample how i update single node in loop.

    int id = fetchNodes.Id; var contentService = ApplicationContext.Current.Services.ContentService; var doc = contentService.GetById(id); if (colbrandID != null && !string.IsNullOrEmpty(ID.ToString()))
    doc.SetValue("id", ID.ToString());

                                    if (Name != null && !string.IsNullOrEmpty(Name.ToString()))
                                        doc.SetValue("name", Name.ToString());
    
    
    
    
                                    contentService.SaveAndPublishWithStatus(doc);
    
  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Feb 06, 2018 @ 14:39
    Alex Skrypnyk
    2

    Hi Sukhwinder

    You don't need to instantiate contentService in a loop, move this line out of the loop:

    var contentService = ApplicationContext.Current.Services.ContentService;
    

    Can you show all the code you have?

    Alex

  • Ivan Ponomarenko 20 posts 105 karma points
    Feb 06, 2018 @ 15:08
    Ivan Ponomarenko
    102

    Hi Sukhwinder,

    In my case, when I needed to change a lot of content, I used overload of SaveAndPublishWithStatus method:

    ContentService.SaveAndPublishWithStatus(content, raiseEvents: false, userId: UmbracoUser.Id);
    

    Parameter raiseEvents - is optional boolean indicating whether or not to raise events. So if you do not subscribe on save events set it to false.

    Try it, maybe it helps you.

  • Sukhwinder 6 posts 87 karma points
    Feb 06, 2018 @ 15:08
    Sukhwinder
    0

    Hi Alex, Here is my code . I fetching data from a excel and update to nodes

    umbraco.NodeFactory.Node nodeC = new umbraco.NodeFactory.Node(nodeNum); for (int rowNumber = startRow + 1; rowNumber <= currentWorksheet.Dimension.End.Row; rowNumber++) // read each row from the start of the data (start row + 1 header row) to the end of the spreadsheet. { //hotelid object colHotelID = currentWorksheet.Cells[rowNumber, 1].Value;

                                object colbrandName = currentWorksheet.Cells[rowNumber, 3].Value != null && currentWorksheet.Cells[rowNumber, 3].Value.ToString() != "" ? currentWorksheet.Cells[rowNumber, 3].Value : "NA";
    
                                var fetchNodes = nodeC.ChildrenAsList.Where(x => x.GetProperty("ID") != null && x.GetProperty("ID").Value.ToString() == colHotelID.ToString()).FirstOrDefault();
                                //int id = childNode.Id;
                                if (fetchNodes != null)
                                {
                                    string amexID = (fetchNodes.GetProperty("ID").Value).ToString();
                                    int id = fetchNodes.Id;
                                    var contentService = ApplicationContext.Current.Services.ContentService;
                                    var doc = contentService.GetById(id);
    
                                    if (colbrandName != null && !string.IsNullOrEmpty(colbrandName.ToString()))
                                        doc.SetValue("brandname", colbrandName.ToString());
    
                                    contentService.SaveAndPublishWithStatus(doc);
                                    count += 1;
                                }
                            }//end of excel; loop
                        } 
    
  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Feb 06, 2018 @ 15:27
    Alex Skrypnyk
    1

    Hi Sukhwinder

    Please, follow the suggestion from Ivan, what version of Umbraco are you using?

    Alex

  • Sukhwinder 6 posts 87 karma points
    Feb 06, 2018 @ 15:38
    Sukhwinder
    0

    Thanks Alex and Ivan . I used 7.4.3 version of Umbraco. I will follow both the suggestions and notify u people what results i will get.

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Feb 06, 2018 @ 22:06
    Alex Skrypnyk
    2

    Also, do not use "umbraco.NodeFactory.Node" class, it's deprecated, use IPublishedContent instead - https://our.umbraco.org/documentation/reference/querying/ipublishedcontent

  • Sukhwinder 6 posts 87 karma points
    Feb 07, 2018 @ 09:00
    Sukhwinder
    1

    Thanks Alex and Ivan your suggestions worked and now i able to update all 1000 nodes at once without any problem. If anyone face same problem please follow both suggestions of Alex and Ivan.

  • Ivan Ponomarenko 20 posts 105 karma points
    Feb 07, 2018 @ 10:38
    Ivan Ponomarenko
    0

    Cool, we are glad to help you :)

    Be well!

Please Sign in or register to post replies

Write your reply to:

Draft