private Item CreateOrGetProject(string gcProjectId, string gcProjectName) { var projectItem = GetProject(gcProjectId); if (projectItem != null) { return(projectItem); } var accountSettings = _accountsRepository.GetAccountSettings(); var parentItem = GetItem(accountSettings.AccountItemId); using (new SecurityDisabler()) { var template = ContextDatabase.GetTemplate(new ID(Constants.GcProject)); var validFolderName = ItemUtil.ProposeValidItemName(gcProjectName); projectItem = parentItem.Add(validFolderName, template); using (new SecurityDisabler()) { projectItem.Editing.BeginEdit(); projectItem.Fields["Id"].Value = gcProjectId; projectItem.Fields["Name"].Value = gcProjectName; projectItem.Editing.EndEdit(); } CreateProjectFolders(projectItem.ID.ToString()); } return(projectItem); }
private void SetupLinkedGcTemplate(TemplateMapping templateMapping) { var linkedGcTemplate = ContextDatabase.GetTemplate(new ID(Constants.GCLinkItemTemplateID)); if (templateMapping.CmsTemplate == null) { return; } var sitecoreTemplate = GetItem(templateMapping.CmsTemplate.TemplateId); if (sitecoreTemplate == null) { return; } var baseTemplates = sitecoreTemplate[FieldIDs.BaseTemplate]; if (!baseTemplates.ToLower().Contains(linkedGcTemplate.ID.ToString().ToLower())) { using (new SecurityDisabler()) { sitecoreTemplate.Editing.BeginEdit(); sitecoreTemplate[Sitecore.FieldIDs.BaseTemplate] = string.Format("{0}|{1}", baseTemplates, linkedGcTemplate.ID); sitecoreTemplate.Editing.EndEdit(); } } }
/// <summary> /// </summary> /// <param name="parentId"></param> /// <param name="cmsItem"></param> /// <returns></returns> public string CreateNotMappedItem(string parentId, CmsItem cmsItem) { if (parentId != null) { using (new SecurityDisabler()) { using (new LanguageSwitcher(cmsItem.Language)) { var templateItem = ContextDatabase.GetItem("/sitecore/templates/GatherContent/Not Mapped Item"); if (templateItem != null) { var template = ContextDatabase.GetTemplate(new ID(templateItem.ID.Guid)); var validName = cmsItem.Title.ProposeValidItemName(); var parent = ContextDatabase.GetItem(new ID(parentId)); if (parent != null) { try { var createdItem = parent.Add(validName, template); return createdItem.ID.ToString(); } catch (Exception) { throw new Exception(string.Format("Item cannot be created: parent='{0}l cmsItem.Title='{1}'", parent.Name, cmsItem.Title)); } } } } } } return null; }
private void CreateProjectFolders(string id) { var names = new[] { Constants.MappingFolderName }; foreach (var name in names) { using (new SecurityDisabler()) { var folder = GetItem(id); var project = ContextDatabase.GetTemplate(new ID(Constants.GcFolderId)); var validFolderName = ItemUtil.ProposeValidItemName(name); folder.Add(validFolderName, project); } } }
/// <summary> /// </summary> /// <param name="parentId"></param> /// <param name="cmsItem"></param> /// <param name="mappingId"></param> /// <param name="gcPath"></param> public string CreateMappedItem(string parentId, CmsItem cmsItem, string mappingId, string gcPath) { if (parentId != null) { using (new SecurityDisabler()) { using (new LanguageSwitcher(cmsItem.Language)) { using (new EnforceVersionPresenceDisabler()) { var template = ContextDatabase.GetTemplate(new ID(cmsItem.Template.TemplateId)); var validName = ItemUtil.ProposeValidItemName(cmsItem.Title); var parent = ContextDatabase.GetItem(new ID(parentId)); if (parent != null) { var createdItem = parent.Add(validName, template); Log.Error("createdItem is null:" + (createdItem == null), this); try { EnsureMetaTemplateInherited(createdItem.Template); var idField = cmsItem.Fields.FirstOrDefault(f => f.TemplateField.FieldName == GcContentId); if (idField != null) { createdItem.Editing.BeginEdit(); createdItem.Fields[GcContentId].Value = idField.Value.ToString(); var isoDate = DateUtil.ToIsoDate(DateTime.UtcNow); createdItem.Fields[LastSyncDate].Value = isoDate; createdItem.Fields[MappingId].Value = mappingId; createdItem.Fields[GcPath].Value = gcPath; createdItem.Editing.EndEdit(); } return(createdItem.ID.ToString()); } catch (Exception ex) { Log.Error("cannot create mapped item.", ex, this); throw new Exception(string.Format("Your template({0}) is not inherited from the GC Linked Item.", createdItem.TemplateName)); } } } } } } return(null); }
private void CreateFieldMapping(Item templateMappingItem, FieldMapping fieldMapping) { var validName = ItemUtil.ProposeValidItemName(fieldMapping.GcField.Name + " " + fieldMapping.GcField.Id); using (new SecurityDisabler()) { var mapping = ContextDatabase.GetTemplate(new ID(Constants.GcFieldMapping)); var createdItem = templateMappingItem.Add(validName, mapping); using (new SecurityDisabler()) { createdItem.Editing.BeginEdit(); createdItem.Fields["GC Field"].Value = fieldMapping.GcField.Name; createdItem.Fields["GC Field Id"].Value = fieldMapping.GcField.Id; createdItem.Fields["Sitecore Field"].Value = fieldMapping.CmsField.TemplateField.FieldId; createdItem.Editing.EndEdit(); } } }
private Item CreateTemplateMapping(TemplateMapping templateMapping) { var scProject = CreateOrGetProject(templateMapping.GcProjectId, templateMapping.GcProjectName); if (scProject == null) { return(null); } var mappingsFolder = GetMappingFolder(scProject); if (mappingsFolder == null) { return(null); } using (new SecurityDisabler()) { var mapping = ContextDatabase.GetTemplate(new ID(Constants.GcTemplateMapping)); SetupLinkedGcTemplate(templateMapping); var validFolderName = ItemUtil.ProposeValidItemName(templateMapping.GcTemplate.GcTemplateName); var createdItem = mappingsFolder.Add(validFolderName, mapping); using (new SecurityDisabler()) { createdItem.Editing.BeginEdit(); createdItem.Fields["Sitecore Template"].Value = templateMapping.CmsTemplate.TemplateId; createdItem.Fields["Default Location"].Value = templateMapping.DefaultLocationId; if (!string.IsNullOrEmpty(templateMapping.MappingTitle)) { createdItem.Fields["Template mapping title"].Value = templateMapping.MappingTitle; } createdItem.Fields["GC Template"].Value = templateMapping.GcTemplate.GcTemplateId; createdItem.Fields["Last Mapped Date"].Value = DateUtil.ToIsoDate(DateTime.Now); createdItem.Fields["Last Updated in GC"].Value = templateMapping.LastUpdatedDate; createdItem.Editing.EndEdit(); } return(createdItem); } }