SharePoint文档库文件夹特殊字符转义

当我们在SharePoint网站文档库中新建文件夹时包含了~ " # % & * : < > ? / \ { | }字符时(一共15个),

或者以.开头或者结束,或者包含连续的.(加上.16个)

会得到如下图的提示:

这在网页上使用时并没有什么问题,改个名字就好了。

不过,你可能遇到这样一种场景

你的文件夹结构树,是从别的什么地方同步过来的,比如某第三方组织机构里,刚好有一个"A部&B部"的数据,而你们的解决方案是根据组织机构名称命名文件夹。

呃,你要做转义了。

我刚好做了一个,拿走不谢。

 1 using System.Collections.Generic;
 2 using System.Linq;
 3 namespace SharePointCharacter
 4 {
 5     public static class CharacterFormatter
 6     {
 7         private static readonly Dictionary<string, string> Characters = new Dictionary<string, string> {
 8            {"~","[td]"},
 9            {"\"","[qt]"},
10            {"#","[hs]"},
11            {"%","[md]"},
12            {"&","[nd]"},
13            {"*","[st]"},
14            {":","[cl]"},
15            {"<","[lt]"},
16            {">","[gt]"},
17            {"?","[qs]"},
18            {"/","[sl]"},
19            {"\\","[bs]"},
20            {"{","[lb]"},
21            {"|","[pp]"},
22            {"}","[rb]"},
23            {".","[dt]"}
24         };
25 
26         public static string Encode(string content)
27         {
28             foreach (string key in Characters.Keys)
29                 if (content.Contains(key))
30                     content = content.Replace(key, Characters[key]);
31             return content;
32         }
33 
34         public static string Decode(string content)
35         {
36             foreach (string value in Characters.Values)
37                 if (content.Contains(value))
38                     content = content.Replace(value, Characters.FirstOrDefault(x => x.Value == value).Key);
39             return content;
40         }
41     }
42 }
View Code

 

命名参考:

就酱。

 

posted @ 2015-12-21 17:15  Evolving  阅读(463)  评论(0编辑  收藏  举报