/// <summary> /// Sets tags for the property - will add tags to the tags table and set the property value to be the comma delimited value of the tags. /// </summary> /// <param name="content">The content item to assign the tags to</param> /// <param name="storageType">The tag storage type in cache (default is csv)</param> /// <param name="propertyTypeAlias">The property alias to assign the tags to</param> /// <param name="tags">The tags to assign</param> /// <param name="replaceTags">True to replace the tags on the current property with the tags specified or false to merge them with the currently assigned ones</param> /// <param name="tagGroup">The group/category to assign the tags, the default value is "default"</param> /// <returns></returns> public static void SetTags(this IContentBase content, TagCacheStorageType storageType, string propertyTypeAlias, IEnumerable <string> tags, bool replaceTags, string tagGroup = "default") { var property = content.Properties[propertyTypeAlias]; if (property == null) { throw new IndexOutOfRangeException("No property exists with name " + propertyTypeAlias); } property.SetTags(storageType, propertyTypeAlias, tags, replaceTags, tagGroup); }
internal static void SetTags(this Property property, TagCacheStorageType storageType, string propertyTypeAlias, IEnumerable <string> tags, bool replaceTags, string tagGroup = "default") { if (property == null) { throw new ArgumentNullException("property"); } var trimmedTags = tags.Select(x => x.Trim()).ToArray(); property.TagSupport.Enable = true; property.TagSupport.Tags = trimmedTags.Select(x => new Tuple <string, string>(x, tagGroup)); property.TagSupport.Behavior = replaceTags ? PropertyTagBehavior.Replace : PropertyTagBehavior.Merge; //ensure the property value is set to the same thing if (replaceTags) { switch (storageType) { case TagCacheStorageType.Csv: property.Value = string.Join(",", trimmedTags); break; case TagCacheStorageType.Json: //json array property.Value = JsonConvert.SerializeObject(trimmedTags); break; } } else { switch (storageType) { case TagCacheStorageType.Csv: var currTags = property.Value.ToString().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) .Select(x => x.Trim()); property.Value = string.Join(",", trimmedTags.Union(currTags)); break; case TagCacheStorageType.Json: var currJson = JsonConvert.DeserializeObject <JArray>(property.Value.ToString()); //need to append the new ones foreach (var tag in trimmedTags) { currJson.Add(tag); } //json array property.Value = JsonConvert.SerializeObject(currJson); break; } } }
public void CreateFile(FileStream file, string[] tags, string Name) { if (ModelState.IsValid) { StringBuilder fileNames = new StringBuilder(); //var idOfYourContent = -1; fileNames.Append("<ul>"); var ms = Services.MediaService; //You can do something with the files here like save them to disk or upload them into the Umbraco Media section fileNames.Append("<li>"); fileNames.Append(file); fileNames.Append("</li>"); if (file != null && file.Length > 0) { // 1086 is the id of the Files folder where the files will be uploaded. var mediaImage = ms.CreateMedia(Name, 1054, "CustomFile"); var optionalTagGroup = "default"; mediaImage.SetValue("umbracoFile", Name, file); TagCacheStorageType storageType = TagCacheStorageType.Json; file.Close(); ms.Save(mediaImage); var mediaId = mediaImage.Id; IMedia content = ms.GetById(mediaId); if (tags.Length > 0) { content.SetTags(storageType, "tags", tags, true, optionalTagGroup); } ms.Save(content); } fileNames.Append("</ul>"); TempData["FileNames"] = fileNames.ToString(); } }
/// <summary> /// Sets the tag values on the content property based on the property editor's tags attribute /// </summary> /// <param name="property">The content's Property</param> /// <param name="convertedPropertyValue"> /// If the <see cref="TagValueType"/> is <see cref="TagValueType.FromDelimitedValue"/> then this is expected to be a delimited string, /// otherwise if it is <see cref="TagValueType.CustomTagList"/> then this is expected to be IEnumerable{string} /// </param> /// <param name="delimiter">The delimiter for the value if convertedPropertyValue is a string delimited value</param> /// <param name="replaceTags">Whether or not to replace the tags with the new value or append them (true to replace, false to append)</param> /// <param name="tagGroup">The tag group to use when tagging</param> /// <param name="valueType">Defines how the tag values will be extracted</param> /// <param name="storageType">Defines how to store the tags in cache (CSV or Json)</param> internal static void SetPropertyTags(Property property, object convertedPropertyValue, string delimiter, bool replaceTags, string tagGroup, TagValueType valueType, TagCacheStorageType storageType) { if (convertedPropertyValue == null) { convertedPropertyValue = ""; } switch (valueType) { case TagValueType.FromDelimitedValue: var tags = convertedPropertyValue.ToString().Split(new[] { delimiter }, StringSplitOptions.RemoveEmptyEntries); property.SetTags(storageType, property.Alias, tags, replaceTags, tagGroup); break; case TagValueType.CustomTagList: //for this to work the object value must be IEnumerable<string> var stringList = convertedPropertyValue as IEnumerable <string>; if (stringList != null) { property.SetTags(storageType, property.Alias, stringList, replaceTags, tagGroup); } else { //it's not enumerable string, so lets see if we can automatically make it that way based on the current storage type switch (storageType) { case TagCacheStorageType.Csv: var split = convertedPropertyValue.ToString().Split(new[] { delimiter }, StringSplitOptions.RemoveEmptyEntries); //recurse with new value SetPropertyTags(property, split, delimiter, replaceTags, tagGroup, valueType, storageType); break; case TagCacheStorageType.Json: try { var parsedJson = JsonConvert.DeserializeObject <IEnumerable <string> >(convertedPropertyValue.ToString()); if (parsedJson != null) { //recurse with new value SetPropertyTags(property, parsedJson, delimiter, replaceTags, tagGroup, valueType, storageType); } } catch (Exception ex) { LogHelper.WarnWithException <TagExtractor>("Could not automatically convert stored json value to an enumerable string", ex); } break; default: throw new ArgumentOutOfRangeException("storageType"); } } break; } }