SharePoint 2010: Convert a SharePoint document name to a Friendly name that you can save

SharePoint 2010: Convert a SharePoint document name to a Friendly name that you can save

Convert a SharePoint name to a Friendly name that you can save, returning max of 180 characters
private static readonly Regex InvalidCharsRegex =
        new Regex(@"[\*\?\|\\\t/:""'<>#{}%~&]", RegexOptions.Compiled);
 
        private static readonly Regex InvalidRulesRegex =
        new Regex(@"\.{2,}", RegexOptions.Compiled);
 
        private static readonly Regex StartEndRegex =
        new Regex(@"^[\. ]|[\. ]$", RegexOptions.Compiled);
 
        private static readonly Regex ExtraSpacesRegex =
        new Regex(" {2,}", RegexOptions.Compiled);
 
        /// <summary>
        /// Returns a folder or file name that
        /// conforms to SharePoint's naming restrictions
        /// </summary>
        /// <param name="original">
        /// The original file or folder name. 
        /// For files, this should be the file name without the extension.
        /// </param>
        public static string GetSharePointFriendlyName(string original)
        {
            // remove invalid characters and some initial replacements
            var friendlyName = ExtraSpacesRegex.Replace(
            InvalidRulesRegex.Replace(
            InvalidCharsRegex.Replace(
            original, String.Empty).Trim()
            , ".")
            , " ");
 
            // Check beginning and end for periods and spaces
            while (StartEndRegex.IsMatch(friendlyName))
                friendlyName = StartEndRegex.Replace(
                friendlyName, String.Empty);
 
            return friendlyName.Length > 180 ? friendlyName.Substring(0, 180) : friendlyName;
        }
Leave a Comment
  • Please add 5 and 6 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Gokan Ozcifci edited Revision 2. Comment: Tag & Title  

Page 1 of 1 (1 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Gokan Ozcifci edited Revision 2. Comment: Tag & Title  

Page 1 of 1 (1 items)