Beispiel #1
0
        private Item GetItem(ID itemId, Language language, Data.Version version)
        {
            Item item;

            if (language == null && version == null)
            {
                item = WorkingDatabase.GetItem(itemId);
            }
            else if (language == null && version != null)
            {
                item = WorkingDatabase.GetItem(itemId, Sitecore.Context.Language, version);
            }
            else if (language != null && version == null)
            {
                item = WorkingDatabase.GetItem(itemId, language);
            }
            else
            {
                item = WorkingDatabase.GetItem(itemId, language, version);
            }

            return(item);
        }
Beispiel #2
0
        public bool CreateItem(CsvReader csv, Language language, ImportItemResult importItemResult)
        {
            Item         parentItem  = null;
            TemplateItem template    = null;
            string       newItemName = string.Empty;

            //Make sure we are dealing with a valid row
            if (csv == null || csv.FieldHeaders == null || !csv.FieldHeaders.Any() || csv.CurrentRecord == null || !csv.CurrentRecord.Any())
            {
                CreateErrorResponse(importItemResult, String.Format("Could not create item, in row {0}, row was empty", csv.Row.ToString()));
                return(false);
            }

            foreach (var header in csv.FieldHeaders)
            {
                switch (header)
                {
                case Constants.FIELD_PARENT_ID:

                    ID itemId = ParseIdOrError(csv, header);

                    if (language == null)
                    {
                        parentItem = WorkingDatabase.GetItem(itemId);
                    }
                    else
                    {
                        parentItem = WorkingDatabase.GetItem(itemId, language);
                    }

                    Assert.IsNotNull(parentItem, "ParentItem {0} not found or not accessible", itemId);

                    break;

                case Constants.FIELD_ITEM_NAME:

                    newItemName = csv.GetField <string>(header);
                    importItemResult.ItemName = newItemName;

                    break;

                case Constants.FIELD_TEMPLATE_ID:

                    ID templateId = ParseIdOrError(csv, header);
                    template = WorkingDatabase.GetTemplate(templateId);
                    Assert.IsNotNull(template, "Template {0} not found or not accessible", templateId);

                    break;
                }
            }

            try
            {
                Item newItem = parentItem.Add(newItemName, template);

                if (newItem == null)
                {
                    CreateErrorResponse(importItemResult, String.Format("Error when creating item {0}", newItemName));
                    return(false);
                }

                importItemResult.ItemId = newItem.ID;

                return(UpdateItem(csv, newItem, importItemResult));
            }
            catch (Exception ex)
            {
                if (ex is FormatException || ex is InvalidOperationException)
                {
                    CreateErrorResponse(importItemResult, ex.Message);
                    return(false);
                }
                else
                {
                    throw new Exception(String.Format("A fatal error occured while creating item {0}", newItemName), ex.InnerException);
                }
            }
        }