I have the following code in my controller:
string[] componentIds = new string[] {"tcm:26-123","tcm:26-456", "tcm:26-789"};
ComponentFactory cf = new ComponentFactory();
var components = cf.GetComponents(componentIds);
All 3x components use the same dynamic component template which contains standard DD4T logic.
The code executes without errors when the 3x components are published but I get the following error when I unpublish one of the components:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Source Error:
Line 155: var components =
Line 156: componentUris
Line 157: .Select(componentUri => { TcmUri uri = new TcmUri(componentUri); return (T.ComponentPresentation)GetComponentPresentationFactory(uri.PublicationId).FindAllComponentPresentations(componentUri)[0]; })
Line 158: .Where(cp => cp != null)
Line 159: .Select(cp => cp.Content)
Notice that the code FindAllComponentPresentations(componentUri)[0] is assuming a result (e.g. [0]) and that this is causing the error.
The DD4T method GetComponents has a comment 'Returns the Component contents which could be found. Components that couldn't be found don't appear in the list.' but this doesn't seem to be true because FindAllComponentPresentations errors if the component doesn't exist, rather than returning null.
Question: Is GetComponents working in the expected behaviour?
Thanks in advance