0
votes

I have a command that I've added to a "national" node in the Sitecore Editor. The command generates a list of the 50 state nodes beneath it. It then goes through the state nodes (1 at a time) and generates a list of local nodes under each state node. Within the lists of local nodes, I iterate through them and check if the new item exists - if it does not, I add (create) it as a child under the local node. Ultimately, there are nearly 300 local items that are being added during the course of this command.

Is there a more efficient way to do this (fast query to get the 300 local nodes into one list, then check if the item exists, and create it)? If so, I'm not sure how to do that...

I'm not sure what is the most costly part of the operation. Ultimately, I'm still doing up to 300 separate queries to check if it's there, followed by insert statements, so that may still take a while to run. If so, which setting in web config will increase the applicable "timeout" setting in Sitecore? The sample structure is as follows:

//Derive the template from the name of the item (page) that was passed in - this assumes that the template name and the item name are the same
Sitecore.Data.Database database = Sitecore.Data.Database.GetDatabase("master");                

TemplateItem contentPageTemplate = database.SelectSingleItem("fast:/sitecore/Templates/User Defined/Home/Pages/Local Site/" + newPage);

Sitecore.Data.Items.Item[] stateNodes = null;
Sitecore.Data.Items.Item[] localNodes = null;
Item localHomePage = null;
Item newLocalPage = null;
int webBusinessID = 0;
string ID = "";
WebBusiness business;

//Get all of the immediate child nodes (state pages) under the "parent" node ("National Locations") - and put them into a list or array                

stateNodes = database.SelectItems("fast:/sitecore/content/Home/National Locations/*");

for (int i = 0; i < stateNodes.Length; i++)
{ 

  if (stateNodes[i].Children.Count > 0)
  {
    localNodes = database.SelectItems("fast:/sitecore/content/Home/National Locations/" + stateNodes[i].Fields["State Abbreviation"].ToString() + "/*");
  }
  else
  {
    //Do nothing
  }                   

  for (int j = 0; j < localNodes.Length; j++)
  {
    localHomePage = localNodes[j];

    if (localHomePage.Publishing.IsPublishable(DateTime.Now, false) == true)                        
    {   

      //If the new page does not exist, create it
      if (localHomePage.Children[newPage] == null)
      {
        newLocalPage = localHomePage.Add(newPage, contentPageTemplate);
        counter = counter + 1;
      }
      else
      {
        //Additional business logic
      }
    }
  }
}
1
It will be easier to help you improve it if you post sample code showing from a code perspective what you're doing.Mark Ursino
It's quite a bit of code. Basically, it just builds a generic list of 50 items (representing the 50 states). It then goes through each of the 50 items in the list and creates another list of items (for the local nodes) for each state. It then iterates though the list of local nodes (one state at a time), first checking if the item exists, and then creating it if it doesn't. The code works, it's just timing out during the operation because it takes so long to complete the task.sean
So, to be clear: you (1) create 50 Items, (2) loop over the newly created 50 Items and create X many sub-Items with a null check? Can you maybe update the question with a sample tree showing the structure?Mark Ursino
Not exactly, Mark. The sample code has been added, above.sean

1 Answers

0
votes

Unless I'm missing logic/code you don't have there, I think you can simply trim this down to one query to get to the local nodes by changing the XPath:

localNodes = database.SelectItems("fast:/sitecore/content/Home/National Locations/*/*");

The change is to get all immediate sub-items of the immediate sub-items of "National Locations"