本文整理汇总了C#中IEntity.GetDraft方法的典型用法代码示例。如果您正苦于以下问题:C# IEntity.GetDraft方法的具体用法?C# IEntity.GetDraft怎么用?C# IEntity.GetDraft使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IEntity
的用法示例。
在下文中一共展示了IEntity.GetDraft方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetDictionaryFromEntity
/// <summary>
/// Convert an entity into a lightweight dictionary, ready to serialize
/// </summary>
/// <param name="entity"></param>
/// <param name="language"></param>
/// <returns></returns>
public virtual Dictionary<string, object> GetDictionaryFromEntity(IEntity entity, string language)
{
var lngs = new[] {language};
// Convert Entity to dictionary
// If the value is a relationship, then give those too, but only Title and Id
var entityValues = (from d in entity.Attributes select d.Value).ToDictionary(k => k.Name, v =>
{
var value = entity.GetBestValue(v.Name, lngs, true);
if (v.Type == "Entity" && value is Data.EntityRelationship)
return ((Data.EntityRelationship) value).Select(p => new
{
Id = p?.EntityId,
Title = p?.GetBestValue("EntityTitle", lngs)?.ToString()
}).ToList();
return value;
}, StringComparer.OrdinalIgnoreCase);
// Add Id and Title
// ...only if these are not already existing with this name in the entity itself as an internal value
if (entityValues.ContainsKey("Id")) entityValues.Remove("Id");
entityValues.Add("Id", entity.EntityId);
if (IncludeGuid)
{
if (entityValues.ContainsKey("Guid")) entityValues.Remove("Guid");
entityValues.Add("Guid", entity.EntityGuid);
}
if (IncludePublishingInfo)
{
entityValues.Add("RepositoryId", entity.RepositoryId);
entityValues.Add("IsPublished", entity.IsPublished);
if (entity.IsPublished && entity.GetDraft() != null)
{
// do a check if there was a field called Published, which we must remove for this to work
if (entityValues.ContainsKey(Constants.DraftEntityField))
entityValues.Remove(Constants.DraftEntityField);
entityValues.Add(Constants.DraftEntityField, new
{
entity.GetDraft().RepositoryId,
});
}
if (!entity.IsPublished & entity.GetPublished() != null)
{
// do a check if there was a field called Published, which we must remove for this to work
if (entityValues.ContainsKey(Constants.PublishedEntityField))
entityValues.Remove(Constants.PublishedEntityField);
entityValues.Add(Constants.PublishedEntityField, new
{
entity.GetPublished().RepositoryId,
});
}
}
if (IncludeMetadata)
{
if(entity.Metadata.HasMetadata)
entityValues.Add("Metadata", entity.Metadata);
}
if (!entityValues.ContainsKey("Title"))
try // there are strange cases where the title is missing, then just ignore this
{
entityValues.Add("Title", entity.GetBestValue("EntityTitle", lngs, true));
}
// ReSharper disable once EmptyGeneralCatchClause
catch
{
}
return entityValues;
}